-
Notifications
You must be signed in to change notification settings - Fork 1
/
api.go
73 lines (61 loc) · 1.58 KB
/
api.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
package exceptionless
import (
"bytes"
"net/http"
"fmt"
"io/ioutil"
"encoding/json"
)
//Post posts to the Exceptionless Server
func Post(endpoint string, postBody string, authorization string) string {
exceptionless := GetClient()
var baseURL string = GetBaseURL()
if exceptionless.ServerURL != "" {
baseURL = exceptionless.ServerURL
}
url := baseURL + endpoint
var jsonStr = []byte(postBody)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req.Header.Set("Authorization", "Bearer "+authorization)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
panic(err)
}
defer resp.Body.Close()
// body, _ := ioutil.ReadAll(resp.Body)
return string(resp.Status)
}
//GET makes api GET requests
func Get(endpoint string, authorization string) map[string]interface{} {
exceptionless := GetClient()
var baseURL string = GetBaseURL()
if exceptionless.ServerURL != "" {
baseURL = exceptionless.ServerURL
}
url := baseURL + endpoint
httpClient := &http.Client{}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
fmt.Println(err)
// return "Error"
}
req.Header.Add("accept", "application/json")
req.Header.Add("Authorization", "Bearer "+authorization)
res, err := httpClient.Do(req)
if err != nil {
fmt.Println(err)
// return "Error"
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
if err != nil {
fmt.Println(err)
// return "Error"
}
// resp := string(body)
var result map[string]interface{}
json.Unmarshal([]byte(body), &result)
return result
}