Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added group add/remove for files #7

Merged
merged 1 commit into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,3 +250,65 @@ func DeleteGroup(id string) error {
return nil

}

func AddFile(groupId string, fileId string) error {

jwt, err := findToken()
if err != nil {
return err
}
url := fmt.Sprintf("https://api.pinata.cloud/v3/files/groups/%s/ids/%s", groupId, fileId)

req, err := http.NewRequest("PUT", url, nil)
if err != nil {
return errors.Join(err, errors.New("failed to create the request"))
}
req.Header.Set("Authorization", "Bearer "+string(jwt))
req.Header.Set("content-type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return errors.Join(err, errors.New("failed to send the request"))
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("server Returned an error %d, check CID", resp.StatusCode)
}

fmt.Println("File added to group")

return nil
}

func RemoveFile(groupId string, fileId string) error {

jwt, err := findToken()
if err != nil {
return err
}
url := fmt.Sprintf("https://api.pinata.cloud/v3/files/groups/%s/ids/%s", groupId, fileId)

req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return errors.Join(err, errors.New("failed to create the request"))
}
req.Header.Set("Authorization", "Bearer "+string(jwt))
req.Header.Set("content-type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return errors.Join(err, errors.New("failed to send the request"))
}
defer resp.Body.Close()

if resp.StatusCode != 200 {
return fmt.Errorf("server Returned an error %d, check CID", resp.StatusCode)
}

fmt.Println("File removed from group")

return nil
}
36 changes: 36 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,42 @@ func main() {
return err
},
},
{
Name: "add",
Aliases: []string{"a"},
Usage: "Add a file to a group",
ArgsUsage: "[group id] [file id]",
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
fileId := ctx.Args().Get(1)
if groupId == "" {
return errors.New("no group id provided")
}
if fileId == "" {
return errors.New("no file id provided")
}
err := AddFile(groupId, fileId)
return err
},
},
{
Name: "remove",
Aliases: []string{"r"},
Usage: "Remove a file from a group",
ArgsUsage: "[group id] [file id]",
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
fileId := ctx.Args().Get(1)
if groupId == "" {
return errors.New("no group id provided")
}
if fileId == "" {
return errors.New("no file id provided")
}
err := RemoveFile(groupId, fileId)
return err
},
},
},
},
{
Expand Down