@@ -29,7 +29,6 @@ func NewRequest(ctx context.Context, url, command string, args ...string) *Reque
29
29
if ! strings .HasPrefix (url , "http" ) {
30
30
url = "http://" + url
31
31
}
32
-
33
32
opts := map [string ]string {
34
33
"encoding" : "json" ,
35
34
"stream-channels" : "true" ,
@@ -114,14 +113,13 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
114
113
if err != nil {
115
114
return nil , err
116
115
}
117
-
116
+
118
117
req = req .WithContext (r .Ctx )
119
118
120
119
// Add any headers that were supplied via the RequestBuilder.
121
120
for k , v := range r .Headers {
122
121
req .Header .Add (k , v )
123
122
}
124
-
125
123
if fr , ok := r .Body .(* files.MultiFileReader ); ok {
126
124
req .Header .Set ("Content-Type" , "multipart/form-data; boundary=" + fr .Boundary ())
127
125
req .Header .Set ("Content-Disposition" , "form-data; name=\" files\" " )
@@ -149,18 +147,18 @@ func (r *Request) Send(c *http.Client) (*Response, error) {
149
147
case contentType == "text/plain" :
150
148
out , err := ioutil .ReadAll (resp .Body )
151
149
if err != nil {
152
- fmt .Fprintf (os .Stderr , "ipfs-shell: warning! response (%d) read error: %s\n " , resp . StatusCode , err )
150
+ fmt .Fprintf (os .Stderr , "ipfs-shell: warning! response read error: %s\n " , err )
153
151
}
154
152
e .Message = string (out )
155
153
case contentType == "application/json" :
156
154
if err = json .NewDecoder (resp .Body ).Decode (e ); err != nil {
157
- fmt .Fprintf (os .Stderr , "ipfs-shell: warning! response (%d) unmarshall error: %s\n " , resp . StatusCode , err )
155
+ fmt .Fprintf (os .Stderr , "ipfs-shell: warning! response unmarshall error: %s\n " , err )
158
156
}
159
157
default :
160
- fmt .Fprintf (os .Stderr , "ipfs-shell: warning! unhandled response (%d) encoding: %s" , resp . StatusCode , contentType )
158
+ fmt .Fprintf (os .Stderr , "ipfs-shell: warning! unhandled response encoding: %s" , contentType )
161
159
out , err := ioutil .ReadAll (resp .Body )
162
160
if err != nil {
163
- fmt .Fprintf (os .Stderr , "ipfs-shell: response (%d) read error: %s\n " , resp . StatusCode , err )
161
+ fmt .Fprintf (os .Stderr , "ipfs-shell: response read error: %s\n " , err )
164
162
}
165
163
e .Message = fmt .Sprintf ("unknown ipfs-shell error encoding: %q - %q" , contentType , out )
166
164
}
@@ -187,3 +185,58 @@ func (r *Request) getURL() string {
187
185
188
186
return fmt .Sprintf ("%s/%s?%s" , r .ApiBase , r .Command , values .Encode ())
189
187
}
188
+
189
+ func (r * Request ) SendGET (c * http.Client ) (* Response , error ) {
190
+ url := r .getURLNoEncode ()
191
+ req , err := http .NewRequest ("GET" , url , r .Body )
192
+ if err != nil {
193
+ return nil , err
194
+ }
195
+ resp , err := c .Do (req )
196
+ if err != nil {
197
+ return nil , err
198
+ }
199
+ contentType := resp .Header .Get ("Content-Type" )
200
+ parts := strings .Split (contentType , ";" )
201
+ contentType = parts [0 ]
202
+ nresp := new (Response )
203
+ nresp .Output = resp .Body
204
+ if resp .StatusCode >= http .StatusBadRequest {
205
+ e := & Error {
206
+ Command : r .Command ,
207
+ }
208
+ switch {
209
+ case resp .StatusCode == http .StatusNotFound :
210
+ e .Message = "command not found"
211
+ case contentType == "text/plain" :
212
+ out , err := ioutil .ReadAll (resp .Body )
213
+ if err != nil {
214
+ fmt .Fprintf (os .Stderr , "ipfs-shell: warning! response read error: %s\n " , err )
215
+ }
216
+ e .Message = string (out )
217
+ case contentType == "application/json" :
218
+ if err = json .NewDecoder (resp .Body ).Decode (e ); err != nil {
219
+ fmt .Fprintf (os .Stderr , "ipfs-shell: warning! response unmarshall error: %s\n " , err )
220
+ }
221
+ default :
222
+ fmt .Fprintf (os .Stderr , "ipfs-shell: warning! unhandled response encoding: %s" , contentType )
223
+ out , err := ioutil .ReadAll (resp .Body )
224
+ if err != nil {
225
+ fmt .Fprintf (os .Stderr , "ipfs-shell: response read error: %s\n " , err )
226
+ }
227
+ e .Message = fmt .Sprintf ("unknown ipfs-shell error encoding: %q - %q" , contentType , out )
228
+ }
229
+ nresp .Error = e
230
+ nresp .Output = nil
231
+
232
+ // drain body and close
233
+ ioutil .ReadAll (resp .Body )
234
+ resp .Body .Close ()
235
+ }
236
+
237
+ return nresp , nil
238
+ }
239
+
240
+ func (r * Request ) getURLNoEncode () string {
241
+ return fmt .Sprintf ("%s/%s/%s" , r .ApiBase , r .Command , r .Args [0 ])
242
+ }
0 commit comments