snippet/exceptionless/api.go

64 lines
1.4 KiB
Go
Raw Normal View History

2022-08-13 14:04:48 +00:00
package exceptionless
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
)
// Post posts to the Exceptionless Server
func Post(endpoint string, postBody string, authorization string) string {
2022-08-14 06:22:00 +00:00
url := conf.ServerURL + endpoint
2022-08-13 14:04:48 +00:00
var jsonStr = []byte(postBody)
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
2022-08-13 14:16:09 +00:00
if err != nil {
return err.Error()
}
2022-08-13 14:04:48 +00:00
req.Header.Set("Authorization", "Bearer "+authorization)
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
2022-08-13 14:16:47 +00:00
return err.Error()
2022-08-13 14:04:48 +00:00
}
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{} {
2022-08-14 06:22:00 +00:00
url := conf.ServerURL + endpoint
2022-08-13 14:04:48 +00:00
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
}