-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
53 lines (44 loc) · 1008 Bytes
/
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
package golamap
import (
"encoding/json"
"io"
"net/http"
)
type OlaRequest struct{}
func (o *OlaRequest) SendOlaMapRequest(method, url, requestID, oauthToken string, responseObj interface{}) error {
// Create a new request
req, err := http.NewRequest(method, url, nil)
if err != nil {
return err
}
req.Header.Add("X-Request-Id", requestID)
if oauthToken != "" {
req.Header.Add("Authorization", oauthToken)
}
// Send the request
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
// Read the response body
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
// Parse the JSON response
err = json.Unmarshal(body, responseObj)
if err != nil {
return err
}
return nil
}
// ParseJSONBody parses the JSON request body into the provided struct.
func ParseJSONBody(r *http.Request, v interface{}) error {
body, err := io.ReadAll(r.Body)
if err != nil {
return err
}
return json.Unmarshal(body, v)
}