This repository was archived by the owner on Jun 27, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest.go
211 lines (178 loc) · 6.21 KB
/
request.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
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
// Copyright 2013 Dobrosław Żybort
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package oauth2
import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"strings"
)
var _ = fmt.Printf
// ResRequest represents values needed to make authenticated HTTP requests.
type ResRequest struct {
// Base URL for API
apiBaseURL url.URL
// Access token added to each resource request
AccessToken string
// Header allows you to add custom headers that'll be added to each
// resource request
Header http.Header
// The OAuth 2.0 Authorization Framework: Bearer Token Usage
// http://tools.ietf.org/html/rfc6750
// Set AccessTokenInURL to true if destination service require
// authorization in the HTTP request URI
// GET /resource?access_token=<YOUR_ACCESS_TOKEN> HTTP/1.1
// Host: server.example.com
AccessTokenInURL bool
// Authentication URI parameter, default: "access_token"
AccessTokenInURLParam string
// Set AccessTokenInHeader to true if destination service require
// authorization in the "Authorization" request header
// GET /resource HTTP/1.1
// Host: server.example.com
// Authorization: Bearer <YOUR_ACCESS_TOKEN>
AccessTokenInHeader bool
// Authentication header scheme, default: "Bearer"
AccessTokenInHeaderScheme string
}
// Request initializes basic values that can be used to make
// authenticated HTTP requests.
func Request(apiBaseURL, accessToken string) *ResRequest {
req := new(ResRequest)
req.Header = make(http.Header)
req.AccessToken = accessToken
req.setApiBaseURL(apiBaseURL)
req.AccessTokenInURLParam = "access_token"
req.AccessTokenInHeaderScheme = "Bearer"
return req
}
// setApiBaseURL parse and update req.ApiBaseURL
func (req *ResRequest) setApiBaseURL(baseURL string) {
apiBaseURL, err := url.Parse(strings.TrimRight(baseURL, "/"))
if err != nil {
panic("ApiBaseURL error: " + err.Error())
}
req.apiBaseURL = *apiBaseURL
}
// ApiBaseURL update req.ApiBaseURL
func (req *ResRequest) ApiBaseURL(baseURL string) {
req.setApiBaseURL(baseURL)
}
// buildURL build full URL from req.apiBaseURL and endPoint
func (req *ResRequest) buildURL(endPoint string) string {
endPoint = strings.TrimLeft(endPoint, "/")
return req.apiBaseURL.String() + "/" + endPoint
}
// updateTokenInURL add access token to fullURL
// if req.AccessTokenInURL is set to true.
func (req *ResRequest) updateTokenInURL(fullURL string) (string, error) {
if req.AccessTokenInURL {
parsedURL, err := url.Parse(fullURL)
if err != nil {
return "", errors.New("Error updating token in URL")
}
params, _ := url.ParseQuery(parsedURL.RawQuery)
params.Set(req.AccessTokenInURLParam, req.AccessToken)
parsedURL.RawQuery = params.Encode()
fullURL = parsedURL.String()
}
return fullURL, nil
}
// updateTokenInHeader add access token to request header
// if req.AccessTokenInHeader is set to true.
func (req *ResRequest) updateTokenInHeader(request *http.Request) (
updatedRequest *http.Request) {
if req.AccessTokenInHeader {
authHeader := req.AccessTokenInHeaderScheme + " " + req.AccessToken
request.Header.Set("Authorization", authHeader)
}
return request
}
// Do updates HTTP request with access token. Next sends an HTTP request
// and returns an HTTP response
//func (req *Request) Do(req *http.Request) (
// resp *http.Response, err error) {
// return service.Client().Do(req)
//}
// Delete issues a DELETE to the specified API endpoint.
func (req *ResRequest) Delete(endPoint string) (
resp *http.Response, err error) {
return req.sendRequest("DELETE", endPoint, nil)
}
// Get issues a GET to the specified API endpoint.
func (req *ResRequest) Get(endPoint string) (
resp *http.Response, err error) {
return req.sendRequest("GET", endPoint, nil)
}
// Head issues a HEAD to the specified API endpoint.
func (req *ResRequest) Head(endPoint string) (
resp *http.Response, err error) {
return req.sendRequest("HEAD", endPoint, nil)
}
// Options issues a OPTIONS to the specified API endpoint.
func (req *ResRequest) Options(endPoint string) (
resp *http.Response, err error) {
return req.sendRequest("OPTIONS", endPoint, nil)
}
// Patch issues a PATCH to the specified API endpoint, with data's keys
// and values urlencoded as the request body.
func (req *ResRequest) Patch(endPoint string, data url.Values) (
resp *http.Response, err error) {
return req.sendRequest("PATCH", endPoint, data)
}
// Post issues a POST to the specified API endpoint, with data's keys
// and values urlencoded as the request body.
func (req *ResRequest) Post(endPoint string, data url.Values) (
resp *http.Response, err error) {
return req.sendRequest("POST", endPoint, data)
}
// Put issues a PUT to the specified API endpoint, with data's keys and values
// urlencoded as the request body.
func (req *ResRequest) Put(endPoint string, data url.Values) (
resp *http.Response, err error) {
return req.sendRequest("PUT", endPoint, data)
}
// Trace issues a TRACE to the specified API endpoint.
func (req *ResRequest) Trace(endPoint string) (
resp *http.Response, err error) {
return req.sendRequest("TRACE", endPoint, nil)
}
// sendRequest issues OAuth-authenticated request method to the specified
// API endpoint, with data's keys and values URL-encoded as the request body.
// Caller should close resp.Body when done reading from it.
func (req *ResRequest) sendRequest(method, endPoint string, data url.Values) (
resp *http.Response, err error) {
fullURL := req.buildURL(endPoint)
fullURL, err = req.updateTokenInURL(fullURL)
if err != nil {
return nil, err
}
var encData string
var body io.ReadCloser
if data != nil {
encData = data.Encode()
reader := strings.NewReader(encData)
body = ioutil.NopCloser(reader)
}
//fmt.Println(fullURL)
request, err := http.NewRequest(method, fullURL, body)
if err != nil {
return nil, errors.New("Error building request")
}
request.Header = req.Header
request = req.updateTokenInHeader(request)
if data != nil {
request.Header.Set("Content-Type", "application/x-www-form-urlencoded")
request.ContentLength = int64(len(encData))
}
//for key, val := range request.Header {
// fmt.Println(key, val)
//}
return http.DefaultClient.Do(request)
}