using System; using System.Collections.Generic; using System.Text; using System.Text.RegularExpressions; using SUISS.Cloud.Boomlagoon.JSON; using UnityEngine; namespace SUISS.Cloud { public class ApiRequest : CustomYieldInstruction { private ApiRequest(string call) { this._request = new WWW(call); } private ApiRequest(string call, string json) { Dictionary headers = new Dictionary { { "Content-Type", "application/json" } }; UTF8Encoding utf8Encoding = new UTF8Encoding(); byte[] bytes = utf8Encoding.GetBytes(json); this._request = new WWW(call, bytes, headers); } public static ApiRequest Post(string call, JSONValue json) { return new ApiRequest(call, json.ToString()); } public static ApiRequest Post(string call, IDictionary json) { return new ApiRequest(call, Json.Encode(json)); } public static ApiRequest Post(string call, JSONValue json, string v, string app) { call = string.Concat(new string[] { call, (!call.Contains("?")) ? "?" : "&", "v=", v, "&app=", app }); return new ApiRequest(call, json.ToString()); } public static ApiRequest Post(string call, Dictionary json, string v, string app) { call = string.Concat(new string[] { call, (!call.Contains("?")) ? "?" : "&", "v=", v, "&app=", app }); return new ApiRequest(call, Json.Encode(json)); } public static ApiRequest Get(string call) { return new ApiRequest(call); } public static ApiRequest Get(string call, string v, string app) { call = string.Concat(new string[] { call, (!call.Contains("?")) ? "?" : "&", "v=", v, "&app=", app }); return new ApiRequest(call); } public bool IsDone { get { this.ProcessResults(); return this._resultsProcessed; } } public ApiResponse Result { get { this.ProcessResults(); return this._result; } } public ApiError Error { get { this.ProcessResults(); return this._error; } } public int HttpStatus { get { if (this._request.isDone) { if (this._request.responseHeaders.ContainsKey("STATUS")) { Match match = Regex.Match(this._request.responseHeaders["STATUS"], "HTTP\\/\\d.\\d\\s(\\d{1,3})"); if (match.Success) { return int.Parse(match.Groups[1].Value); } } return 0; } return -1; } } public override bool keepWaiting { get { return !this._request.isDone; } } private void ProcessResults() { if (this._request.isDone && !this._resultsProcessed) { int httpStatus = this.HttpStatus; if (httpStatus <= 0) { this._error = new ApiError { Status = 0, Error = (this._request.error ?? "unknown error"), ErrorCode = 0, ServerError = false, NoInternet = true }; } else if (200 <= httpStatus && httpStatus < 300 && !string.IsNullOrEmpty(this._request.text)) { bool flag = false; object obj = Json.Decode(this._request.text, ref flag); if (!flag) { this._error = new ApiError { Status = 0, Error = "server returned invalid json", ErrorCode = 0, ServerError = true, NoInternet = false }; } else { Dictionary dictionary = obj as Dictionary; if (dictionary != null && dictionary.ContainsKey("code") && dictionary.ContainsKey("message") && dictionary.ContainsKey("error")) { this._error = new ApiError { Status = this.GetJsonInt(obj, "code", 0), Error = this.GetJsonString(obj, "message", string.Empty), ErrorCode = this.GetJsonInt(obj, "error", 0), ServerError = false, NoInternet = false }; } else { this._result = new ApiResponse(obj); } } } else { this._error = new ApiError { Status = httpStatus, Error = (string.IsNullOrEmpty(this._request.text) ? "server returned nothing" : this._request.text), ErrorCode = 0, ServerError = true, NoInternet = false }; } this._resultsProcessed = true; } } private string GetJsonString(object json, string key, string defaultValue) { if (json is Dictionary) { Dictionary dictionary = (Dictionary)json; if (dictionary.ContainsKey(key) && dictionary[key] is string) { return (string)dictionary[key]; } } return defaultValue; } private int GetJsonInt(object json, string key, int defaultValue) { if (json is Dictionary) { Dictionary dictionary = (Dictionary)json; if (dictionary.ContainsKey(key) && dictionary[key] is double) { return (int)((double)dictionary[key]); } } return defaultValue; } private WWW _request; private ApiError _error; private ApiResponse _result; private bool _resultsProcessed; } }