-
Notifications
You must be signed in to change notification settings - Fork 1
/
do.go
52 lines (43 loc) · 1.03 KB
/
do.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
package payzego
import (
"bytes"
"context"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
func (c *PayzeClient) doRequest(ctx context.Context, requestBody, responseBody interface{}, method string) error {
requestBytes, err := json.Marshal(requestBody)
if err != nil {
return err
}
req, err := http.NewRequestWithContext(ctx, method, c.baseURL, bytes.NewBuffer(requestBytes))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/json")
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
// Check for errors in the response
errResponse, err := c.hasError(respBody, requestBody)
if err != nil {
return err
}
// If there is an error response, return it
if errResponse != nil {
return errors.New("error occurred with payze")
}
// Unmarshal the response into the provided responseBody struct
if err := json.Unmarshal(respBody, responseBody); err != nil {
return err
}
return nil
}