Skip to content

Commit

Permalink
feat: Added group add/remove for files
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedylandev committed Oct 23, 2024
1 parent f4f3b69 commit 8af20d6
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
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

0 comments on commit 8af20d6

Please sign in to comment.