/
json.go
179 lines (133 loc) · 3.07 KB
/
json.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
178
179
package response
import (
"encoding/json"
"errors"
"fmt"
"github.com/tidwall/gjson"
"github.com/whosonfirst/go-whosonfirst-api"
"github.com/whosonfirst/go-whosonfirst-api/result"
"github.com/whosonfirst/go-whosonfirst-api/util"
_ "log"
"net/http"
)
type JSONPagination struct {
page int
pages int
per_page int
total int
cursor string
next_query string
}
func (p JSONPagination) Page() int {
return p.page
}
func (p JSONPagination) Pages() int {
return p.pages
}
func (p JSONPagination) PerPage() int {
return p.per_page
}
func (p JSONPagination) Total() int {
return p.total
}
func (p JSONPagination) Cursor() string {
return p.cursor
}
func (p JSONPagination) NextQuery() string {
return p.next_query
}
func (p JSONPagination) String() string {
return fmt.Sprintf("total %d page %d/%d (%d per page) cursor %s next_query %s", p.Total(), p.Page(), p.Pages(), p.PerPage(), p.Cursor(), p.NextQuery())
}
type JSONError struct {
api.APIError
code int64
message string
}
func (e JSONError) Code() int64 {
return e.code
}
func (e JSONError) Message() string {
return e.message
}
func (e JSONError) String() string {
return fmt.Sprintf("%d %s", e.Code(), e.Message())
}
type JSONResponse struct {
api.APIResponse
raw []byte
}
func (rsp JSONResponse) Raw() []byte {
return rsp.raw
}
func (rsp JSONResponse) String() string {
return string(rsp.raw)
}
func (rsp JSONResponse) Stat() string {
r := rsp.get("stat")
return r.String()
}
func (rsp JSONResponse) Ok() (bool, api.APIError) {
if rsp.Stat() == "ok" {
return true, nil
}
code := rsp.get("error.code")
msg := rsp.get("error.message")
err := JSONError{
code: code.Int(),
message: msg.String(),
}
return false, &err
}
func (rsp JSONResponse) Places() ([]api.APIPlacesResult, error) {
results := make([]api.APIPlacesResult, 0)
_results := rsp.get("places")
// TO DO: signal failed NewJSONResult
_results.ForEach(func(key, value gjson.Result) bool {
_result, err := result.NewJSONResult(value)
if err != nil {
return false
}
results = append(results, _result)
return true
})
return results, nil
}
func (rsp JSONResponse) get(path string) gjson.Result {
return gjson.GetBytes(rsp.raw, path)
}
func (rsp JSONResponse) Pagination() (api.APIPagination, error) {
page := rsp.get("page")
if !page.Exists() {
return nil, errors.New("Response is not paginated")
}
pages := rsp.get("pages")
per_page := rsp.get("per_page")
total := rsp.get("total")
cursor := rsp.get("cursor")
next_query := rsp.get("next_query")
pg := JSONPagination{
page: int(page.Int()),
pages: int(pages.Int()),
per_page: int(per_page.Int()),
total: int(total.Int()),
cursor: cursor.String(),
next_query: next_query.String(),
}
return &pg, nil
}
func ParseJSONResponse(http_rsp *http.Response) (*JSONResponse, error) {
raw, err := util.HTTPResponseToBytes(http_rsp)
if err != nil {
return nil, err
}
var stub interface{}
err = json.Unmarshal(raw, &stub)
if err != nil {
return nil, err
}
rsp := JSONResponse{
raw: raw,
}
return &rsp, nil
}