-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapi_request.go
74 lines (60 loc) · 1.74 KB
/
api_request.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
74
package espocrm
import (
"bytes"
"encoding/base64"
"fmt"
"io"
"net/http"
)
func NewRequest(client *ApiClient, method, endpoint string) (*http.Request, error) {
request, err := http.NewRequest(method, endpoint, nil)
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/json")
err = AddAuthToRequest(client, request)
if err != nil {
return nil, err
}
return request, err
}
func NewRequestWithBody(client *ApiClient, method, endpoint string, body []byte) (*http.Request, error) {
request, err := http.NewRequest(method, endpoint, bytes.NewReader(body))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/json")
err = AddAuthToRequest(client, request)
if err != nil {
return nil, err
}
return request, err
}
func AddAuthToRequest(client *ApiClient, request *http.Request) error {
if client.config.apiKey != nil {
request.Header.Set("X-Api-Key", *client.config.apiKey)
return nil
}
if client.config.username != nil && client.config.password != nil {
usernameAndPassword := []byte(*client.config.username + ":" + *client.config.password)
base64EncodedAuth := base64.StdEncoding.EncodeToString(usernameAndPassword)
request.Header.Set("Authorization", "Basic "+base64EncodedAuth)
return nil
}
return fmt.Errorf("client missing credentials")
}
func SendRequest(request *http.Request) ([]byte, error) {
client := &http.Client{}
response, err := client.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
return nil, fmt.Errorf("http_status_code: %d, error: %s", response.StatusCode, response.Header.Get("X-Status-Reason"))
}
responseBody, err := io.ReadAll(response.Body)
if err != nil {
return nil, err
}
return responseBody, err
}