/
httprouter.go
721 lines (647 loc) · 21.6 KB
/
httprouter.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
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
package main
import (
"assignment3/permutation"
"bytes"
"encoding/json"
"fmt"
"github.com/julienschmidt/httprouter"
"gopkg.in/mgo.v2"
"gopkg.in/mgo.v2/bson"
"io/ioutil"
"math/rand"
"net/http"
"strconv"
"strings"
)
var id int
var hmap map[int]DataStorage
type Request struct {
Name string `json:"name"`
Address string `json:"address"`
City string `json:"city"`
State string `json:"state"`
Zip string `json:"zip"`
}
type Response struct {
Id bson.ObjectId `json:"id" bson:"_id"`
Name string `json:"name" bson:"name"`
Address string `json:"address" bson:"address"`
City string `json:"city" bson:"city"`
State string `json:"state" bson:"state"`
Zip string `json:"zip" bson:"zip"`
Coordinate struct {
Lat float64 `json:"lat" bson:"lat"`
Lng float64 `json:"lng" bson:"lng"`
} `json:"coordinate" bson:"coordinate"`
}
type MyJsonName struct {
Results []struct {
AddressComponents []struct {
LongName string `json:"long_name"`
ShortName string `json:"short_name"`
Types []string `json:"types"`
} `json:"address_components"`
FormattedAddress string `json:"formatted_address"`
Geometry struct {
Location struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"location"`
LocationType string `json:"location_type"`
Viewport struct {
Northeast struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"northeast"`
Southwest struct {
Lat float64 `json:"lat"`
Lng float64 `json:"lng"`
} `json:"southwest"`
} `json:"viewport"`
} `json:"geometry"`
PlaceID string `json:"place_id"`
Types []string `json:"types"`
} `json:"results"`
Status string `json:"status"`
}
type UberAPIResponse struct {
Prices []struct {
CurrencyCode string `json:"currency_code"`
DisplayName string `json:"display_name"`
Distance float64 `json:"distance"`
Duration int `json:"duration"`
Estimate string `json:"estimate"`
HighEstimate int `json:"high_estimate"`
LowEstimate int `json:"low_estimate"`
ProductID string `json:"product_id"`
SurgeMultiplier int `json:"surge_multiplier"`
} `json:"prices"`
}
type TripRequest struct {
Starting_from_location_id bson.ObjectId
Location_ids []bson.ObjectId
}
type TripResponse struct {
Id int
Status string
Starting_from_location_id bson.ObjectId
Best_route_location_id []bson.ObjectId
Total_uber_costs int
Total_uber_duration int
Total_distance float64
}
type DataStorage struct {
Id int
// Product_id string
Index int
Status string
Starting_from_location_id bson.ObjectId
Best_route_location_id []bson.ObjectId
Total_uber_costs int
Total_uber_duration int
Total_distance float64
}
type UberRequestResponse struct {
RequestID string `json:"request_id"`
Status string `json:"status"`
Vehicle string `json:"vehicle"`
Driver string `json:"driver"`
Location string `json:"location"`
ETA int `json:"eta"`
SurgeMultiplier float64 `json:"surge_multiplier"`
}
type CarResponse struct {
Id int
Status string
Starting_from_location_id bson.ObjectId
Next_destination_location_id bson.ObjectId
Best_route_location_id []bson.ObjectId
Total_uber_costs int
Total_uber_duration int
Total_distance float64
Uber_wait_time_eta int
}
type UserRequest struct {
Product_id string `json:"product_id"`
Start_latitude float64 `json:"start_latitude"`
Start_longitude float64 `json:"start_longitude"`
End_latitude float64 `json:"end_latitude"`
End_longitude float64 `json:"end_longitude"`
}
type UberResponse struct {
Driver interface{} `json:"driver"`
Eta int `json:"eta"`
Location interface{} `json:"location"`
RequestID string `json:"request_id"`
Status string `json:"status"`
SurgeMultiplier float64 `json:"surge_multiplier"`
Vehicle interface{} `json:"vehicle"`
}
var session *mgo.Session
func Create(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
var userInfo Request
json.NewDecoder(req.Body).Decode(&userInfo)
response := Response{}
response.Id = bson.NewObjectId()
response.Name = userInfo.Name
response.Address = userInfo.Address
response.City = userInfo.City
response.State = userInfo.State
response.Zip = userInfo.Zip
requestURL := createURL(response.Address, response.City, response.State)
getLocation(&response, requestURL)
session.DB("cmpe273").C("assignment2").Insert(response)
location, _ := json.Marshal(response)
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(201)
fmt.Fprintf(rw, "%s", location)
}
func CreateTrip(rw http.ResponseWriter, req *http.Request, p httprouter.Params) {
var tripReq TripRequest
// var coordinateData [4]float64
json.NewDecoder(req.Body).Decode(&tripReq)
// fmt.Println(tripReq)
// fmt.Println(len(tripReq.Location_ids))
dataStorage := DataStorage{}
tripRes := TripResponse{}
tripRes.Id = getID()
tripRes.Status = "planning"
tripRes.Starting_from_location_id = tripReq.Starting_from_location_id
tripRes.Best_route_location_id = tripReq.Location_ids
getBestRoute(&tripRes, &dataStorage, tripReq.Starting_from_location_id, tripReq.Location_ids)
//bestRouteLocation(&tripRes, &dataStorage, tripReq.Starting_from_location_id, tripReq.Location_ids)
hmap[tripRes.Id] = dataStorage
trip, _ := json.Marshal(tripRes)
rw.Header().Set("Content-Type", "application/json")
rw.WriteHeader(201)
fmt.Fprintf(rw, "%s", trip)
}
func Get(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
if !bson.IsObjectIdHex(id) {
w.WriteHeader(404)
return
}
userId := bson.ObjectIdHex(id)
response := Response{}
if err := session.DB("cmpe273").C("assignment2").FindId(userId).One(&response); err != nil {
w.WriteHeader(404)
return
}
location, _ := json.Marshal(response)
w.WriteHeader(200)
fmt.Fprintf(w, "%s", location)
}
func GetTrip(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
tId := p.ByName("trip_id")
checkID, _ := strconv.Atoi(tId)
var dataStorage DataStorage
findTarget := false
for key, value := range hmap {
if key == checkID {
dataStorage = value
findTarget = true
}
}
if findTarget == false {
w.WriteHeader(404)
return
}
tripRes := TripResponse{}
tripRes.Id = dataStorage.Id
tripRes.Status = dataStorage.Status
tripRes.Starting_from_location_id = dataStorage.Starting_from_location_id
tripRes.Best_route_location_id = dataStorage.Best_route_location_id
tripRes.Total_uber_costs = dataStorage.Total_uber_costs
tripRes.Total_distance = dataStorage.Total_distance
tripRes.Total_uber_duration = dataStorage.Total_uber_duration
trip, _ := json.Marshal(tripRes)
w.WriteHeader(200)
fmt.Fprintf(w, "%s", trip)
}
func Update(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
if !bson.IsObjectIdHex(id) {
w.WriteHeader(404)
return
}
var userInfo Request
json.NewDecoder(r.Body).Decode(&userInfo)
response := Response{}
response.Id = bson.ObjectIdHex(id)
response.Name = userInfo.Name
response.Address = userInfo.Address
response.City = userInfo.City
response.State = userInfo.State
response.Zip = userInfo.Zip
requestURL := createURL(response.Address, response.City, response.State)
getLocation(&response, requestURL)
if err := session.DB("cmpe273").C("assignment2").Update(bson.M{"_id": response.Id}, bson.M{"$set": bson.M{"address": response.Address, "city": response.City, "state": response.State, "zip": response.Zip, "coordinate.lat": response.Coordinate.Lat, "coordinate.lng": response.Coordinate.Lng}}); err != nil {
w.WriteHeader(404)
return
}
if err := session.DB("cmpe273").C("assignment2").FindId(response.Id).One(&response); err != nil {
w.WriteHeader(404)
return
}
location, _ := json.Marshal(response)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(201)
fmt.Fprintf(w, "%s", location)
}
func CarRequest(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
tId := p.ByName("trip_id")
checkID, _ := strconv.Atoi(tId)
var dataStorage DataStorage
findTarget := false
for key, value := range hmap {
if key == checkID {
dataStorage = value
findTarget = true
}
}
if findTarget == false {
w.WriteHeader(404)
return
}
var startLat float64
var startLng float64
var endLat float64
var endLng float64
carRes := CarResponse{}
response := Response{}
if dataStorage.Index == 0 {
if err := session.DB("cmpe273").C("assignment2").FindId(dataStorage.Starting_from_location_id).One(&response); err != nil {
// w.WriteHeader(404)
return
}
startLat = response.Coordinate.Lat
startLng = response.Coordinate.Lng
if err := session.DB("cmpe273").C("assignment2").FindId(dataStorage.Best_route_location_id[0]).One(&response); err != nil {
// w.WriteHeader(404)
return
}
endLat = response.Coordinate.Lat
endLng = response.Coordinate.Lng
uberAPI(&carRes, dataStorage, startLat, startLng, endLat, endLng)
carRes.Status = "requesting"
carRes.Starting_from_location_id = dataStorage.Starting_from_location_id
carRes.Next_destination_location_id = dataStorage.Best_route_location_id[0]
} else if dataStorage.Index == len(dataStorage.Best_route_location_id) {
if err := session.DB("cmpe273").C("assignment2").FindId(dataStorage.Best_route_location_id[len(dataStorage.Best_route_location_id)-1]).One(&response); err != nil {
// w.WriteHeader(404)
return
}
startLat = response.Coordinate.Lat
startLng = response.Coordinate.Lng
if err := session.DB("cmpe273").C("assignment2").FindId(dataStorage.Starting_from_location_id).One(&response); err != nil {
// w.WriteHeader(404)
return
}
endLat = response.Coordinate.Lat
endLng = response.Coordinate.Lng
uberAPI(&carRes, dataStorage, startLat, startLng, endLat, endLng)
carRes.Status = "requesting"
carRes.Starting_from_location_id = dataStorage.Best_route_location_id[len(dataStorage.Best_route_location_id)-1]
carRes.Next_destination_location_id = dataStorage.Starting_from_location_id
} else if dataStorage.Index > len(dataStorage.Best_route_location_id) {
carRes.Status = "finished"
carRes.Starting_from_location_id = dataStorage.Starting_from_location_id
carRes.Next_destination_location_id = dataStorage.Starting_from_location_id
} else {
if err := session.DB("cmpe273").C("assignment2").FindId(dataStorage.Best_route_location_id[dataStorage.Index-1]).One(&response); err != nil {
// w.WriteHeader(404)
return
}
startLat = response.Coordinate.Lat
startLng = response.Coordinate.Lng
if err := session.DB("cmpe273").C("assignment2").FindId(dataStorage.Best_route_location_id[dataStorage.Index]).One(&response); err != nil {
// w.WriteHeader(404)
return
}
endLat = response.Coordinate.Lat
endLng = response.Coordinate.Lng
uberAPI(&carRes, dataStorage, startLat, startLng, endLat, endLng)
carRes.Status = "requesting"
carRes.Starting_from_location_id = dataStorage.Best_route_location_id[dataStorage.Index-1]
carRes.Next_destination_location_id = dataStorage.Best_route_location_id[dataStorage.Index]
}
carRes.Id = dataStorage.Id
carRes.Best_route_location_id = dataStorage.Best_route_location_id
carRes.Total_uber_costs = dataStorage.Total_uber_costs
carRes.Total_uber_duration = dataStorage.Total_uber_duration
carRes.Total_distance = dataStorage.Total_distance
dataStorage.Index = dataStorage.Index + 1
hmap[dataStorage.Id] = dataStorage
trip, _ := json.Marshal(carRes)
w.WriteHeader(200)
fmt.Fprintf(w, "%s", trip)
}
func Delete(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
id := p.ByName("id")
if !bson.IsObjectIdHex(id) {
w.WriteHeader(404)
return
}
userId := bson.ObjectIdHex(id)
if err := session.DB("cmpe273").C("assignment2").RemoveId(userId); err != nil {
w.WriteHeader(404)
return
}
w.WriteHeader(200)
}
func main() {
id = 0
hmap = make(map[int]DataStorage)
session, _ = mgo.Dial("mongodb://admin:12345@ds051838.mongolab.com:51838/cmpe273")
defer session.Close()
mux := httprouter.New()
//connectDB()
mux.GET("/locations/:id", Get)
mux.GET("/trips/:trip_id", GetTrip)
mux.POST("/locations/", Create)
mux.POST("/trips/", CreateTrip)
mux.DELETE("/locations/:id", Delete)
mux.PUT("/locations/:id", Update)
mux.PUT("/trips/:trip_id/request", CarRequest)
server := http.Server{
Addr: "0.0.0.0:8080",
Handler: mux,
}
server.ListenAndServe()
}
func createURL(address string, city string, state string) string {
var getURL string
spStr := strings.Split(address, " ")
for i := 0; i < len(spStr); i++ {
if i == 0 {
getURL = spStr[i] + "+"
} else if i == len(spStr)-1 {
getURL = getURL + spStr[i] + ","
} else {
getURL = getURL + spStr[i] + "+"
}
}
spStr = strings.Split(city, " ")
for i := 0; i < len(spStr); i++ {
if i == 0 {
getURL = getURL + "+" + spStr[i]
} else if i == len(spStr)-1 {
getURL = getURL + "+" + spStr[i] + ","
} else {
getURL = getURL + "+" + spStr[i]
}
}
getURL = getURL + "+" + state
return getURL
}
func getLocation(response *Response, formatURL string) {
urlLeft := "http://maps.google.com/maps/api/geocode/json?address="
urlRight := "&sensor=false"
urlFormat := urlLeft + formatURL + urlRight
getLocation, err := http.Get(urlFormat)
if err != nil {
fmt.Println("Get Location Error", err)
panic(err)
}
body, err := ioutil.ReadAll(getLocation.Body)
if err != nil {
fmt.Println("Get Location Error", err)
panic(err)
}
var data MyJsonName
byt := []byte(body)
if err := json.Unmarshal(byt, &data); err != nil {
panic(err)
}
response.Coordinate.Lat = data.Results[0].Geometry.Location.Lat
response.Coordinate.Lng = data.Results[0].Geometry.Location.Lng
}
func getBestRoute(tripRes *TripResponse, dataStorage *DataStorage, originId bson.ObjectId, targetId []bson.ObjectId) {
pmtTarget, err := permutation.NewPerm(targetId, nil)
if err != nil {
fmt.Println(err)
return
}
res := make([][]bson.ObjectId, 0, 0)
routePrice := make([]int, 0, 0)
routeDuration := make([]int, 0, 0)
routeDistance := make([]float64, 0, 0)
curPrice := 0
curDuration := 0
curDistance := 0.0
for result, err := pmtTarget.Next(); err == nil; result, err = pmtTarget.Next() {
//----------------------get Price---------------------------------
for i := 0; i <= len(result.([]bson.ObjectId)); i++ {
var startLat float64
var startLng float64
var endLat float64
var endLng float64
minPrice := 0
minDuration := 0
minDistance := 0.0
response := Response{}
if i == 0 {
if err := session.DB("cmpe273").C("assignment2").FindId(originId).One(&response); err != nil {
// w.WriteHeader(404)
return
}
startLat = response.Coordinate.Lat
startLng = response.Coordinate.Lng
if err := session.DB("cmpe273").C("assignment2").FindId((result.([]bson.ObjectId))[i]).One(&response); err != nil {
// w.WriteHeader(404)
return
}
endLat = response.Coordinate.Lat
endLng = response.Coordinate.Lng
} else if i == len(result.([]bson.ObjectId)) {
if err := session.DB("cmpe273").C("assignment2").FindId((result.([]bson.ObjectId))[i-1]).One(&response); err != nil {
// w.WriteHeader(404)
return
}
startLat = response.Coordinate.Lat
startLng = response.Coordinate.Lng
if err := session.DB("cmpe273").C("assignment2").FindId(originId).One(&response); err != nil {
// w.WriteHeader(404)
return
}
endLat = response.Coordinate.Lat
endLng = response.Coordinate.Lng
} else {
if err := session.DB("cmpe273").C("assignment2").FindId((result.([]bson.ObjectId))[i-1]).One(&response); err != nil {
// w.WriteHeader(404)
return
}
startLat = response.Coordinate.Lat
startLng = response.Coordinate.Lng
if err := session.DB("cmpe273").C("assignment2").FindId((result.([]bson.ObjectId))[i]).One(&response); err != nil {
// w.WriteHeader(404)
return
}
endLat = response.Coordinate.Lat
endLng = response.Coordinate.Lng
}
// you need to add your sever token at the end
urlLeft := "https://api.uber.com/v1/estimates/price?"
urlRight := "start_latitude=" + strconv.FormatFloat(startLat, 'f', -1, 64) + "&start_longitude=" + strconv.FormatFloat(startLng, 'f', -1, 64) + "&end_latitude=" + strconv.FormatFloat(endLat, 'f', -1, 64) + "&end_longitude=" + strconv.FormatFloat(endLng, 'f', -1, 64) + "&server_token=xxxxxxxxxxxxxxxxxxxxxxx"
urlFormat := urlLeft + urlRight
getPrices, err := http.Get(urlFormat)
if err != nil {
fmt.Println("Get Prices Error", err)
panic(err)
}
var data UberAPIResponse
json.NewDecoder(getPrices.Body).Decode(&data)
minPrice = data.Prices[0].LowEstimate
minDuration = data.Prices[0].Duration
minDistance = data.Prices[0].Distance
for i := 0; i < len(data.Prices); i++ {
if minPrice > data.Prices[i].LowEstimate && data.Prices[i].LowEstimate > 0 {
minPrice = data.Prices[i].LowEstimate
minDuration = data.Prices[i].Duration
minDistance = data.Prices[i].Distance
}
}
curPrice = curPrice + minPrice
curDuration = curDuration + minDuration
curDistance = curDistance + minDistance
}
//----------------------get Price---------------------------------
routePrice = AppendInt(routePrice, curPrice)
routeDuration = AppendInt(routeDuration, curDuration)
routeDistance = AppendFloat(routeDistance, curDistance)
fmt.Println(curPrice)
fmt.Println(curDuration)
fmt.Println(curDistance)
curPrice = 0
curDuration = 0
curDistance = 0.0
res = AppendBsonId(res, result.([]bson.ObjectId))
fmt.Println(pmtTarget.Index(), result.([]bson.ObjectId))
}
index := 0
curPrice = 1000
for i := 0; i < len(routePrice); i++ {
if curPrice > routePrice[i] {
curPrice = routePrice[i]
index = i
}
}
fmt.Println("best route is => ")
fmt.Println(res[index])
fmt.Println(routePrice[index])
fmt.Println(routeDuration[index])
fmt.Println(routeDistance[index])
// res[index] // 最后的数组
tripRes.Best_route_location_id = res[index]
tripRes.Total_distance = routeDistance[index]
tripRes.Total_uber_costs = routePrice[index]
tripRes.Total_uber_duration = routeDuration[index]
dataStorage.Id = tripRes.Id
dataStorage.Index = 0
dataStorage.Status = tripRes.Status
dataStorage.Starting_from_location_id = tripRes.Starting_from_location_id
dataStorage.Best_route_location_id = res[index]
dataStorage.Total_uber_costs = tripRes.Total_uber_costs
dataStorage.Total_uber_duration = tripRes.Total_uber_duration
dataStorage.Total_distance = tripRes.Total_distance
}
func AppendBsonId(slice [][]bson.ObjectId, data ...[]bson.ObjectId) [][]bson.ObjectId {
m := len(slice)
n := m + 1
if n > cap(slice) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newSlice := make([][]bson.ObjectId, (n+1)*2)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:n]
copy(slice[m:n], data)
return slice
}
func AppendInt(slice []int, data ...int) []int {
m := len(slice)
n := m + 1
if n > cap(slice) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newSlice := make([]int, (n+1)*2)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:n]
copy(slice[m:n], data)
return slice
}
func AppendFloat(slice []float64, data ...float64) []float64 {
m := len(slice)
n := m + 1
if n > cap(slice) { // if necessary, reallocate
// allocate double what's needed, for future growth.
newSlice := make([]float64, (n+1)*2)
copy(newSlice, slice)
slice = newSlice
}
slice = slice[0:n]
copy(slice[m:n], data)
return slice
}
func uberAPI(carRes *CarResponse, dataStorage DataStorage, startLat float64, startLng float64, endLat float64, endLng float64) {
minPrice := 0
serverToken := "xxxxxxxxxxxxxxxxxxxxxxx"
urlLeft := "https://api.uber.com/v1/estimates/price?"
urlRight := "start_latitude=" + strconv.FormatFloat(startLat, 'f', -1, 64) + "&start_longitude=" + strconv.FormatFloat(startLng, 'f', -1, 64) + "&end_latitude=" + strconv.FormatFloat(endLat, 'f', -1, 64) + "&end_longitude=" + strconv.FormatFloat(endLng, 'f', -1, 64) + "&server_token=" + serverToken
urlFormat := urlLeft + urlRight
var userrequest UserRequest
getPrices, err := http.Get(urlFormat)
if err != nil {
fmt.Println("Get Prices Error", err)
panic(err)
}
var data UberAPIResponse
index := 0
json.NewDecoder(getPrices.Body).Decode(&data)
minPrice = data.Prices[0].LowEstimate
for i := 0; i < len(data.Prices); i++ {
if minPrice > data.Prices[i].LowEstimate {
minPrice = data.Prices[i].LowEstimate
index = i
}
userrequest.Product_id = data.Prices[index].ProductID
}
urlPath := "https://sandbox-api.uber.com/v1/requests"
userrequest.Start_latitude = startLat
userrequest.Start_longitude = startLng
userrequest.End_latitude = endLat
userrequest.End_longitude = endLng
accessToken := "xxxxxxxxxxxxxxxxxxxx"
requestbody, _ := json.Marshal(userrequest)
client := &http.Client{}
req, err := http.NewRequest("POST", urlPath, bytes.NewBuffer(requestbody))
if err != nil {
fmt.Println(err)
return
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Authorization", "Bearer "+accessToken)
res, err := client.Do(req)
if err != nil {
fmt.Println("QueryInfo: http.Get", err)
return
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
uberRes := UberResponse{}
json.Unmarshal(body, &uberRes)
fmt.Println(uberRes)
carRes.Uber_wait_time_eta = uberRes.Eta
}
func getID() int {
if id == 0 {
for id == 0 {
id = rand.Intn(10000)
}
} else {
id = id + 1
}
return id
}