Skip to content

Commit 682a960

Browse files
authoredOct 15, 2016
Merge pull request #36 from Unity3dAzure/iss35
Easy API for get, post, put, patch, delete methods. Resolves #35
2 parents 90a3632 + cfbfaae commit 682a960

File tree

5 files changed

+47
-17
lines changed

5 files changed

+47
-17
lines changed
 

‎Assets/AppServices/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ For detailed instructions read my developer blog on [how to setup Azure App Serv
1818
API | Description
1919
--- | -----------
2020
Login | Client-directed login using access token.
21-
InvokeApi | Get custom API
21+
InvokeApi | Invoke custom API (Easy API) using GET, POST, PUT, PATCH or DELETE
2222

2323
### MobileServiceTable
2424
API | Description

‎Assets/AppServices/client/IAzureMobileServiceClient.cs

+11-1
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,18 @@ public interface IAzureMobileServiceClient
2222
//void Login(MobileServiceAuthenticationProvider provider);
2323

2424
/// <summary>
25-
/// TODO: Custom API
25+
/// Invokes a custom API using GET HTTP method
2626
/// </summary>
2727
void InvokeApi<T>(string apiName, Action<IRestResponse<T>> callback = null) where T : new();
28+
29+
/// <summary>
30+
/// Invokes a custom API for HTTP Methods: GET, POST, PUT, PATCH, DELETE
31+
/// </summary>
32+
void InvokeApi<T>(string apiName, Method httpMethod, Action<IRestResponse<T>> callback = null) where T : new();
33+
34+
/// <summary>
35+
/// Invokes a custom API with body object
36+
/// </summary>
37+
void InvokeApi<T> (string apiName, Method httpMethod, T body, Action<IRestResponse<T>> callback = null) where T : new();
2838
}
2939
}

‎Assets/AppServices/client/MobileServiceClient.cs

+25-5
Original file line numberDiff line numberDiff line change
@@ -97,16 +97,36 @@ public void Login(MobileServiceAuthenticationProvider provider)
9797
//*/
9898

9999
/// <summary>
100-
/// TODO: Implement custom API (using GET request)
100+
/// GET custom API
101101
/// </summary>
102102
public void InvokeApi<T>(string apiName, Action<IRestResponse<T>> callback = null) where T : new()
103103
{
104-
string uri = URI_API + apiName;
105-
ZumoRequest request = new ZumoRequest(this, uri, Method.GET);
106-
Debug.Log( "Custom API Request Uri: " + uri );
107-
this.ExecuteAsync(request, callback);
104+
InvokeApi<T> (apiName, Method.GET, callback);
108105
}
109106

107+
/// <summary>
108+
/// Invokes custom API for HTTP Methods: GET, POST, PUT, PATCH, DELETE
109+
/// </summary>
110+
public void InvokeApi<T>(string apiName, Method httpMethod, Action<IRestResponse<T>> callback = null) where T : new()
111+
{
112+
string uri = URI_API + apiName;
113+
ZumoRequest request = new ZumoRequest(this, uri, httpMethod);
114+
Debug.Log( httpMethod.ToString() + " custom API Request Uri: " + uri );
115+
this.ExecuteAsync(request, callback);
116+
}
117+
118+
/// <summary>
119+
/// Invokes custom API with body
120+
/// </summary>
121+
public void InvokeApi<T>(string apiName, Method httpMethod, T body, Action<IRestResponse<T>> callback = null) where T : new()
122+
{
123+
string uri = URI_API + apiName;
124+
ZumoRequest request = new ZumoRequest(this, uri, httpMethod);
125+
request.AddBody (body);
126+
Debug.Log( httpMethod.ToString() + " custom API Request Uri: " + uri );
127+
this.ExecuteAsync(request, callback);
128+
}
129+
110130
/// <summary>
111131
/// When you copy the URL is is 'http' by default, but its preferable to use 'https'
112132
/// </summary>

