Ejemplo n.º 1
0
func uberAPI(carRes *structure.CarResponse, tripData structure.DataStorage, startLat float64, startLng float64, endLat float64, endLng float64) {
	minPrice := 0
	// Server token should be kept safe from others
	serverToken := "PLEASE PUT YOUR OWN SERVER TOKEN HERE"
	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 structure.UserRequest

	getPrices, err := http.Get(urlFormat)
	if err != nil {
		fmt.Println("Get Prices Error", err)
		panic(err)
	}

	var data structure.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 cannot be told to others, therefore I leave this line empty
	accessToken := "PLEASE PUT YOUR OWN ACCESSTOKEN HERE"
	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 := structure.UberResponse{}
	json.Unmarshal(body, &uberRes)

	fmt.Println(uberRes)

	carRes.Uber_wait_time_eta = uberRes.Eta
}
Ejemplo n.º 2
0
func CarRequest(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
	tId := p.ByName("trip_id")
	checkID, _ := strconv.Atoi(tId)
	var tripData structure.DataStorage
	findTarget := false

	for key, value := range hmap {
		if key == checkID {
			tripData = value
			findTarget = true
		}
	}

	if findTarget == false {
		w.WriteHeader(404)
		return
	}

	var startLat float64
	var startLng float64
	var endLat float64
	var endLng float64
	carRes := structure.CarResponse{}
	response := structure.Response{}

	if tripData.Index == 0 {
		if err := session.DB("cmpe273_project").C("hello").FindId(tripData.Starting_from_location_id).One(&response); err != nil {
			return
		}
		startLat = response.Coordinate.Lat
		startLng = response.Coordinate.Lng

		if err := session.DB("cmpe273_project").C("hello").FindId(tripData.Best_route_location_id[0]).One(&response); err != nil {
			return
		}
		endLat = response.Coordinate.Lat
		endLng = response.Coordinate.Lng
		uberAPI(&carRes, tripData, startLat, startLng, endLat, endLng)
		carRes.Status = "requesting"
		carRes.Starting_from_location_id = tripData.Starting_from_location_id
		carRes.Next_destination_location_id = tripData.Best_route_location_id[0]
	} else if tripData.Index == len(tripData.Best_route_location_id) {
		if err := session.DB("cmpe273_project").C("hello").FindId(tripData.Best_route_location_id[len(tripData.Best_route_location_id)-1]).One(&response); err != nil {
			return
		}
		startLat = response.Coordinate.Lat
		startLng = response.Coordinate.Lng

		if err := session.DB("cmpe273_project").C("hello").FindId(tripData.Starting_from_location_id).One(&response); err != nil {
			return
		}
		endLat = response.Coordinate.Lat
		endLng = response.Coordinate.Lng
		uberAPI(&carRes, tripData, startLat, startLng, endLat, endLng)
		carRes.Status = "requesting"
		carRes.Starting_from_location_id = tripData.Best_route_location_id[len(tripData.Best_route_location_id)-1]
		carRes.Next_destination_location_id = tripData.Starting_from_location_id
	} else if tripData.Index > len(tripData.Best_route_location_id) {
		carRes.Status = "finished"
		carRes.Starting_from_location_id = tripData.Starting_from_location_id
		carRes.Next_destination_location_id = tripData.Starting_from_location_id
	} else {
		if err := session.DB("cmpe273_project").C("hello").FindId(tripData.Best_route_location_id[tripData.Index-1]).One(&response); err != nil {
			return
		}
		startLat = response.Coordinate.Lat
		startLng = response.Coordinate.Lng

		if err := session.DB("cmpe273_project").C("hello").FindId(tripData.Best_route_location_id[tripData.Index]).One(&response); err != nil {
			return
		}
		endLat = response.Coordinate.Lat
		endLng = response.Coordinate.Lng
		uberAPI(&carRes, tripData, startLat, startLng, endLat, endLng)
		carRes.Status = "requesting"
		carRes.Starting_from_location_id = tripData.Best_route_location_id[tripData.Index-1]
		carRes.Next_destination_location_id = tripData.Best_route_location_id[tripData.Index]
	}

	carRes.Id = tripData.Id
	carRes.Best_route_location_id = tripData.Best_route_location_id
	carRes.Total_uber_costs = tripData.Total_uber_costs
	carRes.Total_uber_duration = tripData.Total_uber_duration
	carRes.Total_distance = tripData.Total_distance

	tripData.Index = tripData.Index + 1
	hmap[tripData.Id] = tripData

	trip, _ := json.Marshal(carRes)
	w.WriteHeader(200)
	fmt.Fprintf(w, "%s", trip)
}