This repository has been archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
model.go
177 lines (151 loc) · 4.58 KB
/
model.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
package go_pinning_service_http_client
import (
"encoding/json"
"fmt"
"time"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-pinning-service-http-client/openapi"
"github.com/multiformats/go-multiaddr"
)
// PinGetter Getter for Pin object
//
// Deprecated: use github.com/ipfs/boxo/pinning/remote/client.PinGetter
type PinGetter interface {
fmt.Stringer
json.Marshaler
// CID to be pinned recursively
GetCid() cid.Cid
// Optional name for pinned data; can be used for lookups later
GetName() string
// Optional list of multiaddrs known to provide the data
GetOrigins() []string
// Optional metadata for pin object
GetMeta() map[string]string
}
type pinObject struct {
openapi.Pin
}
func (p *pinObject) MarshalJSON() ([]byte, error) {
var originsStr string
if o := p.GetOrigins(); o != nil {
originsBytes, err := json.Marshal(o)
if err == nil {
originsStr = string(originsBytes)
}
}
metaStr := "{}"
if meta := p.GetMeta(); meta != nil {
metaBytes, err := json.Marshal(meta)
if err == nil {
metaStr = string(metaBytes)
}
}
str := fmt.Sprintf("{ \"Cid\" : \"%v\", \"Name\" : \"%s\", \"Origins\" : %v, \"Meta\" : %v }",
p.GetCid(), p.GetName(), originsStr, metaStr)
return []byte(str), nil
}
func (p *pinObject) String() string {
marshalled, err := json.MarshalIndent(p, "", "\t")
if err != nil {
return ""
}
return string(marshalled)
}
func (p *pinObject) GetCid() cid.Cid {
c, err := cid.Parse(p.Pin.Cid)
if err != nil {
return cid.Undef
}
return c
}
// Deprecated: use github.com/ipfs/boxo/pinning/remote/client.Status
type Status string
const (
// Deprecated: use github.com/ipfs/boxo/pinning/remote/client.StatusUnknown
StatusUnknown Status = ""
// Deprecated: use github.com/ipfs/boxo/pinning/remote/client.StatusQueued
StatusQueued Status = Status(openapi.QUEUED)
// Deprecated: use github.com/ipfs/boxo/pinning/remote/client.StatusPinning
StatusPinning Status = Status(openapi.PINNING)
// Deprecated: use github.com/ipfs/boxo/pinning/remote/client.StatusPinned
StatusPinned Status = Status(openapi.PINNED)
// Deprecated: use github.com/ipfs/boxo/pinning/remote/client.StatusFailed
StatusFailed Status = Status(openapi.FAILED)
)
func (s Status) String() string {
switch s {
case StatusQueued, StatusPinning, StatusPinned, StatusFailed:
return string(s)
default:
return string(StatusUnknown)
}
}
var validStatuses = []Status{"queued", "pinning", "pinned", "failed"}
// PinStatusGetter Getter for Pin object with status
//
// Deprecated: use github.com/ipfs/boxo/pinning/remote/client.PinStatusGetter
type PinStatusGetter interface {
fmt.Stringer
json.Marshaler
// Globally unique ID of the pin request; can be used to check the status of ongoing pinning, modification of pin object, or pin removal
GetRequestId() string
GetStatus() Status
// Immutable timestamp indicating when a pin request entered a pinning service; can be used for filtering results and pagination
GetCreated() time.Time
GetPin() PinGetter
// List of multiaddrs designated by pinning service for transferring any new data from external peers
GetDelegates() []multiaddr.Multiaddr
// Optional info for PinStatus response
GetInfo() map[string]string
}
type pinStatusObject struct {
openapi.PinStatus
}
func (p *pinStatusObject) GetDelegates() []multiaddr.Multiaddr {
delegates := p.PinStatus.GetDelegates()
addrs := make([]multiaddr.Multiaddr, 0, len(delegates))
for _, d := range delegates {
a, err := multiaddr.NewMultiaddr(d)
if err != nil {
logger.Errorf("returned delegate is an invalid multiaddr: %w", err)
continue
}
addrs = append(addrs, a)
}
return addrs
}
func (p *pinStatusObject) GetPin() PinGetter {
return &pinObject{p.Pin}
}
func (p *pinStatusObject) GetStatus() Status {
return Status(p.PinStatus.GetStatus())
}
func (p *pinStatusObject) GetRequestId() string {
return p.GetRequestid()
}
func (p *pinStatusObject) MarshalJSON() ([]byte, error) {
var delegatesStr string
if d := p.GetDelegates(); d != nil {
delegatesBytes, err := json.Marshal(d)
if err == nil {
delegatesStr = string(delegatesBytes)
}
}
infoStr := "{}"
if info := p.GetInfo(); info != nil {
infoBytes, err := json.Marshal(info)
if err == nil {
infoStr = string(infoBytes)
}
}
str := fmt.Sprintf("{\"Pin\" : %v, \"RequestID\" : \"%s\", \"Status\" : \"%s\", \"Created\" : \"%v\", \"Delegates\" : %v, \"Info\" : %v }",
p.GetPin(), p.GetRequestId(), p.GetStatus(), p.GetCreated(), delegatesStr, infoStr)
return []byte(str), nil
}
func (p *pinStatusObject) String() string {
marshalled, err := json.MarshalIndent(p, "", "\t")
if err != nil {
return ""
}
return string(marshalled)
}