‎Assets/Scripts/HighscoresDemo.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -245,15 +245,15 @@ public void GetAllHighscores()
245245

246246
private void GetPageHighscores()
247247
{
248-
CustomQuery query = new CustomQuery ("", "score desc", _noPageResults, _skip, "id,username,score"); //CustomQuery.OrderBy ("score desc");
249-
_table.Query<NestedResults<Highscore>>(query, OnReadNestedResultsCompleted); //Query(query);
248+
CustomQuery query = new CustomQuery ("", "score desc", _noPageResults, _skip, "id,username,score");
249+
_table.Query<NestedResults<Highscore>>(query, OnReadNestedResultsCompleted);
250250
}
251251

252252
public void GetTopHighscores()
253253
{
254254
DateTime today = DateTime.Today;
255255
string day = today.ToString("s");
256-
string filter = string.Format("createdAt gt '{0}Z'", day); //string.Format("score gt {0}", 999);
256+
string filter = string.Format("createdAt gt '{0}Z'", day);
257257
Debug.Log ("filter:" + filter);
258258
string orderBy = "score desc";
259259
CustomQuery query = new CustomQuery(filter,orderBy,10);
@@ -263,7 +263,7 @@ public void GetTopHighscores()
263263
public void GetUsernameHighscore()
264264
{
265265
Highscore score = GetScore ();
266-
string filter = string.Format("username eq '{0}'", score.username); // string.Format("startswith(username,'{0}')", score.username);
266+
string filter = string.Format("username eq '{0}'", score.username);
267267
string orderBy = "score desc";
268268
CustomQuery query = new CustomQuery(filter,orderBy);
269269
Query(query);
@@ -308,12 +308,12 @@ private void OnLookupCompleted(IRestResponse<Highscore> response)
308308
/// </summary>
309309
public void Hello()
310310
{
311-
_client.InvokeApi<Message>("hello", OnCustomApiCompleted);
311+
_client.InvokeApi<Message>("hello", Method.GET, OnCustomApiCompleted);
312312
}
313313

314314
public void GenerateScores()
315315
{
316-
_client.InvokeApi<Message>("GenerateScores", OnCustomApiCompleted);
316+
_client.InvokeApi<Message>("GenerateScores", Method.POST, OnCustomApiCompleted);
317317
}
318318

319319
private void OnCustomApiCompleted(IRestResponse<Message> response)
@@ -472,7 +472,7 @@ public int GetNumberOfRowsForTableView(TableView tableView)
472472

473473
public float GetHeightForRowInTableView(TableView tableView, int row)
474474
{
475-
return (_cellPrefab.transform as RectTransform).rect.height; //50.0f;
475+
return (_cellPrefab.transform as RectTransform).rect.height;
476476
}
477477

478478
public TableViewCell GetCellForRowInTableView(TableView tableView, int row)

‎README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ table.insert(function (context) {
5151
## Setup Azure App Services custom APIs with **Easy APIs**
5252
With [Azure App Services](https://portal.azure.com/) you can create custom APIs using **Easy APIs**.
5353

54-
1. Create a 'hello' api to say hello! (Example Easy API message script below)
55-
2. Create a 'GenerateScores' api to generate 10 random scores. (Example Easy API query script below)
54+
1. Create a 'hello' api (using "get" method) to say hello! (Example Easy API message script below)
55+
2. Create a 'GenerateScores' api (using "post" method) to generate 10 random scores. (Example Easy API query script below)
5656

5757
#### Easy API 'hello' script (*api/hello.js*)
5858
```node
@@ -67,7 +67,7 @@ module.exports = {
6767
```node
6868
var util = require('util');
6969
module.exports = {
70-
"get": function (req, res, next) {
70+
"post": function (req, res, next) {
7171
var insert = "INSERT INTO Highscores (username,score) VALUES ";
7272
var i = 10;
7373
while (i--) {

0 commit comments

Comments
 (0)
Please sign in to comment.