Skip to content

Commit

Permalink
feat: Added group delete and creation
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedylandev committed Sep 8, 2024
1 parent 0622b5f commit 4743d6d
Show file tree
Hide file tree
Showing 3 changed files with 153 additions and 35 deletions.
86 changes: 86 additions & 0 deletions groups.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand All @@ -24,6 +25,7 @@ func ListGroups(amount string, isPublic bool) (GroupListResponse, error) {
if isPublic {
params = append(params, "isPublic=true")
}

if len(params) > 0 {
url += strings.Join(params, "&")
}
Expand Down Expand Up @@ -62,3 +64,87 @@ func ListGroups(amount string, isPublic bool) (GroupListResponse, error) {
return response, nil

}

func CreateGroup(name string, isPublic bool) (GroupCreateResponse, error) {
jwt, err := findToken()
if err != nil {
return GroupCreateResponse{}, err
}

payload := GroupCreateBody{
Name: name,
IsPublic: isPublic,
}

jsonPayload, err := json.Marshal(payload)

if err != nil {
return GroupCreateResponse{}, errors.Join(err, errors.New("Failed to marshal paylod"))
}

url := fmt.Sprintf("https://api.pinata.cloud/v3/files/groups")
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
if err != nil {
return GroupCreateResponse{}, 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 GroupCreateResponse{}, errors.Join(err, errors.New("failed to send the request"))
}
defer resp.Body.Close()

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

var response GroupCreateResponse

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

fmt.Println(string(formattedJSON))

return response, nil

}

func DeleteGroup(id string) error {
jwt, err := findToken()
if err != nil {
return err
}
url := fmt.Sprintf("https://api.pinata.cloud/v3/files/groups/%s", id)

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("Group Deleted")

return nil

}
91 changes: 56 additions & 35 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ func main() {
Usage: "Upload a file or folder to Pinata",
ArgsUsage: "[path to file]",
Flags: []cli.Flag{
&cli.IntFlag{
Name: "version",
Aliases: []string{"v"},
Value: 1,
Usage: "Set desired CID version to either 0 or 1. Default is 1.",
&cli.StringFlag{
Name: "group",
Aliases: []string{"g"},
Value: "",
Usage: "Upload a file to a specific group by passing in the groupId",
},
&cli.StringFlag{
Name: "name",
Expand All @@ -52,7 +52,7 @@ func main() {
},
Action: func(ctx *cli.Context) error {
filePath := ctx.Args().First()
groupId := ctx.String("groupId")
groupId := ctx.String("group")
name := ctx.String("name")
cidOnly := ctx.Bool("cid-only")
if filePath == "" {
Expand All @@ -67,6 +67,31 @@ func main() {
Aliases: []string{"g"},
Usage: "Interact with file groups",
Subcommands: []*cli.Command{
{
Name: "create",
Aliases: []string{"c"},
Usage: "Create a new group",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Required: true,
Usage: "The name you want to give for a group",
},
&cli.BoolFlag{
Name: "public",
Aliases: []string{"p"},
Value: false,
Usage: "Determine if the group should be public or not",
},
},
Action: func(ctx *cli.Context) error {
name := ctx.String("name")
public := ctx.Bool("public")
_, err := CreateGroup(name, public)
return err
},
},
{
Name: "list",
Aliases: []string{"l"},
Expand All @@ -75,7 +100,6 @@ func main() {
&cli.BoolFlag{
Name: "public",
Aliases: []string{"p"},
Value: true,
Usage: "List only public groups",
},
&cli.StringFlag{
Expand All @@ -92,6 +116,20 @@ func main() {
return err
},
},
{
Name: "delete",
Aliases: []string{"d"},
Usage: "Delete a group by ID",
ArgsUsage: "[ID of group]",
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
if groupId == "" {
return errors.New("no ID provided")
}
err := DeleteGroup(groupId)
return err
},
},
},
},
{
Expand All @@ -109,7 +147,7 @@ func main() {
if fileId == "" {
return errors.New("no CID provided")
}
err := Delete(fileId)
err := DeleteFile(fileId)
return err
},
},
Expand All @@ -118,44 +156,27 @@ func main() {
Aliases: []string{"l"},
Usage: "List most recent files",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "cid",
Aliases: []string{"c"},
Value: "null",
Usage: "Search files by CID",
},
&cli.StringFlag{
Name: "amount",
Aliases: []string{"a"},
Value: "10",
Usage: "The number of files you would like to return, default 10 max 1000",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Value: "null",
Usage: "The name of the file",
},
&cli.StringFlag{
Name: "status",
Aliases: []string{"s"},
Value: "pinned",
Usage: "Status of the file. Options are 'pinned', 'unpinned', or 'all'. Default: 'pinned'",
},
&cli.StringFlag{
Name: "pageOffset",
Name: "pageToken",
Aliases: []string{"p"},
Value: "null",
Usage: "Allows you to paginate through files. If your file amount is 10, then you could set the pageOffset to '10' to see the next 10 files.",
Usage: "Allows you to paginate through files.",
},
&cli.BoolFlag{
Name: "cidPending",
Value: true,
Usage: "Filter results based on whether or not the CID is pending",
},
},
Action: func(ctx *cli.Context) error {
cid := ctx.String("cid")
amount := ctx.String("amount")
name := ctx.String("name")
status := ctx.String("status")
offset := ctx.String("pageOffset")
_, err := ListFiles(amount, cid, name, status, offset)
pageToken := ctx.String("pageToken")
cidPending := ctx.Bool("cidPending")
_, err := ListFiles(amount, pageToken, cidPending)
return err
},
},
Expand Down
11 changes: 11 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,14 @@ type GroupListResponse struct {
NextPageToken string `json:"next_page_token"`
} `json:"data"`
}

type GroupCreateResponse struct {
Data struct {
GroupResponseItem
} `json:"data"`
}

type GroupCreateBody struct {
Name string `json:"name"`
IsPublic bool `json:"is_public"`
}

0 comments on commit 4743d6d

Please sign in to comment.