Skip to content

Commit 4e79e05

Browse files
committed
Merge pull request #10 from Unity3dAzure/azureappservices
Azure App Services
2 parents 91e01a3 + 4df220b commit 4e79e05

26 files changed

+987
-116
lines changed

Assets/MobileServices/README.md

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# Azure Mobile Services for Unity3d
2-
For game developers looking to use Azure Mobile Services in their Unity3D project.
2+
For game developers looking to use Azure Mobile Services or Azure App Services in their Unity3D project.
33

4-
The REST service implements [UnityRestClient](https://github.com/ProjectStratus/UnityRestClient) which uses the JsonFx plugin.
5-
Runs in UnityEditor and works on iOS, Android and Windows.
4+
Also available as a [MobileServices Demo Project](https://github.com/Unity3dAzure/MobileServicesDemo) which will run inside UnityEditor and on iOS, Android and Windows. The demo project has got everything bundled in and does not require any additional assets to work. If you want to integrate with an existing Unity3d project then follow instructions below:
65

76
## How to add MobileServices into Unity3d project
87
1. [Download UnityRestClient](https://github.com/ProjectStratus/UnityRestClient/archive/master.zip)
9-
* Copy 'plugins' and 'Source' into project `Assets` folder
8+
* Copy JsonFx 'plugins' and the UnityRestClient 'Source' into project `Assets` folder
109
2. [Download MobileServices](https://github.com/Unity3dAzure/MobileServices/archive/master.zip)
11-
* Copy 'MobileServices' into project `Assets` folder.
12-
3. [Create a Mobile Service](https://manage.windowsazure.com)
10+
* Copy 'MobileServices' into project `Assets` folder.
11+
3. Create an [Azure Mobile Service](https://manage.windowsazure.com) or an [Azure App Service](https://portal.azure.com)
1312
* Create a table for app data.
1413

1514
## Supported Features
@@ -22,12 +21,12 @@ InvokeApi | Get custom API
2221
### MobileServiceTable
2322
API | Description
2423
--- | -----------
25-
Insert | Create a new item.
26-
Read | Get a list of items.
27-
Update | Update an item’s data using id property.
24+
Insert | Create a new item.
25+
Read | Get a list of items.
26+
Update | Update an item’s data using id property.
2827
Delete | Delete an item using id property.
29-
Query | Get a list of results using a custom query.
30-
Lookup | Get an item’s data using id property.
28+
Query | Get a list of results using a custom query.
29+
Lookup | Get an item’s data using id property.
3130

3231
### MobileServiceClient Interface
3332
void Login(MobileServiceAuthenticationProvider provider, string token, Action<IRestResponse<MobileServiceUser>> callback = null);
@@ -43,33 +42,33 @@ Lookup | Get an item’s data using id property.
4342

4443

4544
## Sample usage
46-
47-
```csharp
45+
```
4846
using UnityEngine;
4947
using System;
5048
using System.Net;
5149
using System.Collections.Generic;
5250
using RestSharp;
5351
using Pathfinding.Serialization.JsonFx;
5452
using Unity3dAzure.MobileServices;
53+
5554
```
5655

57-
```csharp
56+
```
5857
private MobileServiceClient _client;
5958
private MobileServiceTable<TodoItem> _table;
6059
```
6160

62-
```csharp
61+
```
6362
void Start () {
6463
_client = new MobileServiceClient(appUrl, appKey); // <- add your app connection strings here.
6564
_table = _client.GetTable<TodoItem>("TodoItem");
6665
}
6766
```
68-
69-
```csharp
67+
```
7068
private void ReadItems() {
7169
_table.Read<TodoItem>(OnReadItemsCompleted);
7270
}
71+
7372
private void OnReadItemsCompleted(IRestResponse<List<TodoItem>> response) {
7473
if ( response.StatusCode == HttpStatusCode.OK) {
7574
Debug.Log("OnReadItemsCompleted data: " + response.Content);
@@ -82,4 +81,7 @@ private void OnReadItemsCompleted(IRestResponse<List<TodoItem>> response) {
8281
}
8382
```
8483

85-
Questions or tweet #Azure #GameDev [@deadlyfingers](https://twitter.com/deadlyfingers)
84+
## Dependencies
85+
The REST service implements [UnityRestClient](https://github.com/ProjectStratus/UnityRestClient) which uses [JsonFx](https://bitbucket.org/TowerOfBricks/jsonfx-for-unity3d-git/) to parse JSON data.
86+
87+
Questions or tweet #Azure #GameDev [@deadlyfingers](https://twitter.com/deadlyfingers)

Assets/MobileServices/client/MobileServiceClient.cs

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using UnityEngine;
1+
using UnityEngine;
22
using System.Collections;
33
using RestSharp;
44
using System.Collections.Generic;
@@ -12,16 +12,16 @@
1212
namespace Unity3dAzure.MobileServices
1313
{
1414
public class MobileServiceClient : RestClient, IAzureMobileServiceClient
15-
{
15+
{
1616
public string AppUrl { get; private set; }
1717
public string AppKey { get; private set; }
18-
18+
1919
public MobileServiceUser User { get; set; }
20-
20+
2121
public const string URI_API = "api/";
22-
22+
2323
/// <summary>
24-
/// Creates a new RestClient using Azure Mobile Service's Application Url
24+
/// Creates a new RestClient using Azure Mobile Service's Application Url and App Key
2525
/// </summary>
2626
public MobileServiceClient(string appUrl, string appKey) : base(appUrl)
2727
{
@@ -35,6 +35,14 @@ public MobileServiceClient(string appUrl, string appKey) : base(appUrl)
3535
#endif
3636
}
3737

38+
/// <summary>
39+
/// Creates a new RestClient using Azure App Service's Application Url
40+
/// </summary>
41+
public MobileServiceClient(string appUrl) : base(appUrl)
42+
{
43+
AppUrl = appUrl;
44+
}
45+
3846
public override string ToString()
3947
{
4048
return this.BaseUrl;
@@ -44,19 +52,20 @@ public MobileServiceTable<E> GetTable<E>(string tableName) where E : class
4452
{
4553
return new MobileServiceTable<E>(tableName, this);
4654
}
47-
55+
4856
/// <summary>
4957
/// Client-directed single sign on (POST with access token)
5058
/// </summary>
5159
public void Login(MobileServiceAuthenticationProvider provider, string token, Action<IRestResponse<MobileServiceUser>> callback = null)
5260
{
53-
string uri = "login/" + provider.ToString().ToLower();
61+
string p = provider.ToString().ToLower();
62+
string uri = IsAppService() ? ".auth/login/" + p : "login/" + p;
5463
ZumoRequest request = new ZumoRequest(this, uri, Method.POST);
5564
Debug.Log("Login Request Uri: " + uri + " access token: " + token);
5665
request.AddBodyAccessToken(token);
5766
this.ExecuteAsync(request, callback);
5867
}
59-
68+
6069
/// <summary>
6170
/// TODO: Service login (using GET via webview)
6271
/// </summary>
@@ -66,9 +75,9 @@ public void Login(MobileServiceAuthenticationProvider provider)
6675
Debug.Log("TODO");
6776
}
6877
//*/
69-
78+
7079
/// <summary>
71-
// TODO: Implement custom API (using GET request)
80+
/// TODO: Implement custom API (using GET request)
7281
/// </summary>
7382
public void InvokeApi<T>(string apiName, Action<IRestResponse<T>> callback = null) where T : new()
7483
{
@@ -78,6 +87,14 @@ public void Login(MobileServiceAuthenticationProvider provider)
7887
this.ExecuteAsync(request, callback);
7988
}
8089

90+
/// <summary>
91+
/// Mobile Service uses an AppKey, but App Service does not.
92+
/// </summary>
93+
public bool IsAppService()
94+
{
95+
return String.IsNullOrEmpty(AppKey);
96+
}
97+
8198
#if !NETFX_CORE || UNITY_ANDROID
8299
private bool RemoteCertificateValidationCallback(System.Object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
83100
{

Assets/MobileServices/http/ZumoRequest.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,15 @@ public class ZumoRequest : RestRequest
88
{
99
public ZumoRequest(MobileServiceClient client, string uri, Method httpMethod) : base(uri, httpMethod)
1010
{
11-
this.AddHeader("X-ZUMO-APPLICATION", client.AppKey);
12-
//this.AddHeader("Content-Type", "application/json; charset=UTF-8"); // this line caused winrt app to not connect
11+
if (client.IsAppService())
12+
{
13+
// App Service headers
14+
this.AddHeader("ZUMO-API-VERSION", "2.0.0");
15+
} else {
16+
// Mobile Service headers
17+
this.AddHeader("X-ZUMO-APPLICATION", client.AppKey);
18+
}
19+
1320
this.AddHeader("Accept", "application/json");
1421
this.RequestFormat = DataFormat.Json;
1522
if (client.User != null && !string.IsNullOrEmpty(client.User.authenticationToken))

0 commit comments

Comments
 (0)