-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
81 lines (67 loc) · 1.94 KB
/
utils.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
// Shared Utils
package mindbodybrivo
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"os"
)
// Clients and Transports are safe for concurrent use by multiple goroutines and for efficiency should only be created once and re-used
var client = &http.Client{}
var transport = &http.Transport{Proxy: http.ProxyFromEnvironment}
// JSONError is a custom error type for diagnosing server responses
type JSONError struct {
Code int
Body map[string]interface{}
}
// Render error message to string
func (e *JSONError) Error() string {
return fmt.Sprintf("Error code %d and output:\n%+v\n", e.Code, e.Body)
}
// DoRequest is a utility function for making and handling async requests.
// It accepts an http.Request and `output` as pointer to structure that will Unmarshal into.
func DoRequest(req *http.Request, output interface{}) error {
// Proxy Debugging
// Enable proxy: export https_proxy="http://localhost:8888"
if os.Getenv("PROXY") == "true" {
client = &http.Client{Transport: transport}
}
// Disable proxy: unset https_proxy
// Make request
res, err := client.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
// Handle response
data, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
}
// Check for error response
if res.StatusCode >= 400 {
var errorOut map[string]interface{}
if err = json.Unmarshal(data, &errorOut); err != nil {
return err
}
// Use JSONError type so we can handle specific error codes from the API server
return &JSONError{res.StatusCode, errorOut}
}
// Don't attempt to Unmarshal 204's
if res.StatusCode == 204 {
return nil
}
// Build response into output *interface{}
if err = json.Unmarshal(data, output); err != nil {
return fmt.Errorf("Error unmarshalling json: %s", err)
}
// fmt.Printf("Async Output: %+v\n", output)
return nil
}
// Logger wraps Println in a DEBUG env check
func Logger(message string) {
if os.Getenv("DEBUG") == "true" {
fmt.Println(message)
}
}