Skip to content
This repository has been archived by the owner on Oct 5, 2023. It is now read-only.

Commit

Permalink
Implement Pin API
Browse files Browse the repository at this point in the history
  • Loading branch information
magik6k committed Jan 15, 2019
1 parent 281b2bf commit 5bb7a58
Showing 1 changed file with 87 additions and 5 deletions.
92 changes: 87 additions & 5 deletions pin.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package httpapi

import (
"context"
"encoding/json"
"github.com/ipfs/go-cid"
"github.com/pkg/errors"

"github.com/ipfs/go-ipfs/core/coreapi/interface"
caopts "github.com/ipfs/go-ipfs/core/coreapi/interface/options"
Expand Down Expand Up @@ -31,8 +33,14 @@ func (p *pin) Type() string {
return p.typ
}

func (api *PinAPI) Add(context.Context, iface.Path, ...caopts.PinAddOption) error {
panic("implement me")
func (api *PinAPI) Add(ctx context.Context, p iface.Path, opts ...caopts.PinAddOption) error {
options, err := caopts.PinAddOptions(opts...)
if err != nil {
return err
}

return api.core().request("pin/add", p.String()).
Option("recursive", options.Recursive).Exec(ctx, nil)
}

func (api *PinAPI) Ls(ctx context.Context, opts ...caopts.PinLsOption) ([]iface.Pin, error) {
Expand Down Expand Up @@ -65,11 +73,85 @@ func (api *PinAPI) Rm(ctx context.Context, p iface.Path) error {
}

func (api *PinAPI) Update(ctx context.Context, from iface.Path, to iface.Path, opts ...caopts.PinUpdateOption) error {
panic("implement me")
options, err := caopts.PinUpdateOptions(opts...)
if err != nil {
return err
}

return api.core().request("pin/update").
Option("unpin", options.Unpin).Exec(ctx, nil)
}

type pinVerifyRes struct {
Cid string
JOk bool `json:"Ok"`
JBadNodes []*badNode `json:"BadNodes,omitempty"`
}

func (r *pinVerifyRes) Ok() bool {
return r.JOk
}

func (r *pinVerifyRes) BadNodes() []iface.BadPinNode {
out := make([]iface.BadPinNode, len(r.JBadNodes))
for i, n := range r.JBadNodes {
out[i] = n
}
return out
}

type badNode struct {
Cid string
JErr string `json:"Err"`
}

func (api *PinAPI) Verify(context.Context) (<-chan iface.PinStatus, error) {
panic("implement me")
func (n *badNode) Path() iface.ResolvedPath {
c, err := cid.Parse(n.Cid)
if err != nil {
return nil // todo: handle this better
}
return iface.IpldPath(c)
}

func (n *badNode) Err() error {
if n.JErr != "" {
return errors.New(n.JErr)
}
if _, err := cid.Parse(n.Cid); err != nil {
return err
}
return nil
}

func (api *PinAPI) Verify(ctx context.Context) (<-chan iface.PinStatus, error) {
resp, err := api.core().request("pin/verify").Option("verbose", true).Send(ctx)
if err != nil {
return nil, err
}
if resp.Error != nil {
return nil, resp.Error
}
res := make(chan iface.PinStatus)

go func() {
defer resp.Close()
defer close(res)
dec := json.NewDecoder(resp.Output)
for {
var out pinVerifyRes
if err := dec.Decode(&out); err != nil {
return // todo: handle non io.EOF somehow
}

select {
case res <- &out:
case <-ctx.Done():
return
}
}
}()

return res, nil
}

func (api *PinAPI) core() *HttpApi {
Expand Down

0 comments on commit 5bb7a58

Please sign in to comment.