-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathClient.elm
More file actions
468 lines (383 loc) · 14.9 KB
/
Client.elm
File metadata and controls
468 lines (383 loc) · 14.9 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
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
-- Automatically generated file by swagger_to. DO NOT EDIT OR APPEND ANYTHING!
module Client
exposing
( Activities
, Activity
, PriceEstimate
, PriceEstimateArray
, Product
, ProductList
, ProductMap
, Profile
, decodeActivities
, decodeActivity
, decodePriceEstimate
, decodePriceEstimateArray
, decodeProduct
, decodeProductList
, decodeProductMap
, decodeProfile
, encodeActivities
, encodeActivity
, encodePriceEstimate
, encodePriceEstimateArray
, encodeProduct
, encodeProductList
, encodeProductMap
, encodeProfile
, estimatesPriceRequest
, estimatesTimeRequest
, historyRequest
, productsRequest
, updateMeRequest
)
import Dict exposing (Dict)
import Http
import Json.Decode
import Json.Decode.Pipeline
import Json.Encode
import Json.Encode.Extra
import Time
-- Models
type alias Product =
-- Unique identifier representing a specific product for a given latitude & longitude.
-- For example, uberX in San Francisco will have a different product_id than uberX in Los Angeles.
{ productID : String
-- Description of product.
, desc : String
-- Display name of product.
, displayName : String
-- Capacity of product. For example, 4 people.
, capacity : Int
-- Image URL representing the product.
, image : String
}
type alias ProductList =
-- Contains the list of products
{ products : List Product
}
type alias ProductMap =
Dict String Product
type alias PriceEstimate =
-- Unique identifier representing a specific product for a given latitude & longitude. For example,
-- uberX in San Francisco will have a different product_id than uberX in Los Angeles
{ productID : String
-- [ISO 4217](http://en.wikipedia.org/wiki/ISO_4217) currency code.
, currencyCode : String
-- Display name of product.
, displayName : String
-- Formatted string of estimate in local currency of the start location.
-- Estimate could be a range, a single number (flat rate) or "Metered" for TAXI.
, estimate : String
-- Lower bound of the estimated price.
, lowEstimate : Maybe (Float)
-- Upper bound of the estimated price.
, highEstimate : Maybe (Float)
-- Expected surge multiplier. Surge is active if surge_multiplier is greater than 1.
-- Price estimate already factors in the surge multiplier.
, surgeMultiplier : Maybe (Float)
}
type alias PriceEstimateArray =
List Product
type alias Profile =
-- First name of the Uber user.
{ firstName : Maybe (String)
-- Last name of the Uber user.
, lastName : String
-- Email address of the Uber user
, email : String
-- Image URL of the Uber user.
, picture : String
-- Promo code of the Uber user.
, promoCode : Maybe (String)
}
type alias Activity =
-- Unique identifier for the activity
{ uuid : String
}
type alias Activities =
-- Position in pagination.
{ offset : Int
-- Number of items to retrieve (100 max).
, limit : Int
-- Total number of items available.
, count : Int
, history : List Activity
}
-- Encoders
encodeProduct : Product -> Json.Encode.Value
encodeProduct aProduct =
Json.Encode.object
[ ( "product_id", Json.Encode.string <| aProduct.productID )
, ( "desc", Json.Encode.string <| aProduct.desc )
, ( "display_name", Json.Encode.string <| aProduct.displayName )
, ( "capacity", Json.Encode.int <| aProduct.capacity )
, ( "image", Json.Encode.string <| aProduct.image )
]
encodeProductList : ProductList -> Json.Encode.Value
encodeProductList aProductList =
Json.Encode.object
[ ( "products", Json.Encode.list <| List.map encodeProduct <| aProductList.products )
]
encodeProductMap : (Dict String Product) -> Json.Encode.Value
encodeProductMap aProductMap =
Json.Encode.Extra.dict identity encodeProduct <| aProductMap
encodePriceEstimate : PriceEstimate -> Json.Encode.Value
encodePriceEstimate aPriceEstimate =
Json.Encode.object
[ ( "product_id", Json.Encode.string <| aPriceEstimate.productID )
, ( "currency_code", Json.Encode.string <| aPriceEstimate.currencyCode )
, ( "display_name", Json.Encode.string <| aPriceEstimate.displayName )
, ( "estimate", Json.Encode.string <| aPriceEstimate.estimate )
, ( "low_estimate", Json.Encode.Extra.maybe (Json.Encode.float) <| aPriceEstimate.lowEstimate )
, ( "high_estimate", Json.Encode.Extra.maybe (Json.Encode.float) <| aPriceEstimate.highEstimate )
, ( "surge_multiplier", Json.Encode.Extra.maybe (Json.Encode.float) <| aPriceEstimate.surgeMultiplier )
]
encodePriceEstimateArray : (List Product) -> Json.Encode.Value
encodePriceEstimateArray aPriceEstimateArray =
Json.Encode.list <| List.map encodeProduct <| aPriceEstimateArray
encodeProfile : Profile -> Json.Encode.Value
encodeProfile aProfile =
Json.Encode.object
[ ( "first_name", Json.Encode.Extra.maybe (Json.Encode.string) <| aProfile.firstName )
, ( "last_name", Json.Encode.string <| aProfile.lastName )
, ( "email", Json.Encode.string <| aProfile.email )
, ( "picture", Json.Encode.string <| aProfile.picture )
, ( "promo_code", Json.Encode.Extra.maybe (Json.Encode.string) <| aProfile.promoCode )
]
encodeActivity : Activity -> Json.Encode.Value
encodeActivity aActivity =
Json.Encode.object
[ ( "uuid", Json.Encode.string <| aActivity.uuid )
]
encodeActivities : Activities -> Json.Encode.Value
encodeActivities aActivities =
Json.Encode.object
[ ( "offset", Json.Encode.int <| aActivities.offset )
, ( "limit", Json.Encode.int <| aActivities.limit )
, ( "count", Json.Encode.int <| aActivities.count )
, ( "history", Json.Encode.list <| List.map encodeActivity <| aActivities.history )
]
-- Decoders
decodeProduct : Json.Decode.Decoder Product
decodeProduct =
Json.Decode.Pipeline.decode Product
|> Json.Decode.Pipeline.required "product_id" Json.Decode.string
|> Json.Decode.Pipeline.required "desc" Json.Decode.string
|> Json.Decode.Pipeline.required "display_name" Json.Decode.string
|> Json.Decode.Pipeline.required "capacity" Json.Decode.int
|> Json.Decode.Pipeline.required "image" Json.Decode.string
decodeProductList : Json.Decode.Decoder ProductList
decodeProductList =
Json.Decode.Pipeline.decode ProductList
|> Json.Decode.Pipeline.required "products" (Json.Decode.list <| decodeProduct)
decodeProductMap : Json.Decode.Decoder (Dict String Product)
decodeProductMap =
Json.Decode.dict <| decodeProduct
decodePriceEstimate : Json.Decode.Decoder PriceEstimate
decodePriceEstimate =
Json.Decode.Pipeline.decode PriceEstimate
|> Json.Decode.Pipeline.required "product_id" Json.Decode.string
|> Json.Decode.Pipeline.required "currency_code" Json.Decode.string
|> Json.Decode.Pipeline.required "display_name" Json.Decode.string
|> Json.Decode.Pipeline.required "estimate" Json.Decode.string
|> Json.Decode.Pipeline.optional "low_estimate" (Json.Decode.nullable Json.Decode.float) Nothing
|> Json.Decode.Pipeline.optional "high_estimate" (Json.Decode.nullable Json.Decode.float) Nothing
|> Json.Decode.Pipeline.optional "surge_multiplier" (Json.Decode.nullable Json.Decode.float) Nothing
decodePriceEstimateArray : Json.Decode.Decoder (List Product)
decodePriceEstimateArray =
Json.Decode.list <| decodeProduct
decodeProfile : Json.Decode.Decoder Profile
decodeProfile =
Json.Decode.Pipeline.decode Profile
|> Json.Decode.Pipeline.optional "first_name" (Json.Decode.nullable Json.Decode.string) Nothing
|> Json.Decode.Pipeline.required "last_name" Json.Decode.string
|> Json.Decode.Pipeline.required "email" Json.Decode.string
|> Json.Decode.Pipeline.required "picture" Json.Decode.string
|> Json.Decode.Pipeline.optional "promo_code" (Json.Decode.nullable Json.Decode.string) Nothing
decodeActivity : Json.Decode.Decoder Activity
decodeActivity =
Json.Decode.Pipeline.decode Activity
|> Json.Decode.Pipeline.required "uuid" Json.Decode.string
decodeActivities : Json.Decode.Decoder Activities
decodeActivities =
Json.Decode.Pipeline.decode Activities
|> Json.Decode.Pipeline.required "offset" Json.Decode.int
|> Json.Decode.Pipeline.required "limit" Json.Decode.int
|> Json.Decode.Pipeline.required "count" Json.Decode.int
|> Json.Decode.Pipeline.required "history" (Json.Decode.list <| decodeActivity)
-- Remote Calls
{-| Contains a "get" request to the endpoint: `/products`, to be sent with Http.send
The Products endpoint returns information about the Uber products offered at a given location.
-}
productsRequest : String -> Maybe Time.Time -> Bool -> Float -> Float -> Http.Request (Dict String Product)
productsRequest prefix maybeTimeout withCredentials latitude longitude =
let
baseUrl = prefix ++ "products"
queryString =
paramsToQuery
[ ("latitude", (toString latitude))
, ("longitude", (toString longitude))
]
[]
url = baseUrl ++ queryString
in
Http.request
{ body = Http.emptyBody
, expect = Http.expectJson (Json.Decode.dict <| decodeProduct)
, headers = []
, method = "GET"
, timeout = maybeTimeout
, url = url
, withCredentials = withCredentials
}
{-| Contains a "get" request to the endpoint: `/estimates/price/{start_latitude}/{start_longitude}/{end_latitude}/{end_longitude}`, to be sent with Http.send
The Price Estimates endpoint returns an estimated price range for each product offered at a given
location. The price estimate is provided as a formatted string with the full price range and the localized
currency symbol.
-}
estimatesPriceRequest :
String
-> Maybe Time.Time
-> Bool
-> Float
-> Float
-> Float
-> Float
-> Maybe Int
-> Http.Request (List Product)
estimatesPriceRequest
prefix
maybeTimeout
withCredentials
startLatitude
startLongitude
endLatitude
endLongitude
maybeMaxLines =
let
baseUrl = prefix
++ "estimates/price/"
++ (toString startLatitude)
++ "/"
++ (toString startLongitude)
++ "/"
++ (toString endLatitude)
++ "/"
++ (toString endLongitude)
queryString =
paramsToQuery
[]
[ ("max_lines", (Maybe.map toString maybeMaxLines))
]
url = baseUrl ++ queryString
in
Http.request
{ body = Http.emptyBody
, expect = Http.expectJson (Json.Decode.list <| decodeProduct)
, headers = []
, method = "GET"
, timeout = maybeTimeout
, url = url
, withCredentials = withCredentials
}
{-| Contains a "get" request to the endpoint: `/estimates/time`, to be sent with Http.send
The Time Estimates endpoint returns ETAs for all products.
-}
estimatesTimeRequest :
String
-> Maybe Time.Time
-> Bool
-> Float
-> Float
-> Maybe String
-> Maybe String
-> Http.Request (Dict String Product)
estimatesTimeRequest prefix maybeTimeout withCredentials startLatitude startLongitude maybeCustomerUuid maybeProductID =
let
baseUrl = prefix ++ "estimates/time"
queryString =
paramsToQuery
[ ("start_latitude", (toString startLatitude))
, ("start_longitude", (toString startLongitude))
]
[ ("customer_uuid", maybeCustomerUuid)
, ("product_id", maybeProductID)
]
url = baseUrl ++ queryString
in
Http.request
{ body = Http.emptyBody
, expect = Http.expectJson (Json.Decode.dict <| decodeProduct)
, headers = []
, method = "GET"
, timeout = maybeTimeout
, url = url
, withCredentials = withCredentials
}
{-| Contains a "patch" request to the endpoint: `/me`, to be sent with Http.send
Update an User Profile.
-}
updateMeRequest : String -> Maybe Time.Time -> Bool -> Profile -> Http.Request Profile
updateMeRequest prefix maybeTimeout withCredentials updateUser =
let
url = prefix ++ "me"
in
Http.request
{ body = (encodeProfile updateUser) |> Http.jsonBody
, expect = Http.expectJson decodeProfile
, headers = []
, method = "PATCH"
, timeout = maybeTimeout
, url = url
, withCredentials = withCredentials
}
{-| Contains a "get" request to the endpoint: `/history`, to be sent with Http.send
The User Activity endpoint returns data about a user's lifetime activity with Uber. The response will
include pickup locations and times, dropoff locations and times, the distance of past requests, and
information about which products were requested.
-}
historyRequest : String -> Maybe Time.Time -> Bool -> Maybe Int -> Maybe Int -> Http.Request Activities
historyRequest prefix maybeTimeout withCredentials maybeOffset maybeLimit =
let
baseUrl = prefix ++ "history"
queryString =
paramsToQuery
[]
[ ("offset", (Maybe.map toString maybeOffset))
, ("limit", (Maybe.map toString maybeLimit))
]
url = baseUrl ++ queryString
in
Http.request
{ body = Http.emptyBody
, expect = Http.expectJson decodeActivities
, headers = []
, method = "GET"
, timeout = maybeTimeout
, url = url
, withCredentials = withCredentials
}
{-| Translates a list of (name, parameter) and a list of (name, optional parameter) to a
well-formatted query string.
-}
paramsToQuery : List ( String, String ) -> List ( String, Maybe String ) -> String
paramsToQuery params maybeParams =
let
queryParams : List String
queryParams =
List.map (\( name, value ) -> name ++ "=" ++ Http.encodeUri value) params
filteredParams : List String
filteredParams =
List.filter (\( _, maybeValue ) -> maybeValue /= Nothing) maybeParams
|> List.map (\( name, maybeValue ) -> ( name, Maybe.withDefault "" maybeValue ))
|> List.map (\( name, value ) -> name ++ "=" ++ Http.encodeUri value)
in
List.concat [queryParams, filteredParams]
|> String.join "&"
|> \str ->
if String.isEmpty str then
""
else
"?" ++ str
-- Automatically generated file by swagger_to. DO NOT EDIT OR APPEND ANYTHING!