-
Notifications
You must be signed in to change notification settings - Fork 5
/
misc.go
60 lines (49 loc) · 1.44 KB
/
misc.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
package ravepay
import (
"bytes"
"encoding/json"
"log"
"net/http"
)
// Bool is a helper routine that allocates a new bool value
// to store v and returns a pointer to it.
func Bool(v bool) *bool { return &v }
// Int is a helper routine that allocates a new int value
// to store v and returns a pointer to it.
func Int(v int) *int { return &v }
// String is a helper routine that allocates a new string value
// to store v and returns a pointer to it.
func String(v string) *string { return &v }
func sendRequestAndParseResponse(mtd, url string, payload, respObj interface{}) error {
resp, err := sendRequest(mtd, url, payload)
if err != nil {
log.Println("Error occured while making request", err)
return err
}
err = json.NewDecoder(resp.Body).Decode(respObj)
if err != nil {
log.Println("Error occured while parsing response body", err)
}
return err
}
func sendRequest(mtd, url string, payload interface{}) (*http.Response, error) {
var req *http.Request
var err error
if payload != nil {
body, err := json.Marshal(payload)
if err != nil {
log.Println("Error marshalling request payload: ", err)
return nil, err
}
req, err = http.NewRequest(mtd, url, bytes.NewBuffer(body))
} else {
req, err = http.NewRequest(mtd, url, nil)
}
if err != nil {
log.Println("Error occured while creating request", err)
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
return client.Do(req)
}