forked from uber-go/hackeroni
/
legacy.go
143 lines (119 loc) · 4.3 KB
/
legacy.go
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
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package legacy
// Imports
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
)
const (
libraryVersion = "1"
defaultBaseURL = "https://hackerone.com/"
userAgent = "uber/h1/v" + libraryVersion
)
// A Client manages communication with the H1.
type Client struct {
client *http.Client // HTTP client used to communicate.
// Base URL for requests. Defaults to the public H1. BaseURL should always be specified with a trailing slash.
BaseURL *url.URL
// User agent used when communicating with H1.
UserAgent string
common service // Reuse a single struct instead of allocating one for each service on the heap.
// Services used for talking to different parts of H1.
Session *SessionService
User *UserService
Team *TeamService
Report *ReportService
}
type service struct {
client *Client
}
// NewClient returns a new client. If a nil httpClient is provided, http.DefaultClient will be used.
func NewClient(httpClient *http.Client) *Client {
if httpClient == nil {
httpClient = http.DefaultClient
}
baseURL, _ := url.Parse(defaultBaseURL)
c := &Client{client: httpClient, BaseURL: baseURL, UserAgent: userAgent}
c.common.client = c
c.Session = (*SessionService)(&c.common)
c.User = (*UserService)(&c.common)
c.Team = (*TeamService)(&c.common)
c.Report = (*ReportService)(&c.common)
return c
}
// NewRequest creates an request. A relative URL can be provided in urlStr
func (c *Client) NewRequest(method, urlStr string, body io.Reader) (*http.Request, error) {
rel, err := url.Parse(urlStr)
if err != nil {
return nil, err
}
req, err := http.NewRequest(method, c.BaseURL.ResolveReference(rel).String(), body)
if err != nil {
return nil, err
}
req.Header.Add("User-Agent", c.UserAgent)
return req, nil
}
// Response is a H1 response. This wraps the standard http.Response and provides convenience fields for pagination
type Response struct {
*http.Response
}
// ErrorResponse wraps a http.Response and is returned when the API returns an error.
type ErrorResponse struct {
Response *http.Response // HTTP response that caused this error
}
// ErrorResponse needs to implement Error to be a valid error type.
func (r *ErrorResponse) Error() string {
return fmt.Sprintf("%v %v: %d", r.Response.Request.Method, r.Response.Request.URL, r.Response.StatusCode)
}
// CheckResponse determines if the given http.Response was an error and converts it to a h1.ErrorResponse if so
func CheckResponse(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
errorResponse := &ErrorResponse{Response: r}
return errorResponse
}
// Do sends an request and returns the response.
func (c *Client) Do(req *http.Request, resource interface{}) (*Response, error) {
// Actually do the request
resp, err := c.client.Do(req)
if err != nil {
return nil, err
}
// Make a response object
response := &Response{Response: resp}
// If request returned an error, return the response and err back to user to inspect
if err := CheckResponse(resp); err != nil {
return response, err
}
// Decode if a resource was provided
if resource != nil {
if err := json.NewDecoder(resp.Body).Decode(resource); err != nil {
return response, err
}
}
// Return success
return response, nil
}