Azure Mobile Services API support for Xamarin
This might be one of my shorter post, but I thought it was worth sharing. I’ve been working with a game lately and depend heavily on Azure Mobile Services. Microsoft has been kind enough to provide us with the Azure Mobile Services for Xamarin, which can be found here. Sadly thought, it does not include support for API’s.
You can do this:
var profiles = App.MobileService.GetTable<UserProfile>();
But you can’t do this:
var myProfile = App.MobileService.InvokeApiAsync<UserProfile>("me");
I find API’s much more useful the working with the tables, so I was bit bummed out when I found out it wasn’t supported. I solved this by simply using the HttpClient to make the calls to the API’s. However that didn’t work to good when enabling the authorization.
It took a bit of work with fiddler to find our what headers needed to be used. So I updated the calls to include these headers:
HttpContent content = new StringContent(payload);
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync(url, content);
That did the trick, and I could now call the API with the permissions != Everyone.
- But I was still bothered by the lack of support for API’s, since that meant I couldn’t share my code across the platforms. Of course I could have used the HttpClient approach above with my Windows Phone clients, but that didn’t seem right.
So I spent some time adding the missing InvokeApi* methods as Extension Methods. And you can download the code here:
To use it, simply download the MobileServiceClientExtensionMethods.cs file and add it to your project. You should update the namespace, and the just add the namespace in the using statement in the class where you want to make the call.