Skip to content

Instantly share code, notes, and snippets.

@samilamti
Last active December 15, 2015 14:39
Show Gist options
  • Save samilamti/5276056 to your computer and use it in GitHub Desktop.
Save samilamti/5276056 to your computer and use it in GitHub Desktop.
Common facade for the Azure Mobile Services end-point we've defined. Kept abstract to allow for platform-specific client APIs for Azure Mobile Services.
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace NinjaLocator.Core
{
public abstract class ServiceClient<TAuthenticationProvider>
{
public TAuthenticationProvider AuhenticationProvider { get; set; }
public string AuthenticationToken { get; set; }
protected abstract void Initialize(string serviceUrl, string applicationKey);
protected abstract Task LoginAsync(string authenticationToken);
protected abstract Task LoginAsync(TAuthenticationProvider auhenticatonProvider);
protected abstract IMobileServiceTable<T> GetTable<T>();
public void LocateNinjas(Ninja requestor, Action<IEnumerable<Ninja>> ninjasLocated)
{
Initialize(Configuration.ServiceUrl, Configuration.ApplicationKey);
Task loginTask;
if (!String.IsNullOrEmpty(AuthenticationToken))
loginTask = LoginAsync(AuthenticationToken);
else
loginTask = LoginAsync(AuhenticationProvider);
loginTask.ContinueWith(delegate
{
var ninjaTable = GetTable<Ninja>();
ninjaTable.InsertAsync(requestor)
.ContinueWith(insertTask => ninjaTable.ReadAsync())
.ContinueWith(readTask => ninjasLocated(readTask.Result.Result));
}, TaskContinuationOptions.OnlyOnRanToCompletion);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment