1
1
package gateway
2
2
3
3
import (
4
+ "context"
4
5
"errors"
5
6
"net/http"
7
+ "strings"
6
8
7
9
"github.com/gin-gonic/gin"
8
10
"github.com/textileio/go-buckets/pinning"
@@ -32,7 +34,9 @@ func (g *Gateway) listPins(c *gin.Context, key string) {
32
34
return
33
35
}
34
36
35
- pins , err := g .ps .ListPins (thread , key , oapiQueryToQuery (query ), token )
37
+ ctx , cancel := context .WithTimeout (context .Background (), handlerTimeout )
38
+ defer cancel ()
39
+ pins , err := g .ps .ListPins (ctx , thread , key , oapiQueryToQuery (query ), token )
36
40
if err != nil {
37
41
newFailure (c , http .StatusBadRequest , err )
38
42
return
@@ -45,12 +49,32 @@ func (g *Gateway) listPins(c *gin.Context, key string) {
45
49
c .JSON (http .StatusOK , res )
46
50
}
47
51
52
+ // @todo: Validate Cids
48
53
func oapiQueryToQuery (q openapi.Query ) queue.Query {
49
54
var (
55
+ status []openapi.Status
50
56
before , after string
51
57
limit int
52
58
meta map [string ]string
53
59
)
60
+ if len (q .Status ) != 0 {
61
+ for _ , p := range strings .Split (q .Status , "," ) {
62
+ var s openapi.Status
63
+ switch p {
64
+ case "queued" :
65
+ s = openapi .QUEUED
66
+ case "pinning" :
67
+ s = openapi .PINNING
68
+ case "pinned" :
69
+ s = openapi .PINNED
70
+ case "failed" :
71
+ s = openapi .FAILED
72
+ default :
73
+ continue
74
+ }
75
+ status = append (status , s )
76
+ }
77
+ }
54
78
if q .Before != nil {
55
79
before = queue .NewIDFromTime (* q .Before )
56
80
}
@@ -67,7 +91,7 @@ func oapiQueryToQuery(q openapi.Query) queue.Query {
67
91
Cid : q .Cid ,
68
92
Name : q .Name ,
69
93
Match : q .Match ,
70
- Status : q . Status ,
94
+ Status : status ,
71
95
Before : before ,
72
96
After : after ,
73
97
Limit : limit ,
@@ -97,7 +121,9 @@ func (g *Gateway) addPin(c *gin.Context, key string) {
97
121
return
98
122
}
99
123
100
- status , err := g .ps .AddPin (thread , key , pin , token )
124
+ ctx , cancel := context .WithTimeout (context .Background (), handlerTimeout )
125
+ defer cancel ()
126
+ status , err := g .ps .AddPin (ctx , thread , key , pin , token )
101
127
if err != nil {
102
128
newFailure (c , http .StatusBadRequest , err )
103
129
return
@@ -122,7 +148,9 @@ func (g *Gateway) getPin(c *gin.Context, key, id string) {
122
148
return
123
149
}
124
150
125
- status , err := g .ps .GetPin (thread , key , id , token )
151
+ ctx , cancel := context .WithTimeout (context .Background (), handlerTimeout )
152
+ defer cancel ()
153
+ status , err := g .ps .GetPin (ctx , thread , key , id , token )
126
154
if errors .Is (pinning .ErrPinNotFound , err ) {
127
155
newFailure (c , http .StatusNotFound , err )
128
156
return
@@ -156,7 +184,9 @@ func (g *Gateway) replacePin(c *gin.Context, key, id string) {
156
184
return
157
185
}
158
186
159
- status , err := g .ps .ReplacePin (thread , key , id , pin , token )
187
+ ctx , cancel := context .WithTimeout (context .Background (), handlerTimeout )
188
+ defer cancel ()
189
+ status , err := g .ps .ReplacePin (ctx , thread , key , id , pin , token )
160
190
if errors .Is (pinning .ErrPinNotFound , err ) {
161
191
newFailure (c , http .StatusNotFound , err )
162
192
return
@@ -184,7 +214,9 @@ func (g *Gateway) removePin(c *gin.Context, key, id string) {
184
214
return
185
215
}
186
216
187
- if err := g .ps .RemovePin (thread , key , id , token ); errors .Is (pinning .ErrPinNotFound , err ) {
217
+ ctx , cancel := context .WithTimeout (context .Background (), handlerTimeout )
218
+ defer cancel ()
219
+ if err := g .ps .RemovePin (ctx , thread , key , id , token ); errors .Is (pinning .ErrPinNotFound , err ) {
188
220
newFailure (c , http .StatusNotFound , err )
189
221
return
190
222
} else if err != nil {
0 commit comments