Test Setup Failed
Branch main (c228ed)
by Acho
01:53
created

mtnmomo.*Client.addURLParams   A

Complexity

Conditions 2

Size

Total Lines 7
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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