-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlatch_api.go
More file actions
86 lines (71 loc) · 1.97 KB
/
latch_api.go
File metadata and controls
86 lines (71 loc) · 1.97 KB
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
package golatch
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
)
type LatchAPI struct {
Proxy *url.URL
OnRequestStart func(request *LatchRequest)
OnResponseReceive func(request *LatchRequest, response *http.Response, responseBody string)
}
func (l *LatchAPI) DoRequest(request *LatchRequest, responseType LatchResponse) (response *LatchResponse, err error) {
var client *http.Client
var resp *http.Response
var body []byte
//Initialize the client
client = &http.Client{}
if l.Proxy != nil {
client.Transport = &http.Transport{Proxy: http.ProxyURL(l.Proxy)}
}
//Perform the request
req := request.GetHttpRequest()
if l.OnRequestStart != nil {
l.OnRequestStart(request)
}
if resp, err = client.Do(req); err != nil {
return
}
//Get the response's body
defer resp.Body.Close()
if body, err = ioutil.ReadAll(resp.Body); err != nil {
return
}
if l.OnResponseReceive != nil {
l.OnResponseReceive(request, resp, string(body))
}
//Handle HTTP errors
if resp.StatusCode != 200 {
err = errors.New(fmt.Sprintf("HTTP error [%d] body: %s", resp.StatusCode, body))
return
}
//Check if the response is an error before decoding it
latch_error_response := &LatchErrorResponse{}
if err = json.Unmarshal(body, latch_error_response); err != nil {
return
} else if (*latch_error_response).Err.Code != 0 {
err = &latch_error_response.Err
return
}
//Decode response into a typed response (if one has been specified)
if responseType != nil {
err = responseType.Unmarshal(string(body))
response = &responseType
}
return response, err
}
//Sets the proxy URL to be used in all requests to the API
func (l *LatchAPI) SetProxy(proxyURL *url.URL) {
l.Proxy = proxyURL
}
//Gets the complete url for a request
func GetLatchURL(queryString string) *url.URL {
latch_url, err := (&url.URL{}).Parse(fmt.Sprint(API_URL, API_PATH, "/", API_VERSION, "/", queryString))
if err != nil {
latch_url = &url.URL{}
}
return latch_url
}