-
Notifications
You must be signed in to change notification settings - Fork 3
/
Response.go
115 lines (93 loc) · 2.34 KB
/
Response.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
package HologramGo
// This file contains all HTTP Response related behavior.
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
)
// HTTP status codes
const (
STATUS_OK = 200
STATUS_CREATED = 201
STATUS_ACCEPTED = 202
STATUS_NO_CONTENT = 204
STATUS_INVALID = 400
STATUS_UNAUTHORIZED = 401
STATUS_FORBIDDEN = 403
STATUS_NOTFOUND = 404
STATUS_LIMIT = 429
STATUS_GATEWAY = 502
)
// EFFECTS: Takes in the response and unmarshalls it into the appropriate interface.
func unmarshallIntoObject(resp *Response) map[string]interface{} {
var payload = Placeholder{}
err = resp.parsePayload(&payload)
// error handling
if err != nil {
fmt.Printf("Problem parsing response: %v\n", err)
os.Exit(1)
}
return payload["data"].(map[string]interface{})
}
// EFFECTS: Takes in the response and unmarshalls into an array object.
func unmarshallIntoArrayObject(resp *Response) []interface{} {
var payload = Placeholder{}
err = resp.parsePayload(&payload)
// error handling
if err != nil {
fmt.Printf("Problem parsing response: %v\n", err)
os.Exit(1)
}
return payload["data"].([]interface{})
}
type Response http.Response
func (response Response) parseBody() (b []byte, err error) {
var (
//header string
reader io.Reader
)
defer response.Body.Close()
reader = response.Body
b, err = ioutil.ReadAll(reader)
return b, err
}
// EFFECTS: Populates the given object based on the returned response
func (response Response) parsePayload(out interface{}) (err error) {
var b []byte
statusCode := response.StatusCode
if statusCode == STATUS_UNAUTHORIZED || statusCode == STATUS_NOTFOUND ||
statusCode == STATUS_GATEWAY || statusCode == STATUS_FORBIDDEN ||
statusCode == STATUS_INVALID {
e := &Errors{}
b, err = response.parseBody()
if err != nil {
return err
}
err = json.Unmarshal(b, e)
if err != nil {
err = NewResponseError(response.StatusCode, string(b))
} else {
err = *e
}
} else if statusCode == STATUS_CREATED || statusCode == STATUS_ACCEPTED ||
statusCode == STATUS_OK {
b, err = response.parseBody()
if err != nil {
return err
}
err = json.Unmarshal(b, out)
if err == io.EOF {
err = nil
}
} else {
b, err = response.parseBody()
if err != nil {
return err
}
err = NewResponseError(response.StatusCode, string(b))
}
return err
}