This repository was archived by the owner on Apr 18, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathstatus.go
244 lines (225 loc) · 5.32 KB
/
status.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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package w3s
import (
"context"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/filecoin-project/go-address"
"github.com/ipfs/go-cid"
"github.com/ipfs-cluster/ipfs-cluster/api"
peer "github.com/libp2p/go-libp2p-core/peer"
)
const iso8601 = "2006-01-02T15:04:05.999Z07:00"
type PinStatus int
const (
PinStatusPinned = PinStatus(api.TrackerStatusPinned)
PinStatusPinning = PinStatus(api.TrackerStatusPinning)
PinStatusPinQueued = PinStatus(api.TrackerStatusPinQueued)
PinStatusRemote = PinStatus(api.TrackerStatusRemote)
PinStatusUnpinned = PinStatus(api.TrackerStatusUnpinned)
PinStatusUnknown = PinStatus(-1)
)
func (s PinStatus) String() string {
if s == PinStatusPinned {
return "Pinned"
}
if s == PinStatusPinning {
return "Pinning"
}
if s == PinStatusPinQueued {
return "PinQueued"
}
if s == PinStatusRemote {
return "Remote"
}
if s == PinStatusUnpinned {
return "Unpinned"
}
return "Unknown"
}
type Pin struct {
PeerID peer.ID
PeerName string
Region string
Status PinStatus
Updated time.Time
}
type pinJson struct {
PeerID string `json:"peerId"`
PeerName string `json:"peerName"`
Region string `json:"region"`
Status string `json:"status"`
Updated string `json:"updated"`
}
func (p *Pin) UnmarshalJSON(b []byte) error {
var raw pinJson
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
p.PeerID, err = peer.Decode(raw.PeerID)
if err != nil {
return err
}
p.PeerName = raw.PeerName
p.Region = raw.Region
if raw.Status == "Pinned" {
p.Status = PinStatusPinned
} else if raw.Status == "Pinning" {
p.Status = PinStatusPinning
} else if raw.Status == "PinQueued" {
p.Status = PinStatusPinQueued
} else if raw.Status == "Remote" {
p.Status = PinStatusRemote
} else if raw.Status == "Unpinned" {
p.Status = PinStatusUnpinned
} else {
p.Status = PinStatusUnknown
}
return nil
}
type DealStatus int
const (
DealStatusQueued DealStatus = iota
DealStatusPublished
DealStatusActive
)
func (s DealStatus) String() string {
return []string{"Queued", "Published", "Active"}[s]
}
type Deal struct {
DealID uint64
StorageProvider address.Address
Status DealStatus
PieceCid cid.Cid
DataCid cid.Cid
DataModelSelector string
Activation time.Time
Created time.Time
Updated time.Time
}
type dealJson struct {
DealID uint64 `json:"dealId,omitempty"`
StorageProvider string `json:"storageProvider,omitempty"`
Status string `json:"status"`
PieceCid string `json:"pieceCid,omitempty"`
DataCid string `json:"dataCid,omitempty"`
DataModelSelector string `json:"dataModelSelector,omitempty"`
Activation string `json:"activation,omitempty"`
Created string `json:"created"`
Updated string `json:"updated"`
}
func (d *Deal) UnmarshalJSON(b []byte) error {
var raw dealJson
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
d.DealID = raw.DealID
d.StorageProvider, err = address.NewFromString(raw.StorageProvider)
if err != nil {
return err
}
if raw.Status == "Queued" {
d.Status = DealStatusQueued
} else if raw.Status == "Published" {
d.Status = DealStatusPublished
} else if raw.Status == "Active" {
d.Status = DealStatusActive
} else {
return fmt.Errorf("unknown deal status: %s", raw.Status)
}
if raw.PieceCid != "" {
d.PieceCid, err = cid.Parse(raw.PieceCid)
if err != nil {
return err
}
} else {
d.PieceCid = cid.Undef
}
if raw.DataCid != "" {
d.DataCid, err = cid.Parse(raw.DataCid)
if err != nil {
return err
}
} else {
d.DataCid = cid.Undef
}
d.DataModelSelector = raw.DataModelSelector
if raw.Activation != "" {
d.Activation, err = time.Parse(iso8601, raw.Activation)
if err != nil {
return err
}
}
if raw.Created != "" {
d.Created, err = time.Parse(iso8601, raw.Created)
if err != nil {
return err
}
}
if raw.Updated != "" {
d.Updated, err = time.Parse(iso8601, raw.Updated)
if err != nil {
return err
}
}
return nil
}
// Status is IPFS pin and Filecoin deal status for a given CID.
type Status struct {
Cid cid.Cid
DagSize uint64
Created time.Time
Pins []Pin
Deals []Deal
}
type statusJson struct {
Cid string `json:"cid"`
DagSize uint64 `json:"dagSize"`
Created string `json:"created"`
Pins []Pin `json:"pins"`
Deals []Deal `json:"deals"`
}
func (s *Status) UnmarshalJSON(b []byte) error {
var raw statusJson
err := json.Unmarshal(b, &raw)
if err != nil {
return err
}
s.Cid, err = cid.Parse(raw.Cid)
if err != nil {
return err
}
s.DagSize = raw.DagSize
s.Created, err = time.Parse(iso8601, raw.Created)
if err != nil {
return err
}
s.Pins = raw.Pins
s.Deals = raw.Deals
return nil
}
func (c *client) Status(ctx context.Context, cid cid.Cid) (*Status, error) {
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/status/%s", c.cfg.endpoint, cid), nil)
if err != nil {
return nil, err
}
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", c.cfg.token))
req.Header.Add("X-Client", clientName)
res, err := c.cfg.hc.Do(req)
if err != nil {
return nil, err
}
if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected response status: %d", res.StatusCode)
}
var s Status
d := json.NewDecoder(res.Body)
err = d.Decode(&s)
if err != nil {
return nil, err
}
return &s, nil
}