forked from chargebee/chargebee-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_request.go
More file actions
48 lines (43 loc) · 1.03 KB
/
http_request.go
File metadata and controls
48 lines (43 loc) · 1.03 KB
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
package chargebee
import (
"encoding/json"
"errors"
"io/ioutil"
"net/http"
)
var httpClient = &http.Client{Timeout: DefaultHTTPTimeout}
//Do is used to execute an API Request.
func Do(req *http.Request) (string, error) {
response, err := httpClient.Do(req)
if err != nil {
return "", err
}
defer response.Body.Close()
resBody, err := ioutil.ReadAll(response.Body)
if err != nil {
return "", err
}
if response.StatusCode < 200 || response.StatusCode > 299 {
return "", ErrorHandling(resBody)
}
return string(resBody), nil
}
func ErrorHandling(resBody []byte) error {
cbErr := &Error{}
err := json.Unmarshal(resBody, cbErr)
if err != nil {
return err
}
if cbErr.APIErrorCode == "" {
return errors.New("the api_error_code is not present - probably not a chargebee error")
}
switch cbErr.Type {
case PaymentError:
cbErr.Err = &paymentErr{cbErr: cbErr}
case InvalidRequestError:
cbErr.Err = &invalidRequestErr{cbErr: cbErr}
case OperationFailedError:
cbErr.Err = &operationFailedErr{cbErr: cbErr}
}
return cbErr
}