Skip to content

Commit

Permalink
feat: Added Groups and listing groups
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedylandev committed Sep 6, 2024
1 parent 156108b commit 60228fd
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 3 deletions.
63 changes: 63 additions & 0 deletions groups.go
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
package main

import (
"encoding/json"
"errors"
"fmt"
"net/http"
"strings"
)

func ListGroups(amount string, isPublic bool) (GroupListResponse, error) {
jwt, err := findToken()
if err != nil {
return GroupListResponse{}, err
}
url := fmt.Sprintf("https://api.pinata.cloud/v3/files/groups?")

params := []string{}

if amount != "" {
params = append(params, "limit="+amount)
}

if isPublic {
params = append(params, "isPublic=true")
}
if len(params) > 0 {
url += strings.Join(params, "&")
}

req, err := http.NewRequest("GET", url, nil)
if err != nil {
return GroupListResponse{}, 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 GroupListResponse{}, errors.Join(err, errors.New("failed to send the request"))
}
defer resp.Body.Close()

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

var response GroupListResponse

err = json.NewDecoder(resp.Body).Decode(&response)
if err != nil {
return GroupListResponse{}, err
}
formattedJSON, err := json.MarshalIndent(response.Data, "", " ")
if err != nil {
return GroupListResponse{}, errors.New("failed to format JSON")
}

fmt.Println(string(formattedJSON))

return response, nil

}
33 changes: 32 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,43 @@ func main() {
return err
},
},
{
Name: "groups",
Aliases: []string{"g"},
Usage: "Interact with file groups",
Subcommands: []*cli.Command{
{
Name: "list",
Aliases: []string{"l"},
Usage: "List groups on your account",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "public",
Aliases: []string{"p"},
Value: true,
Usage: "List only public groups",
},
&cli.StringFlag{
Name: "amount",
Aliases: []string{"a"},
Value: "10",
Usage: "The number of groups you would like to return",
},
},
Action: func(ctx *cli.Context) error {
public := ctx.Bool("public")
amount := ctx.String("amount")
_, err := ListGroups(amount, public)
return err
},
},
},
},
{
Name: "files",
Aliases: []string{"f"},
Usage: "Interact with your files on Pinata",
Subcommands: []*cli.Command{

{
Name: "delete",
Aliases: []string{"d"},
Expand Down
6 changes: 4 additions & 2 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ type GroupResponseItem struct {
}

type GroupListResponse struct {
Groups []GroupResponseItem `json:"groups"`
NextPageToken string `json:"next_page_token"`
Data struct {
Groups []GroupResponseItem `json:"groups"`
NextPageToken string `json:"next_page_token"`
} `json:"data"`
}

0 comments on commit 60228fd

Please sign in to comment.