aboutsummaryrefslogtreecommitdiffhomepage
path: root/vendor/github.com/DreamItGetIT/statuscake/client.go
blob: 6094be56756d0b42e825154d85bcb36271e1d826 (plain) (blame)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package statuscake

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"net/url"
	"strings"
)

const apiBaseURL = "https://app.statuscake.com/API"

type responseBody struct {
	io.Reader
}

func (r *responseBody) Close() error {
	return nil
}

// Auth wraps the authorisation headers required for each request
type Auth struct {
	Username string
	Apikey   string
}

func (a *Auth) validate() error {
	e := make(ValidationError)

	if a.Username == "" {
		e["Username"] = "is required"
	}

	if a.Apikey == "" {
		e["Apikey"] = "is required"
	}

	if len(e) > 0 {
		return e
	}

	return nil
}

type httpClient interface {
	Do(*http.Request) (*http.Response, error)
}

type apiClient interface {
	get(string, url.Values) (*http.Response, error)
	delete(string, url.Values) (*http.Response, error)
	put(string, url.Values) (*http.Response, error)
}

// Client is the http client that wraps the remote API.
type Client struct {
	c           httpClient
	username    string
	apiKey      string
	testsClient Tests
}

// New returns a new Client
func New(auth Auth) (*Client, error) {
	if err := auth.validate(); err != nil {
		return nil, err
	}

	return &Client{
		c:        &http.Client{},
		username: auth.Username,
		apiKey:   auth.Apikey,
	}, nil
}

func (c *Client) newRequest(method string, path string, v url.Values, body io.Reader) (*http.Request, error) {
	url := fmt.Sprintf("%s%s", apiBaseURL, path)
	if v != nil {
		url = fmt.Sprintf("%s?%s", url, v.Encode())
	}

	r, err := http.NewRequest(method, url, body)
	if err != nil {
		return nil, err
	}

	r.Header.Set("Username", c.username)
	r.Header.Set("API", c.apiKey)

	return r, nil
}

func (c *Client) doRequest(r *http.Request) (*http.Response, error) {
	resp, err := c.c.Do(r)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode < 200 || resp.StatusCode > 299 {
		return nil, &httpError{
			status:     resp.Status,
			statusCode: resp.StatusCode,
		}
	}

	var aer autheticationErrorResponse

	// We read and save the response body so that if we don't have error messages
	// we can set it again for future usage
	b, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(b, &aer)
	if err == nil && aer.ErrNo == 0 && aer.Error != "" {
		return nil, &AuthenticationError{
			errNo:   aer.ErrNo,
			message: aer.Error,
		}
	}

	resp.Body = &responseBody{
		Reader: bytes.NewReader(b),
	}

	return resp, nil
}

func (c *Client) get(path string, v url.Values) (*http.Response, error) {
	r, err := c.newRequest("GET", path, v, nil)
	if err != nil {
		return nil, err
	}

	return c.doRequest(r)
}

func (c *Client) put(path string, v url.Values) (*http.Response, error) {
	r, err := c.newRequest("PUT", path, nil, strings.NewReader(v.Encode()))
	r.Header.Set("Content-Type", "application/x-www-form-urlencoded")

	if err != nil {
		return nil, err
	}

	return c.doRequest(r)
}

func (c *Client) delete(path string, v url.Values) (*http.Response, error) {
	r, err := c.newRequest("DELETE", path, v, nil)
	if err != nil {
		return nil, err
	}

	return c.doRequest(r)
}

// Tests returns a client that implements the `Tests` API.
func (c *Client) Tests() Tests {
	if c.testsClient == nil {
		c.testsClient = newTests(c)
	}

	return c.testsClient
}