-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
42 lines (31 loc) · 930 Bytes
/
client.go
File metadata and controls
42 lines (31 loc) · 930 Bytes
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
package five9
import (
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"net/http"
"strings"
"github.com/Sceptyre/go-five9-scim/pkg/five9/models"
)
var client = &http.Client{}
var authString string = ""
func Login(username string, password string) {
auth := username + ":" + password
authString = base64.StdEncoding.EncodeToString([]byte(auth))
}
func Request[T any](method string, uri string, body []byte) (*T, *models.Five9ErrorResponse) {
var responseData T
var errorData models.Five9ErrorResponse
req, _ := http.NewRequest(method, uri, strings.NewReader(string(body)))
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", authString))
res, err := client.Do(req)
responseBytes, _ := io.ReadAll(res.Body)
if err != nil || res.StatusCode != 200 {
xml.Unmarshal(responseBytes, &errorData)
return nil, &errorData
} else {
xml.Unmarshal(responseBytes, &responseData)
return &responseData, nil
}
}