-
Notifications
You must be signed in to change notification settings - Fork 0
/
uber.go
205 lines (170 loc) · 5.07 KB
/
uber.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
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)
const (
UberAuthHost = "https://login.uber.com"
)
type UberApiClient struct {
clientSecret string
clientId string
url string
}
type UberTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn uint32 `json:"expires_in"`
RefreshToken string `json:"refresh_token"`
Scope string `json:"scope"`
}
type UberHistoryResponse struct {
Offset int64 `json:"offset"`
Limit int64 `json:"limit"`
Count int64 `json:"count"`
History []UberHistoryItem `json:"history"`
}
type UberHistoryItem struct {
Status string `json:"status"`
Distance float64 `json:"distance"`
RequestTime int64 `json:"request_time"`
StartTime int64 `json:"start_time"`
StartCity UberHistoryCity `json:"start_city"`
EndTime int64 `json:"end_time"`
RequestId string `json:"request_id"`
ProductId string `json:"product_id"`
}
type UberHistoryCity struct {
Latitude float64 `json:"latitude"`
DisplayName string `json:"display_name"`
Longitude float64 `json:"longitude"`
}
type UberReceiptResponse struct {
RequestId string `json:"request_id"`
TotalCharged string `json:"total_charged"`
Distance string `json:"distance"`
DistanceLabel string `json:"miles"`
}
type UberRequestResponse struct {
Status string `json:"status"`
Location UberRequestLocation `json:"location"`
}
type UberRequestLocation struct {
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
}
func (c *UberApiClient) GetOAuthToken(authorizationCode, redirectUri string) (*UberTokenResponse, error) {
uberTokenUrl := fmt.Sprintf("%s/oauth/token", UberAuthHost)
formValues := url.Values{
"client_secret": {c.clientSecret},
"client_id": {c.clientId},
"grant_type": {"authorization_code"},
"redirect_uri": {redirectUri},
"code": {authorizationCode},
}
log.Printf("%s requesting %s\n", SetAuthCode, uberTokenUrl)
httpResponse, err := http.PostForm(uberTokenUrl, formValues)
if err != nil {
log.Printf("/login uber authorize error: %s", err.Error())
return nil, err
}
defer httpResponse.Body.Close()
if httpResponse.StatusCode != 200 {
if err != nil {
log.Printf("%s Uber OAuth token response error: %s", Login, err.Error())
return nil, err
}
body, err := ioutil.ReadAll(httpResponse.Body)
if err != nil {
return nil, err
}
return nil, errors.New(string(body))
}
uberTokenResponse := &UberTokenResponse{}
err = json.NewDecoder(httpResponse.Body).Decode(uberTokenResponse)
return uberTokenResponse, err
}
func (c *UberApiClient) GetHistory(accessToken string) (*UberHistoryResponse, error) {
// Hide cancelled & giving transactions for demo...
uberHistoryUrl := fmt.Sprintf("%s/v1.2/history?offset=3", c.url)
request, err := http.NewRequest("GET", uberHistoryUrl, nil)
if err != nil {
return nil, err
}
request.Header.Add(Authorization, Bearer+accessToken)
response, err := httpClient.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return nil, errors.New(string(body))
}
uberHistoryResponse := &UberHistoryResponse{}
err = json.NewDecoder(response.Body).Decode(uberHistoryResponse)
if err != nil {
return nil, err
}
return uberHistoryResponse, nil
}
func (c *UberApiClient) GetReceipt(accessToken, requestId string) (*UberReceiptResponse, error) {
uberHistoryUrl := fmt.Sprintf("%s/v1/requests/%s/receipt", c.url, requestId)
request, err := http.NewRequest("GET", uberHistoryUrl, nil)
if err != nil {
return nil, err
}
request.Header.Add(Authorization, Bearer+accessToken)
response, err := httpClient.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return nil, errors.New(string(body))
}
uberReceiptResponse := &UberReceiptResponse{}
err = json.NewDecoder(response.Body).Decode(uberReceiptResponse)
if err != nil {
return nil, err
}
return uberReceiptResponse, nil
}
func (c *UberApiClient) GetRequest(accessToken, requestId string) (*UberRequestResponse, error) {
uberHistoryUrl := fmt.Sprintf("%s/v1/requests/%s", c.url, requestId)
request, err := http.NewRequest("GET", uberHistoryUrl, nil)
if err != nil {
return nil, err
}
request.Header.Add(Authorization, Bearer+accessToken)
response, err := httpClient.Do(request)
if err != nil {
return nil, err
}
if response.StatusCode != 200 {
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
if err != nil {
return nil, err
}
return nil, errors.New(string(body))
}
uberRequestResponse := &UberRequestResponse{}
err = json.NewDecoder(response.Body).Decode(uberRequestResponse)
if err != nil {
return nil, err
}
return uberRequestResponse, nil
}