/
httputils.go
58 lines (47 loc) · 1.49 KB
/
httputils.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
package elvidapiclient
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"time"
)
var myClient = &http.Client{Timeout: 15 * time.Second}
func PostRequest(url string, accessToken string, jsonValue []byte) (*http.Response, error) {
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Content-Type", "application/json")
return myClient.Do(req)
}
func PatchRequest(url string, accessToken string, jsonValue []byte) (*http.Response, error) {
req, err := http.NewRequest("PATCH", url, bytes.NewBuffer(jsonValue))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
req.Header.Set("Content-Type", "application/json")
return myClient.Do(req)
}
func GetRequest(url string, accessToken string) (*http.Response, error) {
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
return myClient.Do(req)
}
func DeleteRequest(url string, accessToken string) (*http.Response, error) {
req, err := http.NewRequest("DELETE", url, nil)
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Bearer "+accessToken)
return myClient.Do(req)
}
func ElvidErrorResponse(response *http.Response, url string) error {
data, _ := ioutil.ReadAll(response.Body)
return fmt.Errorf("ElvID returned StatusCode %v for (%s), message: %s", response.StatusCode, url, data)
}