Package: uber_rides
Python SDK (beta) to support theUber Rides API.InstallationTo use the Uber Rides Python SDK:$pipinstalluber_ridesHead over topip-installerfor instructions on installing pip.To run from source, you candownload the source codefor uber-rides, and then run:$pythonsetup.pyinstallWe recommend usingvirtualenvwhen setting up your project environment. You may need to run the above commands withsudoif you’re not using it.Read-Only UseIf you just need read-only access to Uber API resources, like getting a location’s available products, create a Session with the server token you received afterregistering your app.fromuber_rides.sessionimportSessionsession=Session(server_token=YOUR_SERVER_TOKEN)Use this Session to create an UberRidesClient and fetch API resources:fromuber_rides.clientimportUberRidesClientclient=UberRidesClient(session)response=client.get_products(37.77,-122.41)products=response.json.get('products')AuthorizationIf you need to access protected resources or modify resources (like getting a user’s ride history or requesting a ride), you will need the user to grant access to your application through the OAuth 2.0 Authorization Code flow. SeeUber API docs.The Authorization Code flow is a two-step authorization process. The first step is having the user authorize your app and the second involves requesting an OAuth 2.0 access token from Uber. This process is mandatory if you want to take actions on behalf of a user or access their information.fromuber_rides.authimportAuthorizationCodeGrantauth_flow=AuthorizationCodeGrant(YOUR_CLIENT_ID,YOUR_PERMISSION_SCOPES,YOUR_CLIENT_SECRET,YOUR_REDIRECT_URL,)auth_url=auth_flow.get_authorization_url()You can findYOUR_CLIENT_IDandYOUR_CLIENT_SECRETin thedeveloper dashboardunder the settings tab of your application.YOUR_PERMISSION_SCOPESis thelist of scopesyou have requested in the authorizations tab. Note thatYOUR_REDIRECT_URLmust match the value you provided when you registered your application.Navigate the user to theauth_urlwhere they can grant access to your application. After, they will be redirected to aredirect_urlwith the format YOUR_REDIRECT_URL?code=UNIQUE_AUTH_CODE. Use thisredirect_urlto create a session and start UberRidesClient.session=auth_flow.get_session(redirect_url)client=UberRidesClient(session)credentials=session.oauth2credentialKeepcredentialsinformation in a secure data store and reuse them to make API calls on behalf of your user. The SDK will handle the token refresh for you automatically when it makes API requests with an UberRidesClient.Example AppsNavigate to theexamplefolder to access the python example apps. Before you can run an example, you must edit theexample/config.*.yamlfile and add your app credentials from the Uber developer dashboard.To get an UberRidesClient through the Authorization Code flow, run:$pythonexample/authorize_rider.pyThe example above stores user credentials inexample/oauth2_rider_session_store.yaml. To create an UberRidesClient with these credentials and go through a surge ride request run:$pythonexample/request_ride.py—To get an UberRidesClient authorized for driver endpoints, run:$pythonexample/authorize_driver.pyThe example above stores user credentials inexample/oauth2_driver_session_store.yaml.Flask Demo AppsTo get an understanding of how the sdk can be use in an example app see the flask examples for rider and driver dashboards:$pipinstallflask$pythonexample/rider_dashboard.py$pythonexample/driver_dashboard.pyGet Available Productsresponse=client.get_products(37.77,-122.41)products=response.json.get('products')product_id=products[0].get('product_id')Get Price Estimatesresponse=client.get_price_estimates(start_latitude=37.770,start_longitude=-122.411,end_latitude=37.791,end_longitude=-122.405,seat_count=2)estimate=response.json.get('prices')Get Rider Profileresponse=client.get_rider_profile()profile=response.jsonfirst_name=profile.get('first_name')last_name=profile.get('last_name')email=profile.get('email')Get User Historyresponse=client.get_user_activity()history=response.jsonRequest a Ride# Get products for locationresponse=client.get_products(37.77,-122.41)products=response.json.get('products')product_id=products[0].get('product_id')# Get upfront fare for product with start/end locationestimate=client.estimate_ride(product_id=product_id,start_latitude=37.77,start_longitude=-122.41,end_latitude=37.79,end_longitude=-122.41,seat_count=2)fare=estimate.json.get('fare')# Request ride with upfront fare for product with start/end locationresponse=client.request_ride(product_id=product_id,start_latitude=37.77,start_longitude=-122.41,end_latitude=37.79,end_longitude=-122.41,seat_count=2,fare_id=fare['fare_id'])request=response.jsonrequest_id=request.get('request_id')# Request ride details from request_idresponse=client.get_ride_details(request_id)ride=response.json# Cancel a rideresponse=client.cancel_ride(request_id)ride=response.jsonThis makes a real-world request and send an Uber driver to the specified start location.To develop and test against request endpoints in a sandbox environment, make sure to instantiate your UberRidesClient withclient=UberRidesClient(session,sandbox_mode=True)The default forsandbox_modeis set toFalse. See ourdocumentationto read more about using the Sandbox Environment.Update Sandbox RideIf you are requesting sandbox rides, you will need to step through the different states of a ride.response=client.update_sandbox_ride(ride_id,'accepted')response=client.update_sandbox_ride(ride_id,'in_progress')If the update is successful,response.status_codewill be 204.Theupdate_sandbox_ridemethod is not valid in normal mode, where the ride status will change automatically.Get Driver Profileresponse=client.get_driver_profile()profile=response.jsonfirst_name=profile.get('first_name')last_name=profile.get('last_name')email=profile.get('email')Get Driver Tripsresponse=client.get_driver_trips()trips=response.jsonGet Driver Paymentsresponse=client.get_driver_payments()payments=response.jsonGet Uber for Business Receiptsfromuber_rides.authimportClientCredentialGrantfromuber_rides.clientimportUberRidesClientauth_flow=ClientCredentialGrant(<CLIENT_ID>,<SCOPES>,<CLIENT_SECRET>)session=auth_flow.get_session()client=UberRidesClient(session)receipt=client.get_business_trip_receipt('2a2f3da4-asdad-ds-12313asd')pdf_url=client.get_business_trip_receipt_pdf_url('2a2f3da4-asdad-ds-12313asd')Getting helpUber developers actively monitor theUber Tagon StackOverflow. If you need help installing or using the library, you can ask a question there. Make sure to tag your question withuber-apiandpython!For full documentation about our API, visit ourDeveloper Site.See theGetting Started Tutorial.ContributingWe love contributions. If you’ve found a bug in the library or would like new features added, go ahead and open issues or pull requests against this repo. Write a test to show your bug was fixed or the feature works as expected.