-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathclient.go
More file actions
185 lines (161 loc) · 4.13 KB
/
client.go
File metadata and controls
185 lines (161 loc) · 4.13 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright 2017 Prospect One https://prospectone.io/. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package perfops provides functionality to access Prospect One's PerfOps APIs.
package perfops
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"runtime"
)
// UserAgent is the header string used to identify this package.
const UserAgent = "PerfOps API Go Client"
const (
apiRoot = "https://api.perfops.net"
basePath = apiRoot
libVersion = "v1.0.0"
userAgent = UserAgent + "/" + libVersion + " (" + runtime.GOOS + "/" + runtime.GOARCH + ")"
)
type (
// Client defines the API client interface.
Client struct {
client *http.Client
common service
BasePath string // API endpoint base URL
UserAgent string // optional additional User-Agent fragment
apiKey string
DNS *DNSService
Run *RunService
}
service struct {
client *Client
}
clientError struct {
Code int
Text string
}
)
// Error returns
func (err clientError) Error() string {
if err.Text != "" {
return fmt.Sprintf("%d: %s", err.Code, err.Text)
}
return fmt.Sprintf("%d", err.Code)
}
// IsUnauthorized returns a value indicating whether the error is an
// authorization error.
func (err *clientError) IsUnauthorized() bool {
return err.Code == http.StatusUnauthorized
}
type isUnauthorizeder interface {
IsUnauthorized() bool
}
// IsUnauthorized returns a value indicating whether the error is an
// authorization error.
func IsUnauthorized(err error) bool {
if ce, ok := err.(isUnauthorizeder); ok {
return ce.IsUnauthorized()
}
return false
}
// WithAPIKey sets the API key for the API client.
func WithAPIKey(key string) func(c *Client) error {
return func(c *Client) error {
c.apiKey = key
return nil
}
}
// WithHTTPClient sets the HTTP client for the API client.
func WithHTTPClient(client *http.Client) func(c *Client) error {
return func(c *Client) error {
if client == nil {
return errors.New("HTTP client is nil")
}
c.client = client
return nil
}
}
// NewClient returns a new Client given an API key and options.
func NewClient(opts ...func(c *Client) error) (*Client, error) {
c := &Client{
client: http.DefaultClient,
BasePath: basePath,
}
c.common.client = c
for _, opt := range opts {
if err := opt(c); err != nil {
return nil, err
}
}
c.DNS = (*DNSService)(&c.common)
c.Run = (*RunService)(&c.common)
return c, nil
}
func (c *Client) DoRequest(req *http.Request, v interface{}) error {
if err := c.do(req, &v); err != nil {
return err
}
return nil
}
func (c *Client) userAgent() string {
if c.UserAgent == "" {
return userAgent
}
return c.UserAgent + " " + userAgent
}
func (c *Client) do(req *http.Request, v interface{}) error {
req.Header.Set("Authorization", c.apiKey)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", c.userAgent())
resp, err := c.client.Do(req)
if err != nil {
return err
}
defer closeBody(resp)
if resp.StatusCode >= http.StatusBadRequest {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
return &clientError{Code: resp.StatusCode, Text: string(b)}
}
if false {
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fmt.Println(string(b))
r := bytes.NewReader(b)
d := json.NewDecoder(r)
return d.Decode(&v)
}
d := json.NewDecoder(resp.Body)
return d.Decode(&v)
}
func newJSONReader(v interface{}) (io.Reader, error) {
buf := new(bytes.Buffer)
if err := json.NewEncoder(buf).Encode(v); err != nil {
return nil, err
}
return buf, nil
}
func closeBody(res *http.Response) {
if res == nil || res.Body == nil {
return
}
res.Body.Close()
}