Skip to content

DmitriiChepilov/uber-csharp-sdk

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Uber C# SDK

Summary

  • Simple wrapper of the Uber API
  • You need an app.
  • All methods are async.
  • All methods return an instance of UberResponse where T is the serialized Uber response data. An UberResponse also contains an Error property which will contain information in the event of an error being returned by the underlying API.
  • Contains an UberSandboxClient for submitting requests against the Uber API sandbox.

Note I built this in my spare time for a bit of fun, if you've found any bugs or you'd like to contribute please get involved!

Authentication

When making requests on behalf of a user via OAuth, ensure you understand scopes.

The following methods can use client OR server authentication:

  • GetProductsAsync
  • GetPriceEstimateAsync
  • GetTimeEstimateAsync
  • GetPromotionAsync

The following methods require client (OAuth) authentication:

  • GetUserActivityAsync
  • GetUserProfileAsync
  • RequestAsync
  • GetRequestDetailsAsync
  • GetRequestMapAsync
  • CancelRequestAsync

Server authentication

var uberClient = new UberClient(AccessTokenType.Server, serverToken, clientId, clientSecret, baseUri);

Client authentication

var uberClient = new UberClient(AccessTokenType.Server, serverToken, clientId, clientSecret, baseUri);

The web application example demonstrates how to authenticate users and execute each method.

OAuth flow

// Create authentication client
var authClient = new UberAuthenticationClient(clientId, clientSecret);
// Generate authorize URL - If you don't specify scope, state or redirect URI then app defaults will be used
var authorizeUrl = authClient.GetAuthorizeUrl();
// Once authorized, swap authorization code for an access token
var accessToken = await authClient.GetAccessTokenAsync(code, "http://your-redirect-uri.com");
// You can now make requests on behalf of the user

GetAuthorizeUrl

Uber Docs

var authClient = new UberAuthenticationClient(clientId, clientSecret);
var url = uberClient.GetAuthorizeUrl();

GetAccessTokenAsync

Uber Docs

var authClient = new UberAuthenticationClient(clientId, clientSecret);
var accessToken = await uberClient.GetAccessTokenAsync(code, "http://your-redirect-uri.com");

RefreshAccessTokenAsync

Uber Docs

var authClient = new UberAuthenticationClient(clientId, clientSecret);
var accessToken = await uberClient.RefreshAccessTokenAsync(refreshToken, "http://your-redirect-uri.com");

RevokeAccessTokenAsync

Uber Docs

var authClient = new UberAuthenticationClient(clientId, clientSecret);
var revoked = await uberClient.RevokeAccessTokenAsync(accessToken);

GetProductsAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Server, serverToken);
var products = await uberClient.GetProductsAsync(startLatitude, startLongitude);

GetPriceEstimateAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Server, serverToken);
var prices = await uberClient.GetPriceEstimateAsync(startLatitude, startLongitude, endLatitude, endLongitude);

GetTimeEstimateAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Server, serverToken);
var times = await uberClient.GetTimeEstimateAsync(startLatitude, startLongitude);

GetPromotionAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Server, serverToken);
var promotion = await uberClient.GetPromotionAsync(startLatitude, startLongitude, endLatitude, endLongitude);

GetUserActivityAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Client, clientToken);
var userActivity = await uberClient.GetUserActivityAsync(0, 50);

GetUserProfileAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Client, clientToken);
var userProfile = await uberClient.GetUserProfileAsync();

RequestAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Client, clientToken);
var request = await uberClient.RequestAsync(productId, startLatitude, startLongitude, endLatitude, endLongitude);

GetRequestDetailsAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Client, clientToken);
var requestDetails = await uberClient.GetRequestDetailsAsync(requestId);

GetRequestMapAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Client, clientToken);
var request = await uberClient.GetRequestMapAsync(requestId);

CancelRequestAsync

Uber Docs

var uberClient = new UberClient(AccessTokenType.Client, clientToken);
var cancel = await uberClient.CancelRequestAsync(requestId);

Webhook Example

Uber Docs

using System;
using System.Configuration;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using System.Web.Http;
using Newtonsoft.Json;
using Uber.SDK.Helpers;
using Uber.SDK.Models;
using WebHooks.Extensions;

namespace WebHooks.Controllers
{
	public class HookController : ApiController
	{
		[HttpPost]
		public async Task<HttpResponseMessage> Uber()
		{
			try
			{
				string json = await Request.Content.ReadAsStringAsync();

				#region Webhook verification

				string signature = Request.GetHeaderValue("X-Uber-Signature");
				if (string.IsNullOrEmpty(signature))
					return Request.CreateResponse(HttpStatusCode.Forbidden);

				string clientSecret = ConfigurationManager.AppSettings["UBER.ClientSecret"];

				if (!UberSignature.IsSendedByUber(clientSecret, json, signature))
					return Request.CreateResponse(HttpStatusCode.Forbidden);

				#endregion Webhook verification

				var uberWebHookData = await JsonDeserialize<WebHook>(json);
				

			}
			catch (Exception e)
			{
				// log exception
				return Request.CreateResponse(HttpStatusCode.OK);
			}
			return Request.CreateResponse(HttpStatusCode.OK);
		}

		[HttpGet]
		public string Test()
		{
			return $"Test. {DateTime.Now}. MachineName: {Environment.MachineName}. {Environment.CurrentDirectory}: {Environment.NewLine}";
		}

		#region Helper method

		private Task<T> JsonDeserialize<T>(string jsonString)
		{
			Task<T> ser = Task.Factory.StartNew(() => JsonConvert.DeserializeObject<T>(jsonString));
			return ser;
		}

		#endregion Helper method
	}
}

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • C# 100.0%