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 request_ufp_ride(api_client): 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')
def request_surge_ride(api_client, surge_confirmation_id=None): 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): 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 update_ride(api_client, ride_status, ride_id): 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): 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 authorization_code_grant_flow(credentials, storage_filename): """Get an access token through Authorization Code Grant. Parameters credentials (dict) All 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 estimate_ride(api_client): 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 request_ufp_ride_end_to_start(api_client): global display_fare_end_to_start try: estimate_end_to_start = api_client.estimate_ride( product_id=OAKLAND_POOL_PRODUCT_ID, start_latitude=END_LAT, start_longitude=END_LNG, end_latitude=START_LAT, end_longitude=START_LNG, seat_count=1) except (ClientError, ServerError) as error: fail_print(error) return else: # success_print(estimate_end_to_start.json) #See the raw estimate printed in green display_fare_end_to_start = str( (estimate_end_to_start.json.get("fare")).get("display"))
def request_available_products(api_client): try: request_products = api_client.get_products( latitude=START_LAT, longitude=START_LNG, ) except (ClientError, ServerError) as error: fail_print(error) return else: # success_print(request_products.json) #See the raw estimate printed in green product_step1 = (request_products.json.get("products")) find_pool = next( (item for item in product_step1 if item['display_name'] == 'POOL'), None) response_print(find_pool.get("display_name")) response_print(find_pool.get("product_id"))
def get_api_client(result): try: print(result) session = auth_flow.get_session(result) except (ClientError, LyftIllegalState) as error: fail_print(error) return credential = session.oauth2credential credential_data = { 'client_id': credential.client_id, '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(utils.STORAGE_FILENAME, 'w') as yaml_file: yaml_file.write(safe_dump(credential_data, default_flow_style=False)) return LyftRidesClient(session)