def authorization_code_grant_flow(credentials, storage_filename): """Get an access token through Authorization Code Grant. Parameters credentials (dict) All your app credentials and information imported from the configuration file. storage_filename (str) Filename to store OAuth 2.0 Credentials. Returns (UberRidesClient) An UberRidesClient with OAuth 2.0 Credentials. """ auth_flow = AuthorizationCodeGrant( credentials.get('client_id'), credentials.get('scopes'), credentials.get('client_secret'), credentials.get('redirect_url'), ) auth_url = auth_flow.get_authorization_url() login_message = 'Login and grant access by going to:\n{}\n' login_message = login_message.format(auth_url) response_print(login_message) redirect_url = 'Copy the URL you are redirected to and paste here: \n' result = raw_input(redirect_url).strip() try: session = auth_flow.get_session(result) except (ClientError, UberIllegalState), error: fail_print(error) return
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') last_name = profile.get('last_name') email = profile.get('email') message = 'Hello, {} {}. Successfully granted access token to {}.' message = message.format(first_name, last_name, email) success_print(message) success_print(profile) success_print('---') response = api_client.get_home_address() address = response.json success_print(address) success_print('---') response = api_client.get_user_activity() history = response.json success_print(history)
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_driver_profile() except (ClientError, ServerError) as error: fail_print(error) return else: profile = response.json first_name = profile.get('first_name') last_name = profile.get('last_name') email = profile.get('email') message = 'Hello, {} {}. Successfully granted access token to {}.' message = message.format(first_name, last_name, email) success_print(message) success_print(profile) success_print('---') response = api_client.get_driver_trips() trips = response.json success_print(trips) success_print('---') response = api_client.get_driver_payments() payments = response.json success_print(payments)
def authorization_code_grant_flow(credentials, storage_filename): """Get an access token through Authorization Code Grant. Parameters credentials (dict) All your app credentials and information imported from the configuration file. storage_filename (str) Filename to store OAuth 2.0 Credentials. Returns (UberRidesClient) An UberRidesClient with OAuth 2.0 Credentials. """ auth_flow = AuthorizationCodeGrant( credentials.get('client_id'), credentials.get('scopes'), credentials.get('client_secret'), credentials.get('redirect_url'), ) print(auth_fow) exit(1) auth_url = auth_flow.get_authorization_url() login_message = 'Login as a rider and grant access by going to:\n\n{}\n' login_message = login_message.format(auth_url) response_print(login_message) redirect_url = 'Copy the URL you are redirected to and paste here: \n\n' result = input(redirect_url).strip() try: session = auth_flow.get_session(result) except (ClientError, UberIllegalState) as error: fail_print(error) return credential = session.oauth2credential credential_data = { 'client_id': credential.client_id, 'redirect_url': credential.redirect_url, 'access_token': credential.access_token, 'expires_in_seconds': credential.expires_in_seconds, 'scopes': list(credential.scopes), 'grant_type': credential.grant_type, 'client_secret': credential.client_secret, 'refresh_token': credential.refresh_token, } with open(storage_filename, 'w') as yaml_file: yaml_file.write(safe_dump(credential_data, default_flow_style=False)) return UberRidesClient(session, sandbox_mode=True)
def authorization_code_grant_flow(credentials, storage_filename): """Get an access token through Authorization Code Grant. Parameters credentials (dict) All your app credentials and information imported from the configuration file. storage_filename (str) Filename to store OAuth 2.0 Credentials. Returns (UberRidesClient) An UberRidesClient with OAuth 2.0 Credentials. """ auth_flow = AuthorizationCodeGrant( credentials.get('client_id'), credentials.get('scopes'), credentials.get('client_secret'), credentials.get('redirect_url'), ) auth_url = auth_flow.get_authorization_url() login_message = 'Login and grant access by going to:\n{}\n' login_message = login_message.format(auth_url) response_print(login_message) redirect_url = 'Copy the URL you are redirected to and paste here: \n' result = input(redirect_url).strip() try: session = auth_flow.get_session(result) except (ClientError, UberIllegalState) as error: fail_print(error) return credential = session.oauth2credential credential_data = { 'client_id': credential.client_id, 'redirect_url': credential.redirect_url, 'access_token': credential.access_token, 'expires_in_seconds': credential.expires_in_seconds, 'scopes': list(credential.scopes), 'grant_type': credential.grant_type, 'client_secret': credential.client_secret, 'refresh_token': credential.refresh_token, } with open(storage_filename, 'w') as yaml_file: yaml_file.write(safe_dump(credential_data, default_flow_style=False)) return UberRidesClient(session, sandbox_mode=True)
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), error: fail_print(error)
def get_ride_details(api_client, ride_id): """Use an UberRidesClient to update ride status and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient to access API resources. ride_id (str) Unique ride identifier. """ try: ride_details = api_client.get_ride_details(ride_id) except (ClientError, ServerError), error: fail_print(error)
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), error: fail_print(error) return
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 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(PRODUCT_ID, surge_multiplier=surge_multiplier) except (ClientError, ServerError), error: fail_print(error)
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 to access API resources. 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), error: fail_print(error)
def request_ufp_ride(api_client, start_lat, start_lng, end_lat, end_lng): """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: print(error) fail_print(error) return else: #success_print(estimate.json) #success_print(request.json) fare = estimate.json["fare"]["display"] pickup_estimate = estimate.json["pickup_estimate"] trip_duration_estimate = estimate.json["trip"]["duration_estimate"] / 60 paragraph_print( "Die Fahrt wird vorraussichtlich %s kosten.\nDer Fahrer kann in %s Minuten da sein.\nDie Fahrdauer bis zum Ziel betrÃĪgt %s Minuten" % (fare, pickup_estimate, trip_duration_estimate)) return (request.json.get('request_id'), pickup_estimate, fare)
def estimate_ride(api_client): """Use an UberRidesClient to fetch a ride estimate and print the results. Parameters api_client (UberRidesClient) 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)
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), error: fail_print(error)
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, surge_multiplier=surge_multiplier, ) except (ClientError, ServerError), error: fail_print(error)
def estimate_ride(api_client, start_lat, start_lng, end_lat, end_lng): """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=2) except (ClientError, ServerError) as error: fail_print(error) else: success_print(estimate.json)
def get_ride_details(api_client, ride_id, verbose=True): """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. :param verbose: :param verbose: """ try: ride_details = api_client.get_ride_details(ride_id) except (ClientError, ServerError) as error: fail_print(error) else: if verbose: success_print(ride_details.json) return ride_details.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 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 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')
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 = raw_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_ride(api_client, surge_id) except (ClientError, ServerError), 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 update ride status and print the results. Parameters api_client (UberRidesClient) An authorized UberRidesClient to access API resources. ride_id (str) Unique ride identifier.