Skip to content

Commit

Permalink
feat: Added update file and update group
Browse files Browse the repository at this point in the history
  • Loading branch information
stevedylandev committed Oct 9, 2024
1 parent da2f6a8 commit c4ec6b0
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
54 changes: 53 additions & 1 deletion files.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -61,7 +62,58 @@ func GetFile(id string) (GetFileResponse, error) {
defer resp.Body.Close()

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

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

fmt.Println(string(formattedJSON))

return response, nil

}

func UpdateFile(id string, name string) (GetFileResponse, error) {
jwt, err := findToken()
if err != nil {
return GetFileResponse{}, err
}
payload := FileUpdateBody{
Name: name,
}

jsonPayload, err := json.Marshal(payload)

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

url := fmt.Sprintf("https://api.pinata.cloud/v3/files/%s", id)

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

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

Expand Down
53 changes: 53 additions & 0 deletions groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,59 @@ func CreateGroup(name string, isPublic bool) (GroupCreateResponse, error) {

}

func UpdateGroup(id string, 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/%s", id)
req, err := http.NewRequest("PUT", 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 {
Expand Down
51 changes: 51 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,35 @@ func main() {
return err
},
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "Update a group",
ArgsUsage: "[ID of group]",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: "public",
Aliases: []string{"p"},
Value: false,
Usage: "Determine if the group should be public",
},
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Update the name of a group",
},
},
Action: func(ctx *cli.Context) error {
groupId := ctx.Args().First()
name := ctx.String("name")
public := ctx.Bool("public")
if groupId == "" {
return errors.New("no ID provided")
}
_, err := UpdateGroup(groupId, name, public)
return err
},
},
{
Name: "delete",
Aliases: []string{"d"},
Expand Down Expand Up @@ -186,6 +215,28 @@ func main() {
return err
},
},
{
Name: "update",
Aliases: []string{"u"},
Usage: "Update a file by ID",
ArgsUsage: "[ID of file]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "name",
Aliases: []string{"n"},
Usage: "Update the name of a file",
},
},
Action: func(ctx *cli.Context) error {
fileId := ctx.Args().First()
name := ctx.String("name")
if fileId == "" {
return errors.New("no ID provided")
}
_, err := UpdateFile(fileId, name)
return err
},
},
{
Name: "list",
Aliases: []string{"l"},
Expand Down

0 comments on commit c4ec6b0

Please sign in to comment.