-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAzureFunction.cs
173 lines (138 loc) · 7.14 KB
/
AzureFunction.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using Azure.AppServices;
using RESTClient;
using UnityEngine;
using System.Collections;
using System;
namespace Azure.Functions {
public class AzureFunction {
private AzureFunctionClient client;
private string name; // Azure Function name
private string key; // Azure Function key
private string apiPath; // Azure Functions API path
private const string API = "api";
private const string PARAM_CODE = "code";
public AzureFunction(string name, AzureFunctionClient client, string key = null, string apiPath = API) {
this.client = client;
this.name = name;
this.key = key;
this.apiPath = apiPath;
}
public override string ToString() {
return name;
}
public IEnumerator Get<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<T>(Method.GET, callback, path, query);
}
public IEnumerator Post<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<T>(Method.POST, callback, path, query);
}
public IEnumerator Patch<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<T>(Method.PATCH, callback, path, query);
}
public IEnumerator Put<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<T>(Method.PUT, callback, path, query);
}
public IEnumerator Delete<T>(Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<T>(Method.DELETE, callback, path, query);
}
public IEnumerator Post<B, T>(B body, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<B, T>(body, Method.POST, callback, path, query);
}
public IEnumerator Patch<B, T>(B body, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<B, T>(body, Method.PATCH, callback, path, query);
}
public IEnumerator Put<B, T>(B body, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<B, T>(body, Method.PUT, callback, path, query);
}
public IEnumerator Delete<B, T>(B body, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApi<B, T>(body, Method.DELETE, callback, path, query);
}
#region Variant methods for parsing JSON arrays
public IEnumerator GetArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<T>(Method.GET, callback, path, query);
}
public IEnumerator PostArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<T>(Method.POST, callback, path, query);
}
public IEnumerator PatchArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<T>(Method.PATCH, callback, path, query);
}
public IEnumerator PutArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<T>(Method.PUT, callback, path, query);
}
public IEnumerator DeleteArray<T>(Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<T>(Method.DELETE, callback, path, query);
}
public IEnumerator PostArray<B, T>(B body, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<B, T>(body, Method.POST, callback, path, query);
}
public IEnumerator PatchArray<B, T>(B body, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<B, T>(body, Method.PATCH, callback, path, query);
}
public IEnumerator PutArray<B, T>(B body, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<B, T>(body, Method.PUT, callback, path, query);
}
public IEnumerator DeleteArray<B, T>(B body, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
yield return InvokeApiArray<B, T>(body, Method.DELETE, callback, path, query);
}
#endregion
#region Async methods for parsing JSON objects
private IEnumerator InvokeApi<T>(Method httpMethod = Method.GET, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
var request = new ZumoRequest(ApiUrl(path), httpMethod, false, client.User);
request.SetQueryParams(query);
if (!string.IsNullOrEmpty(key)) {
request.AddQueryParam(PARAM_CODE, key, true);
}
yield return request.Request.Send();
if (typeof(T) == typeof(string)) {
request.GetText(callback);
} else {
request.ParseJson<T>(callback);
}
}
private IEnumerator InvokeApi<B, T>(B body, Method httpMethod = Method.POST, Action<IRestResponse<T>> callback = null, string path = null, QueryParams query = null) {
var request = new ZumoRequest(ApiUrl(path), httpMethod, false, client.User);
request.SetQueryParams(query);
if (!string.IsNullOrEmpty(key)) {
request.AddQueryParam(PARAM_CODE, key, true);
}
if (body != null) {
request.AddBody(body);
}
yield return request.Request.Send();
if (typeof(T) == typeof(string)) {
request.GetText(callback);
} else {
request.ParseJson<T>(callback);
}
}
#endregion
#region Async methods for parsing JSON arrays
private IEnumerator InvokeApiArray<T>(Method httpMethod = Method.GET, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
var request = new ZumoRequest(ApiUrl(path), httpMethod, false, client.User);
if (!string.IsNullOrEmpty(key)) {
request.AddQueryParam(PARAM_CODE, key, true);
}
yield return request.Request.Send();
request.ParseJsonArray<T>(callback);
}
private IEnumerator InvokeApiArray<B, T>(B body, Method httpMethod = Method.POST, Action<IRestResponse<T[]>> callback = null, string path = null, QueryParams query = null) {
var request = new ZumoRequest(ApiUrl(path), httpMethod, false, client.User);
if (!string.IsNullOrEmpty(key)) {
request.AddQueryParam(PARAM_CODE, key, true);
}
if (body != null) {
request.AddBody(body);
}
yield return request.Request.Send();
request.ParseJsonArray<T>(callback);
}
#endregion
public string ApiUrl(string path = null) {
string customRoute = string.IsNullOrEmpty(path) ? "" : "/" + path;
return string.Format("{0}/{1}/{2}{3}", client.Url, apiPath, name, customRoute);
}
}
}