smsto.*Client.newResponse   A
last analyzed

Complexity

Conditions 3

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
dl 0
loc 15
c 0
b 0
f 0
rs 9.9
nop 1
1
package smsto
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
	baseURL    string
23
	apiKey     string
24
25
	SMS *smsService
26
}
27
28
// New creates and returns a new campay.Client from a slice of campay.ClientOption.
29
func New(options ...Option) *Client {
30
	config := defaultClientConfig()
31
32
	for _, option := range options {
33
		option.apply(config)
34
	}
35
36
	client := &Client{
37
		httpClient: config.httpClient,
38
		apiKey:     config.apiKey,
39
		baseURL:    config.baseURL,
40
	}
41
42
	client.common.client = client
43
	client.SMS = (*smsService)(&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("Accept", "application/json")
69
	req.Header.Set("Authorization", "Bearer "+client.apiKey)
70
71
	return req, nil
72
}
73
74
// do carries out an HTTP request and returns a Response
75
func (client *Client) do(req *http.Request) (*Response, error) {
76
	if req == nil {
77
		return nil, fmt.Errorf("%T cannot be nil", req)
78
	}
79
80
	httpResponse, err := client.httpClient.Do(req)
81
	if err != nil {
82
		return nil, err
83
	}
84
85
	defer func() { _ = httpResponse.Body.Close() }()
86
87
	resp, err := client.newResponse(httpResponse)
88
	if err != nil {
89
		return resp, err
90
	}
91
92
	_, err = io.Copy(ioutil.Discard, httpResponse.Body)
93
	if err != nil {
94
		return resp, err
95
	}
96
97
	return resp, nil
98
}
99
100
// newResponse converts an *http.Response to *Response
101
func (client *Client) newResponse(httpResponse *http.Response) (*Response, error) {
102
	if httpResponse == nil {
103
		return nil, fmt.Errorf("%T cannot be nil", httpResponse)
104
	}
105
106
	resp := new(Response)
107
	resp.HTTPResponse = httpResponse
108
109
	buf, err := ioutil.ReadAll(resp.HTTPResponse.Body)
110
	if err != nil {
111
		return nil, err
112
	}
113
	resp.Body = &buf
114
115
	return resp, resp.Error()
116
}
117