Skip to content

Commit

Permalink
clients: Create List function in IPFS Pinata client (#48)
Browse files Browse the repository at this point in the history
* clients: Create List function in IPFS Pinata client

* ipfs: Receive page offset instead of page index

Pinata API is very shitty so we have to account for deleted pins
when requesting for new pages.
  • Loading branch information
victorges authored Jul 22, 2022
1 parent 219634e commit 1ff546d
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions clients/ipfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
"time"
Expand All @@ -17,9 +18,25 @@ const (
pinataOptions = `{"cidVersion":1}`
)

type PinInfo struct {
ID string `json:"id"`
IPFSPinHash string `json:"ipfs_pin_hash"`
Size int64 `json:"size"`
Metadata struct {
Name string `json:"name"`
KeyValues map[string]string `json:"keyvalues"`
} `json:"metadata"`
}

type PinList struct {
Count int64 `json:"count"`
Pins []PinInfo `json:"rows"`
}

type IPFS interface {
PinContent(ctx context.Context, name, contentType string, data io.Reader) (cid string, metadata interface{}, err error)
Unpin(ctx context.Context, cid string) error
List(ctx context.Context, pageSize, pageOffset int) (*PinList, int, error)
}

func NewPinataClientJWT(jwt string, filesMetadata map[string]string) IPFS {
Expand Down Expand Up @@ -90,6 +107,22 @@ func (p *pinataClient) Unpin(ctx context.Context, cid string) error {
}, nil)
}

func (p *pinataClient) List(ctx context.Context, pageSize, pageOffset int) (pl *PinList, next int, err error) {
err = p.DoRequest(ctx, Request{
Method: "GET",
URL: fmt.Sprintf("/data/pinList?status=pinned&pageLimit=%d&pageOffset=%d", pageSize, pageOffset),
}, &pl)
if err != nil {
return nil, -1, err
}

next = -1
if len(pl.Pins) >= pageSize {
next = pageOffset + len(pl.Pins)
}
return pl, next, err
}

func marshalFilesMetadata(keyvalues map[string]string) []byte {
if len(keyvalues) == 0 {
return nil
Expand Down

0 comments on commit 1ff546d

Please sign in to comment.