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