-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
96 lines (79 loc) · 2.46 KB
/
client.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package healthcrm
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
"time"
"github.com/savannahghi/authutils"
"github.com/savannahghi/serverutils"
)
// IAuthUtilsLib holds the method defined in authutils library
type authUtilsLib interface {
Authenticate() (*authutils.OAUTHResponse, error)
}
// client is the library's client used to make requests
type client struct {
authClient authUtilsLib
httpClient *http.Client
}
// newClient is the constructor which initializes health crm's authentication mechanism
func newClient() (*client, error) {
config := authutils.Config{
AuthServerEndpoint: serverutils.MustGetEnvVar("HEALTH_CRM_AUTH_SERVER_ENDPOINT"),
ClientID: serverutils.MustGetEnvVar("HEALTH_CRM_CLIENT_ID"),
ClientSecret: serverutils.MustGetEnvVar("HEALTH_CRM_CLIENT_SECRET"),
GrantType: serverutils.MustGetEnvVar("HEALTH_CRM_GRANT_TYPE"),
Username: serverutils.MustGetEnvVar("HEALTH_CRM_USERNAME"),
Password: serverutils.MustGetEnvVar("HEALTH_CRM_PASSWORD"),
}
slade360AuthClient, err := authutils.NewClient(config)
if err != nil {
return nil, err
}
return &client{
authClient: slade360AuthClient,
httpClient: &http.Client{
Timeout: time.Second * 10,
},
}, nil
}
// MakeRequest performs a HTTP request to the provided path and parameters
func (cl *client) MakeRequest(ctx context.Context, method, path string, queryParams url.Values, body interface{}) (*http.Response, error) {
oauthResponse, err := cl.authClient.Authenticate()
if err != nil {
return nil, err
}
urlPath := fmt.Sprintf("%s%s", BaseURL, path)
var request *http.Request
switch method {
case http.MethodGet:
req, err := http.NewRequestWithContext(ctx, method, urlPath, nil)
if err != nil {
return nil, err
}
request = req
case http.MethodPost, http.MethodPatch:
encoded, err := json.Marshal(body)
if err != nil {
return nil, err
}
payload := bytes.NewBuffer(encoded)
req, err := http.NewRequestWithContext(ctx, method, urlPath, payload)
if err != nil {
return nil, err
}
request = req
default:
return nil, fmt.Errorf("s.MakeRequest() unsupported http method: %s", method)
}
request.Header.Set("Accept", "application/json")
request.Header.Set("Content-Type", "application/json")
request.Header.Set("Authorization", fmt.Sprintf("Bearer %s", oauthResponse.AccessToken))
if queryParams != nil {
request.URL.RawQuery = queryParams.Encode()
}
return cl.httpClient.Do(request)
}