Passed
Push — main ( 57c3b2...931586 )
by Acho
01:25
created

flutterwave.New   A

Complexity

Conditions 2

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 11
nop 1
dl 0
loc 16
rs 9.85
c 0
b 0
f 0
1
package flutterwave
2
3
import (
4
	"bytes"
5
	"context"
6
	"encoding/json"
7
	"fmt"
8
	"io"
9
	"io/ioutil"
10
	"net/http"
11
)
12
13
type service struct {
14
	client *Client
15
}
16
17
// Client is the campay API client.
18
// Do not instantiate this client with Client{}. Use the New method instead.
19
type Client struct {
20
	httpClient *http.Client
21
	common     service
22
	secretKey  string
23
	baseURL    string
24
25
	Bills *billsService
26
}
27
28
// New creates and returns a new campay.Client from a slice of campay.ClientOption.
29
func New(options ...ClientOption) *Client {
30
	config := defaultClientConfig()
31
32
	for _, option := range options {
33
		option.apply(config)
34
	}
35
36
	client := &Client{
37
		httpClient: config.httpClient,
38
		secretKey:  config.secretKey,
39
		baseURL:    config.baseURL,
40
	}
41
42
	client.common.client = client
43
	client.Bills = (*billsService)(&client.common)
44
	return client
45
}
46
47
// newRequest creates an API request. A relative URL can be provided in uri,
48
// in which case it is resolved relative to the BaseURL of the Client.
49
// URI's should always be specified without a preceding slash.
50
func (client *Client) newRequest(ctx context.Context, method, uri string, body interface{}) (*http.Request, error) {
51
	var buf io.ReadWriter
52
	if body != nil {
53
		buf = &bytes.Buffer{}
54
		enc := json.NewEncoder(buf)
55
		enc.SetEscapeHTML(false)
56
		err := enc.Encode(body)
57
		if err != nil {
58
			return nil, err
59
		}
60
	}
61
62
	req, err := http.NewRequestWithContext(ctx, method, client.baseURL+uri, buf)
63
	if err != nil {
64
		return nil, err
65
	}
66
67
	req.Header.Set("Content-Type", "application/json")
68
	req.Header.Set("Authorization", "Bearer "+client.secretKey)
69
70
	return req, nil
71
}
72
73
// addURLParams adds urls parameters to an *http.Request
74
func (client *Client) addURLParams(request *http.Request, params map[string]string) *http.Request {
75
	q := request.URL.Query()
76
	for key, value := range params {
77
		q.Add(key, value)
78
	}
79
	request.URL.RawQuery = q.Encode()
80
	return request
81
}
82
83
// do carries out an HTTP request and returns a Response
84
func (client *Client) do(req *http.Request) (*Response, error) {
85
	if req == nil {
86
		return nil, fmt.Errorf("%T cannot be nil", req)
87
	}
88
89
	httpResponse, err := client.httpClient.Do(req)
90
	if err != nil {
91
		return nil, err
92
	}
93
94
	defer func() { _ = httpResponse.Body.Close() }()
95
96
	resp, err := client.newResponse(httpResponse)
97
	if err != nil {
98
		return resp, err
99
	}
100
101
	_, err = io.Copy(ioutil.Discard, httpResponse.Body)
102
	if err != nil {
103
		return resp, err
104
	}
105
106
	return resp, nil
107
}
108
109
// newResponse converts an *http.Response to *Response
110
func (client *Client) newResponse(httpResponse *http.Response) (*Response, error) {
111
	if httpResponse == nil {
112
		return nil, fmt.Errorf("%T cannot be nil", httpResponse)
113
	}
114
115
	resp := new(Response)
116
	resp.HTTPResponse = httpResponse
117
118
	buf, err := ioutil.ReadAll(resp.HTTPResponse.Body)
119
	if err != nil {
120
		return nil, err
121
	}
122
	resp.Body = &buf
123
124
	return resp, resp.Error()
125
}
126