forked from algebraic-brain/algorithmia-go
-
Notifications
You must be signed in to change notification settings - Fork 2
/
get.go
52 lines (42 loc) · 812 Bytes
/
get.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
package algorithmia
import (
"encoding/json"
"io/ioutil"
"net/http"
)
func getRaw(r *http.Response) ([]byte, error) {
b, err := ioutil.ReadAll(r.Body)
if err != nil {
return nil, err
}
if r.StatusCode != http.StatusOK {
return nil, errorFromJsonData(b)
}
return b, nil
}
func getJson(r *http.Response, out interface{}) error {
b, err := getRaw(r)
if err != nil {
return err
}
err = json.Unmarshal(b, out)
return err
}
func getAsyncResp(r *http.Response) (*AsyncResponse, error) {
var ar AsyncResponse
err := getJson(r, &ar)
if err != nil {
return nil, err
}
if ar.Error != nil {
return nil, ar.Error
}
return &ar, nil
}
func getAlgoResp(r *http.Response) (*AlgoResponse, error) {
b, err := getRaw(r)
if err != nil {
return nil, err
}
return createAlgoResponse(b)
}