@@ -54,6 +54,70 @@ const (
54
54
defaultThrottleDuration = 150 * time .Millisecond
55
55
)
56
56
57
+ type DriverInfoResponse struct {
58
+ Cancel func ()
59
+ Pages <- chan * DriverInfoPage
60
+ }
61
+
62
+ type driverInfoWrap struct {
63
+ Count int `json:"count"`
64
+ Limit int `json:"limit"`
65
+ Offset int `json:"offset"`
66
+ Payments []* Payment `json:"payments"`
67
+ Trips []* Trip `json:"trips"`
68
+ }
69
+
70
+ func (dpq * DriverInfoQuery ) toRealDriverQuery () * realDriverQuery {
71
+ rdpq := & realDriverQuery {
72
+ Offset : dpq .Offset ,
73
+ }
74
+ if dpq .StartDate != nil {
75
+ rdpq .StartTimeUnix = dpq .StartDate .Unix ()
76
+ }
77
+ if dpq .EndDate != nil {
78
+ rdpq .EndTimeUnix = dpq .EndDate .Unix ()
79
+ }
80
+ return rdpq
81
+ }
82
+
83
+ // realDriverQuery because it is the 1-to-1 match
84
+ // with the fields sent it to query for the payments.
85
+ // DriverQuery is just there for convenience and
86
+ // easy API usage from callers e.g passing in a date without
87
+ // having to worry about its exact Unix timestamp.
88
+ type realDriverQuery struct {
89
+ Offset int `json:"offset,omitempty"`
90
+ LimitPerPage int `json:"limit,omitempty"`
91
+ StartTimeUnix int64 `json:"from_time,omitempty"`
92
+ EndTimeUnix int64 `json:"to_time,omitempty"`
93
+ }
94
+
95
+ type DriverInfoQuery struct {
96
+ Offset int `json:"offset,omitempty"`
97
+
98
+ // LimitPerPage is the number of items to retrieve per page.
99
+ // Default is 5, maximum is 50.
100
+ LimitPerPage int `json:"limit,omitempty"`
101
+
102
+ StartDate * time.Time `json:"start_date,omitempty"`
103
+ EndDate * time.Time `json:"end_date,omitempty"`
104
+
105
+ MaxPageNumber int `json:"max_page_number,omitempty"`
106
+
107
+ Throttle time.Duration `json:"throttle,omitempty"`
108
+ }
109
+
110
+ type DriverInfoPage struct {
111
+ PageNumber int `json:"page_number,omitempty"`
112
+ Payments []* Payment `json:"payments,omitempty"`
113
+ Trips []* Trip `json:"trips,omitempty"`
114
+ Err error `json:"error"`
115
+ }
116
+
117
+ func (c * Client ) ListDriverTrips (dpq * DriverInfoQuery ) (* DriverInfoResponse , error ) {
118
+ return c .listDriverInfo (dpq , "/partners/trips" )
119
+ }
120
+
57
121
// DriverPayments returns the payments for the given driver.
58
122
// Payments are available at this endpoint in near real-time. Some entries,
59
123
// such as "device_subscription" will appear on a periodic basis when actually
@@ -63,9 +127,13 @@ const (
63
127
// there will be no payments associated and the response will always be an empty
64
128
// array. Drivers working for fleet managers will receive payments from the fleet
65
129
// manager and not from Uber.
66
- func (c * Client ) ListDriverPayments (dpq * DriverPaymentsQuery ) (* DriverPaymentsResponse , error ) {
130
+ func (c * Client ) ListDriverPayments (dpq * DriverInfoQuery ) (* DriverInfoResponse , error ) {
131
+ return c .listDriverInfo (dpq , "/partners/payments" )
132
+ }
133
+
134
+ func (c * Client ) listDriverInfo (dpq * DriverInfoQuery , path string ) (* DriverInfoResponse , error ) {
67
135
if dpq == nil {
68
- dpq = new (DriverPaymentsQuery )
136
+ dpq = new (DriverInfoQuery )
69
137
}
70
138
71
139
throttleDuration := dpq .Throttle
@@ -80,22 +148,22 @@ func (c *Client) ListDriverPayments(dpq *DriverPaymentsQuery) (*DriverPaymentsRe
80
148
return maxPageNumber > 0 && pageNumber >= maxPageNumber
81
149
}
82
150
83
- baseURL := fmt .Sprintf ("%s/partners/payments " , c .baseURL (driverV1API ))
84
- rdpq := dpq .toRealDriverPaymentsQuery ()
151
+ baseURL := fmt .Sprintf ("%s%s " , c .baseURL (driverV1API ), path )
152
+ rdpq := dpq .toRealDriverQuery ()
85
153
limitPerPage := rdpq .LimitPerPage
86
154
if limitPerPage <= 0 {
87
155
limitPerPage = defaultDriverPaymentsLimitPerPage
88
156
}
89
157
90
158
cancelChan , cancelFn := makeCancelParadigm ()
91
- resChan := make (chan * DriverPaymentsPage )
159
+ resChan := make (chan * DriverInfoPage )
92
160
go func () {
93
161
defer close (resChan )
94
162
95
163
pageNumber := 0
96
164
97
165
for {
98
- curPage := new (DriverPaymentsPage )
166
+ curPage := new (DriverInfoPage )
99
167
curPage .PageNumber = pageNumber
100
168
101
169
qv , err := otils .ToURLValues (rdpq )
@@ -123,17 +191,21 @@ func (c *Client) ListDriverPayments(dpq *DriverPaymentsQuery) (*DriverPaymentsRe
123
191
return
124
192
}
125
193
126
- recv := new (driverPaymentsWrap )
194
+ recv := new (driverInfoWrap )
127
195
if err := json .Unmarshal (blob , recv ); err != nil {
128
196
curPage .Err = err
129
197
resChan <- curPage
130
198
return
131
199
}
132
- if len (recv .Payments ) == 0 { // No payments sent back, so sign that we are at the end
200
+
201
+ // No payments nor trips sent back, so a sign that we are at the end
202
+ if len (recv .Payments ) == 0 && len (recv .Trips ) == 0 {
133
203
return
134
204
}
135
205
206
+ curPage .Trips = recv .Trips
136
207
curPage .Payments = recv .Payments
208
+
137
209
resChan <- curPage
138
210
139
211
pageNumber += 1
@@ -151,68 +223,10 @@ func (c *Client) ListDriverPayments(dpq *DriverPaymentsQuery) (*DriverPaymentsRe
151
223
}
152
224
}()
153
225
154
- resp := & DriverPaymentsResponse {
226
+ resp := & DriverInfoResponse {
155
227
Cancel : cancelFn ,
156
228
Pages : resChan ,
157
229
}
158
230
159
231
return resp , nil
160
232
}
161
-
162
- type DriverPaymentsResponse struct {
163
- Cancel func ()
164
- Pages <- chan * DriverPaymentsPage
165
- }
166
-
167
- type driverPaymentsWrap struct {
168
- Count int `json:"count"`
169
- Limit int `json:"limit"`
170
- Offset int `json:"offset"`
171
- Payments []* Payment `json:"payments"`
172
- }
173
-
174
- func (dpq * DriverPaymentsQuery ) toRealDriverPaymentsQuery () * realDriverPaymentsQuery {
175
- rdpq := & realDriverPaymentsQuery {
176
- Offset : dpq .Offset ,
177
- }
178
- if dpq .StartDate != nil {
179
- rdpq .StartTimeUnix = dpq .StartDate .Unix ()
180
- }
181
- if dpq .EndDate != nil {
182
- rdpq .EndTimeUnix = dpq .EndDate .Unix ()
183
- }
184
- return rdpq
185
- }
186
-
187
- // realDriverPaymentsQuery because it is the 1-to-1 match
188
- // with the fields sent it to query for the payments.
189
- // DriverPaymentsQuery is just there for convenience and
190
- // easy API usage from callers e.g passing in a date without
191
- // having to worry about its exact Unix timestamp.
192
- type realDriverPaymentsQuery struct {
193
- Offset int `json:"offset,omitempty"`
194
- LimitPerPage int `json:"limit,omitempty"`
195
- StartTimeUnix int64 `json:"from_time,omitempty"`
196
- EndTimeUnix int64 `json:"to_time,omitempty"`
197
- }
198
-
199
- type DriverPaymentsQuery struct {
200
- Offset int `json:"offset,omitempty"`
201
-
202
- // LimitPerPage is the number of items to retrieve per page.
203
- // Default is 5, maximum is 50.
204
- LimitPerPage int `json:"limit,omitempty"`
205
-
206
- StartDate * time.Time `json:"start_date,omitempty"`
207
- EndDate * time.Time `json:"end_date,omitempty"`
208
-
209
- MaxPageNumber int `json:"max_page_number,omitempty"`
210
-
211
- Throttle time.Duration `json:"throttle,omitempty"`
212
- }
213
-
214
- type DriverPaymentsPage struct {
215
- PageNumber int `json:"page_number,omitempty"`
216
- Payments []* Payment `json:"payments,omitempty"`
217
- Err error `json:"error"`
218
- }
0 commit comments