def request_surge_ride(api_client, surge_confirmation_id=None): """Use an UberRidesClient to request a ride and print the results. If the product has a surge_multiple greater than or equal to 2.0, a SurgeError is raised. Confirm surge by visiting the surge_confirmation_url and automatically try the request again. Parameters api_client (UberRidesClient) An authorized UberRidesClient with 'request' scope. surge_confirmation_id (string) Unique identifer received after confirming surge. Returns The unique ID of the requested ride. """ try: request = api_client.request_ride( product_id=SURGE_PRODUCT_ID, start_latitude=START_LAT, start_longitude=START_LNG, end_latitude=END_LAT, end_longitude=END_LNG, surge_confirmation_id=surge_confirmation_id, seat_count=2 ) except SurgeError as e: surge_message = 'Confirm surge by visiting: \n{}\n' surge_message = surge_message.format(e.surge_confirmation_href) response_print(surge_message) confirm_url = 'Copy the URL you are redirected to and paste here: \n' result = input(confirm_url).strip() querystring = urlparse(result).query query_params = parse_qs(querystring) surge_id = query_params.get('surge_confirmation_id')[0] # automatically try request again return request_surge_ride(api_client, surge_id) except (ClientError, ServerError) as error: fail_print(error) return else: success_print(request.json) return request.json.get('request_id')
def get_ride_details(api_client, ride_id): """Use an UberRidesClient to get ride details and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient with 'request' scope. ride_id (str) Unique ride identifier. """ try: ride_details = api_client.get_ride_details(ride_id) except (ClientError, ServerError) as error: fail_print(error) else: success_print(ride_details.json)
def estimate_ride(api_client): """Use an UberRidesClient to fetch a ride estimate and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient with 'request' scope. """ try: estimate = api_client.estimate_ride(product_id=SURGE_PRODUCT_ID, start_latitude=START_LAT, start_longitude=START_LNG, end_latitude=END_LAT, end_longitude=END_LNG, seat_count=1) except (ClientError, ServerError) as error: fail_print(error) else: success_print(estimate.json)
def update_ride(api_client, ride_status, ride_id): """Use an UberRidesClient to update ride status and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient with 'request' scope. ride_status (str) New ride status to update to. ride_id (str) Unique identifier for ride to update. """ try: update_product = api_client.update_sandbox_ride(ride_id, ride_status) except (ClientError, ServerError) as error: fail_print(error) else: message = '{} New status: {}' message = message.format(update_product.status_code, ride_status) success_print(message)
def hello_user(api_client): """Use an authorized client to fetch and print profile information. Parameters api_client (UberRidesClient) An UberRidesClient with OAuth 2.0 credentials. """ try: response = api_client.get_user_profile() except (ClientError, ServerError) as error: fail_print(error) return else: profile = response.json first_name = profile.get('first_name') email = profile.get('email') message = 'Hello, {}. Successfully granted access token to {}.' message = message.format(first_name, email) success_print(message)
def update_surge(api_client, surge_multiplier): """Use an UberRidesClient to update surge and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient with 'request' scope. surge_mutliplier (float) The surge multiple for a sandbox product. A multiplier greater than or equal to 2.0 will require the two stage confirmation screen. """ try: update_surge = api_client.update_sandbox_product( SURGE_PRODUCT_ID, surge_multiplier=surge_multiplier, ) except (ClientError, ServerError) as error: fail_print(error) else: success_print(update_surge.status_code)
def estimate_ride(api_client): """Use an UberRidesClient to fetch a ride estimate and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient with 'request' scope. """ try: estimate = api_client.estimate_ride( product_id=PRODUCT_ID, start_latitude=START_LAT, start_longitude=START_LNG, end_latitude=END_LAT, end_longitude=END_LNG, ) except (ClientError, ServerError) as error: fail_print(error) else: success_print(estimate.json)
def request_ufp_ride(api_client): """Use an UberRidesClient to request a ride and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient with 'request' scope. Returns The unique ID of the requested ride. """ try: estimate = api_client.estimate_ride( product_id=UFP_PRODUCT_ID, start_latitude=START_LAT, start_longitude=START_LNG, end_latitude=END_LAT, end_longitude=END_LNG, seat_count=2 ) fare = estimate.json.get('fare') request = api_client.request_ride( product_id=UFP_PRODUCT_ID, start_latitude=START_LAT, start_longitude=START_LNG, end_latitude=END_LAT, end_longitude=END_LNG, seat_count=2, fare_id=fare['fare_id'] ) except (ClientError, ServerError) as error: fail_print(error) return else: success_print(estimate.json) success_print(request.json) return request.json.get('request_id')
An authorized UberRidesClient to access API resources. """ try: estimate = api_client.estimate_ride( PRODUCT_ID, START_LAT, START_LNG, END_LAT, END_LNG, ) except (ClientError, ServerError), error: fail_print(error) else: success_print(estimate.json) def update_surge(api_client, surge_multiplier): """Use an UberRidesClient to update surge and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient to access API resources. surge_mutliplier (float) The surge multiple for a sandbox product. A multiplier greater than or equal to 2.0 will require the two stage confirmation screen. """ try: update_surge = api_client.update_sandbox_product( PRODUCT_ID,
""" try: response = api_client.get_user_profile() except (ClientError, ServerError), error: fail_print(error) return else: profile = response.json first_name = profile.get('first_name') email = profile.get('email') message = 'Hello, {}. Successfully granted access token to {}.' message = message.format(first_name, email) success_print(message) if __name__ == '__main__': """Run the example. Get an access token through the OAuth 2.0 Authorization Code Grant and use credentials to create an UberRidesClient. """ credentials = import_app_credentials() api_client = authorization_code_grant_flow( credentials, utils.STORAGE_FILENAME, )