public static IEnumerator Post <T>(RequestArguments <T> requestArguments) where T : class { var bytes = Encoding.UTF8.GetBytes(requestArguments.JsonToSend); // Way to send the raw json in unity is to use Put method and then change the method using (var www = UnityWebRequest.Put(requestArguments.Url, bytes)) { www.method = "POST"; www.SetRequestHeader("Content-Type", "application/json"); www.SetRequestHeader("Accept", "application/json"); yield return(www.SendWebRequest()); if (www.isNetworkError) { LoggingManager.Get.Log(NetworkLogType.RequestError, www); requestArguments.SendErrorToViewCallback(www.error); yield break; } if (www.isHttpError) { LoggingManager.Get.Log(NetworkLogType.RequestError, www); requestArguments.SendErrorToViewCallback(www.downloadHandler.text); yield break; } requestArguments.SendDataToServiceCallback(www.downloadHandler.text); } }
public async Task SendResponseAsync(HttpContext httpContext) { httpContext.Response.StatusCode = StatusCode; foreach (var header in Headers) { //if (httpContext.Response.Headers.ContainsKey(header.Key)) //{ httpContext.Response.Headers[header.Key] = header.Value; //} //else //{ // httpContext.Response.Headers.Add(header.Key, header.Value); //} } var routeValues = httpContext.GetRouteValues(); var arguments = new RequestArguments( new DynamicValues(routeValues), new DynamicValues(httpContext.Request.Query), httpContext.Request.Body); await httpContext.Response.WriteAsync((string)Responder(httpContext.Request, arguments)); }
private IEnumerator ProcessRequest(string endpoint, RequestArguments args, ResponseEvent onSuccess) { using (UnityWebRequest www = UnityWebRequest.Get(baseUri + endpoint + baseArgs + args)) { yield return(www.Send()); circle.SetActive(false); //if(www.isNetworkError || www.isHttpError) { if (www.isError) { onError.Invoke(www.error); } else { onSuccess.Invoke(www.downloadHandler.text); } } }
// Run a GET public void Get(string endpoint, RequestArguments args, ResponseEvent onSuccess) { circle.SetActive(true); StartCoroutine(ProcessRequest(endpoint, args, onSuccess)); }
public GeneratedUClass(Type reflectedClass) { reflectedClassType = reflectedClass; Name = reflectedClass.Name; Type = reflectedClass.GetCustomAttribute <GenerateApiAttribute>().ClassType; if (Type == UClassType.Request) { foreach (var requesDataField in reflectedClass.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) .Where(property => property.GetCustomAttributes <ApiRequestDataAttribute>(true).Any())) { RequestArguments.Add(new UProperty(requesDataField, true)); } if (reflectedClass.GetCustomAttributes <RequireServerAuthAttribute>(true).Any()) { RequestArguments.Add(new UProperty() { Name = "ServerAuthKey", Type = PropertyType.String, IsArgument = true }); if (RequestArguments.Count >= 2) { RequestArguments.Swap(0, RequestArguments.Count - 1); } } else if (reflectedClass.GetCustomAttributes <RequireAuthAttribute>(true).Any()) { if (!RequestArguments.Exists(a => a.Name.Equals("PlayerId", StringComparison.OrdinalIgnoreCase))) { RequestArguments.Add(new UProperty() { Name = "PlayerId", Type = PropertyType.String, IsArgument = true }); if (RequestArguments.Count >= 2) { RequestArguments.Swap(0, RequestArguments.Count - 1); } } RequestArguments.Add(new UProperty() { Name = "SessionToken", Type = PropertyType.String, IsArgument = true }); if (RequestArguments.Count >= 3) { RequestArguments.Swap(1, RequestArguments.Count - 1); } } foreach (var responseDataField in reflectedClass.GetFields(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) .Where(property => property.GetCustomAttributes <ApiResponseDataAttribute>(true).Any())) { Properties.Add(new UProperty(responseDataField)); } } else if (Type == UClassType.Enum) { foreach (var enumValue in reflectedClass.GetEnumNames()) { EnumValues.Add(enumValue); } } else { foreach (var reflectedProperty in reflectedClass.GetProperties(BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.Instance) .Where(property => !property.GetCustomAttributes <IgnorePropertyAttribute>(true).Any() && property.CanWrite)) { Properties.Add(new UProperty(reflectedProperty)); } } }