/
ipcsClient.go
77 lines (66 loc) · 1.54 KB
/
ipcsClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package storage_ipfs
import (
"errors"
"fmt"
"net/url"
. "github.com/infrago/base"
)
type (
ipcsClient struct {
Cluster string
}
ipcsPinOpt struct {
RFMin, RFMax int
Name string
Metadata Map
}
)
func (opt *ipcsPinOpt) Query() string {
qs := url.Values{}
if opt.RFMin > 0 && opt.RFMax > 0 {
qs.Set("replication-min", fmt.Sprintf("%d", opt.RFMin))
qs.Set("replication-max", fmt.Sprintf("%d", opt.RFMax))
}
if opt.Name != "" {
qs.Set("name", opt.Name)
}
//qs.Set("shard-size", fmt.Sprintf("%d", po.ShardSize))
//qs.Set("user-allocations", strings.Join(PeersToStrings(po.UserAllocations), ","))
//if !po.ExpireAt.IsZero() {
// v, err := po.ExpireAt.MarshalText()
// if err != nil {
// return "", err
// }
// q.Set("expire-at", string(v))
//}
for k, v := range opt.Metadata {
if k == "" {
continue
}
qs.Set(fmt.Sprintf("meta-%s", k), fmt.Sprintf("%s", v))
}
//if po.PinUpdate != cid.Undef {
// q.Set("pin-update", po.PinUpdate.String())
//}
return qs.Encode()
}
func (client *ipcsClient) Pin(hash string, opt *ipcsPinOpt) (Map, error) {
url := fmt.Sprintf("%s/pins/%s", client.Cluster, hash)
query := ""
if opt != nil {
query = opt.Query()
}
res, err := HttpPostJson(url+"?"+query, "application/x-www-form-urlencoded", nil)
if err != nil {
return nil, err
}
return res, nil
}
func (client *ipcsClient) Unpin(hash string) (Map, error) {
url := fmt.Sprintf("%s/pins/%s", client.Cluster, hash)
res := HttpDeleteJson(url)
if res == nil {
return nil, errors.New("http error")
}
return res, nil
}