-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
99 lines (84 loc) · 2.9 KB
/
common.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
package typeform
import (
"bytes"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strconv"
)
// NewClient creates a new API client
func NewClient(APIVersion APIVersion) (*Client, error) {
return &Client{
httpClient: http.DefaultClient,
APIVersion: APIVersion,
}, nil
}
func (client *Client) fetchAndReturnPage(path string, method string, headers http.Header, queryParameters url.Values, bodyPayload interface{}) ([]byte, http.Header, error) {
if client.Config.APIKey == "" {
return []byte(""), http.Header{}, fmt.Errorf("%s", "APIKey not provided")
}
domain := "https://api.typeform.io/"
requestURL, err := url.Parse(domain)
if err != nil {
return []byte(""), http.Header{}, err
}
requestURL.Path = path
requestURL.RawQuery = queryParameters.Encode()
if method != "GET" && method != "POST" && method != "PUT" && method != "PATCH" && method != "DELETE" {
return []byte(""), http.Header{}, fmt.Errorf("Method not supported: %v", method)
}
encodedBody, err := json.Marshal(bodyPayload)
if err != nil {
return []byte(""), http.Header{}, err
}
//fmt.Println(requestURL.String())
request, err := http.NewRequest(method, requestURL.String(), bytes.NewBuffer(encodedBody))
if err != nil {
return []byte(""), http.Header{}, fmt.Errorf("Failed to get the URL %s: %s", requestURL, err)
}
request.Header = headers
request.Header.Add("Content-Length", strconv.Itoa(len(encodedBody)))
request.Header.Add("Connection", "Keep-Alive")
request.Header.Add("Accept-Encoding", "gzip, deflate")
request.Header.Add("Content-Type", "application/json")
request.Header.Add("User-Agent", "github.com/gagliardetto/go-ask-awesomely")
request.Header.Add("X-API-TOKEN", client.Config.APIKey)
response, err := client.httpClient.Do(request)
if err != nil {
return []byte(""), http.Header{}, fmt.Errorf("Failed to get the URL %s: %s", requestURL, err)
}
defer response.Body.Close()
var responseReader io.ReadCloser
switch response.Header.Get("Content-Encoding") {
case "gzip":
decompressedBodyReader, err := gzip.NewReader(response.Body)
if err != nil {
return []byte(""), http.Header{}, err
}
responseReader = decompressedBodyReader
defer responseReader.Close()
default:
responseReader = response.Body
}
responseBody, err := ioutil.ReadAll(responseReader)
if err != nil {
return []byte(""), http.Header{}, err
}
if response.StatusCode > 299 || response.StatusCode < 199 {
var apiError APIError
err = json.Unmarshal(responseBody, &apiError)
if err != nil {
return []byte(""), http.Header{}, nil
}
//fmt.Println(string(responseBody))
return []byte(""), http.Header{}, fmt.Errorf("HTTPStatus %s: %s", strconv.Itoa(response.StatusCode), apiError.String())
}
return responseBody, response.Header, nil
}
func (apiError *APIError) String() string {
return fmt.Sprintf("Error: %q; Field: %q; Description: %q", apiError.Error, apiError.Field, apiError.Description)
}