-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
178 lines (157 loc) · 4.12 KB
/
api.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
package worldota
import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"github.com/molodsom/worldota-sdk-go/models"
"io"
"io/ioutil"
"log"
"net/http"
)
type APIClient struct {
http.Client
}
type APIRoundTripper struct {
id string
token string
r http.RoundTripper
}
func (rt APIRoundTripper) RoundTrip(r *http.Request) (*http.Response, error) {
auth := base64.StdEncoding.EncodeToString([]byte(rt.id + ":" + rt.token))
r.Header.Add("Content-Type", "application/json")
r.Header.Add("Accept-Charset", "utf-8")
r.Header.Add("Authorization", "Basic "+auth)
return http.DefaultTransport.RoundTrip(r)
}
func Client(id, token string) (c APIClient) {
c.Transport = APIRoundTripper{id: id, token: token}
return
}
func (c APIClient) request(method, endpoint string, payload []byte) (body []byte) {
url := fmt.Sprintf("https://api.worldota.net/api/b2b/v3/%s/", endpoint)
req, err := http.NewRequest(method, url, bytes.NewBuffer(payload))
if err != nil {
log.Fatal(err)
}
res, _ := c.Do(req)
defer func(Body io.ReadCloser) {
err := Body.Close()
if err != nil {
log.Fatal(err)
}
}(res.Body)
if res.StatusCode != 200 {
log.Fatal(fmt.Sprintf("Server returned %d in response to URL: %s\n", res.StatusCode, url))
return
}
body, _ = ioutil.ReadAll(res.Body)
return
}
func (c APIClient) get(endpoint string, payload []byte) (body []byte) {
body = c.request("GET", endpoint, payload)
return
}
func (c APIClient) post(endpoint string, payload []byte) (body []byte) {
body = c.request("POST", endpoint, payload)
return
}
func (c APIClient) Overview() (res models.Overview) {
r := c.get("overview", nil)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) HotelInfoDump(req models.LanguageRequest) (res models.HotelDump) {
payload, _ := json.Marshal(req)
r := c.post("hotel/info/dump", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) HotelInfoIncrementalDump(req models.LanguageRequest) (res models.HotelDump) {
payload, _ := json.Marshal(req)
r := c.post("hotel/info/incremental_dump", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) HotelReviewsDump(req models.LanguageRequest) (res models.HotelDump) {
payload, _ := json.Marshal(req)
r := c.post("hotel/reviews/dump", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) HotelRegionDump(req models.LanguageRequest) (res models.HotelDump) {
payload, _ := json.Marshal(req)
r := c.post("hotel/region/dump", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) HotelInfo(req models.HotelInfoRequest) (res models.HotelInfo) {
payload, _ := json.Marshal(req)
r := c.post("hotel/info", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) SearchMulticomplete(req models.SearchMulticompleteRequest) (res models.SearchMulticomplete) {
payload, _ := json.Marshal(req)
r := c.post("search/multicomplete", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) SearchSERPRegion(req models.SearchRegionRequest) (res models.SearchResult) {
payload, _ := json.Marshal(req)
r := c.post("search/serp/region", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) SearchSERPGeo(req models.SearchGeoRequest) (res models.SearchResult) {
payload, _ := json.Marshal(req)
r := c.post("search/serp/geo", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) SearchSERPHotels(req models.SearchHotelsRequest) (res models.SearchResult) {
payload, _ := json.Marshal(req)
r := c.post("search/serp/hotels", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}
func (c APIClient) SearchHotelPage(req models.SearchHotelPage) (res models.SearchResult) {
payload, _ := json.Marshal(req)
r := c.post("search/hp", payload)
err := json.Unmarshal(r, &res)
if err != nil {
log.Fatal(err)
}
return
}