/
destination_searcher.go
65 lines (46 loc) · 1.34 KB
/
destination_searcher.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
package transaction
import (
"bytes"
"compress/gzip"
"context"
"encoding/json"
"fmt"
"io/ioutil"
)
func (s *ServiceClient) DestinationSearcher(c DestinationSearcherCriteria) (string, error) {
//ValidateRequest
if c.Text == "" || c.Access == "" || c.MaxSize == 0 {
return "", fmt.Errorf("Error params")
}
//buildRequest
requestString := fmt.Sprintf(`{"query":"query ($criteria: HotelXDestinationSearcherInput!) {\n hotelX {\n destinationSearcher(criteria: $criteria) {\n ... on DestinationData {\n \n code\n available\n }\n }\n }\n}\n\n","variables":
{"criteria": {"text":"%s","access":"%s","maxSize":%d}}}`, c.Text, c.Access, c.MaxSize)
r := bytes.NewReader([]byte(requestString))
//Do call
resp, err := s.client.Post(context.TODO(), r)
defer func() {
if resp != nil {
resp.Close()
}
}()
rs := HotelxResponse{}
if err != nil {
return "", fmt.Errorf("Error")
}
//parseResponse
re, err := gzip.NewReader(resp)
if err != nil {
return "", fmt.Errorf("Error reader")
}
bodyResponse, err := ioutil.ReadAll(re)
if err != nil {
return "", fmt.Errorf("Error ReadAll")
}
println(string(bodyResponse))
err = json.Unmarshal(bodyResponse, &rs)
if err != nil {
return "", fmt.Errorf("Error Unmarshal")
}
return rs.Data.HotelX.DestinationSearcher[0].Code, nil
//errors control
}