-
Notifications
You must be signed in to change notification settings - Fork 1
/
hugo.go
273 lines (218 loc) · 5.58 KB
/
hugo.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package hugo
import (
"encoding/json"
"io/ioutil"
"net/http"
"strconv"
"strings"
)
const (
baseUrl = "https://api.hurriyet.com.tr/v1/"
)
// HurriyetAPI keeps apikey and baseUrl info.
type HurriyetAPI struct {
apiKey string
client *http.Client
baseUrl string
}
// NewHurriyetAPI takes apiKey and returns new HurriyetAPI for communicating Hurriyet API.
func NewHurriyetAPI(apiKey string) *HurriyetAPI {
return &HurriyetAPI{
apiKey: apiKey,
baseUrl: baseUrl,
client: http.DefaultClient,
}
}
// ListArticles returns articles.
func (h HurriyetAPI) ListArticles(query *Query) ([]Article, error) {
data, err := h.request("articles", query)
if err != nil {
return nil, err
}
var articles []Article
if jsonErr := json.Unmarshal(data, &articles); jsonErr != nil {
// check response for error
return nil, jsonErr
}
return articles, nil
}
// SingleArticle takes an id and returns an article .
func (h HurriyetAPI) SingleArticle(Id string, query *Query) (Article, error) {
data, err := h.request("articles/"+Id, query)
if err != nil {
return Article{}, err
}
var article Article
if jsonErr := json.Unmarshal(data, &article); jsonErr != nil {
// check response for error
return Article{}, jsonErr
}
return article, nil
}
// Search takes search keyword and returns search results.
func (h HurriyetAPI) Search(keyword string, query *Query) (Search, error) {
data, err := h.request("search/"+keyword, query)
if err != nil {
return Search{}, err
}
var search Search
if jsonErr := json.Unmarshal(data, &search); jsonErr != nil {
// check response for error
return Search{}, jsonErr
}
return search, nil
}
func (h HurriyetAPI) ListColumns(query *Query) ([]Column, error) {
data, err := h.request("columns", query)
if err != nil {
return nil, err
}
var columns []Column
if jsonErr := json.Unmarshal(data, &columns); jsonErr != nil {
// check response for error
return nil, jsonErr
}
return columns, nil
}
func (h HurriyetAPI) SingleColumn(Id string, query *Query) (Column, error) {
data, err := h.request("columns/"+Id, query)
if err != nil {
return Column{}, err
}
var column Column
if jsonErr := json.Unmarshal(data, &column); jsonErr != nil {
// check response for error
return Column{}, jsonErr
}
return column, nil
}
func (h HurriyetAPI) ListPages(query *Query) ([]Page, error) {
data, err := h.request("pages", query)
if err != nil {
return nil, err
}
var pages []Page
if jsonErr := json.Unmarshal(data, &pages); jsonErr != nil {
// check response for error
return nil, jsonErr
}
return pages, nil
}
func (h HurriyetAPI) SinglePage(Id string, query *Query) (Page, error) {
data, err := h.request("pages/"+Id, query)
if err != nil {
return Page{}, err
}
var page Page
if jsonErr := json.Unmarshal(data, &page); jsonErr != nil {
// check response for error
return Page{}, jsonErr
}
return page, nil
}
func (h HurriyetAPI) ListPhotoGalleries(query *Query) ([]Article, error) {
data, err := h.request("newsphotogalleries", query)
if err != nil {
return nil, err
}
var galleries []Article
if jsonErr := json.Unmarshal(data, &galleries); jsonErr != nil {
// check response for error
return nil, jsonErr
}
return galleries, nil
}
func (h HurriyetAPI) SinglePhotoGallery(Id string, query *Query) (Article, error) {
data, err := h.request("newsphotogalleries/"+Id, query)
if err != nil {
return Article{}, err
}
var gallery Article
if jsonErr := json.Unmarshal(data, &gallery); jsonErr != nil {
// check response for error
return Article{}, jsonErr
}
return gallery, nil
}
func (h HurriyetAPI) ListPaths(query *Query) ([]Path, error) {
data, err := h.request("paths", query)
if err != nil {
return nil, err
}
var paths []Path
if jsonErr := json.Unmarshal(data, &paths); jsonErr != nil {
// check response for error
return nil, jsonErr
}
return paths, nil
}
func (h HurriyetAPI) SinglePath(Id string, query *Query) (Path, error) {
data, err := h.request("paths/"+Id, query)
if err != nil {
return Path{}, err
}
var path Path
if jsonErr := json.Unmarshal(data, &path); jsonErr != nil {
// check response for error
return Path{}, jsonErr
}
return path, nil
}
func (h HurriyetAPI) ListWriters(query *Query) ([]Writer, error) {
data, err := h.request("writers", query)
if err != nil {
return nil, err
}
var writers []Writer
if jsonErr := json.Unmarshal(data, &writers); jsonErr != nil {
// check response for error
return nil, jsonErr
}
return writers, nil
}
func (h HurriyetAPI) SingleWriter(Id string, query *Query) (Writer, error) {
data, err := h.request("writers/"+Id, query)
if err != nil {
return Writer{}, err
}
var writer Writer
if jsonErr := json.Unmarshal(data, &writer); jsonErr != nil {
// check response for error
return Writer{}, jsonErr
}
return writer, nil
}
func (h HurriyetAPI) request(url string, query *Query) ([]byte, error) {
req, err := http.NewRequest("GET", h.baseUrl+url, nil)
req.Header.Add("apikey", h.apiKey)
req.Header.Add("accept", "application/json")
if query != nil {
q := req.URL.Query()
if len(query.Filter) > 0 {
q.Add("$filter", query.Filter)
}
if len(query.Select) > 0 {
q.Add("$select", strings.Join(query.Select, ","))
}
if query.Top > 0 {
q.Add("$top", strconv.Itoa(query.Top))
}
if query.Skip > 0 {
q.Add("$skip", strconv.Itoa(query.Skip))
}
if query.S != "" {
q.Add("s", query.S)
}
req.URL.RawQuery = q.Encode()
}
resp, err := h.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return body, nil
}