using System; using System.Collections.Generic; using System.Text; using BestHTTP; using UnityEngine; namespace Http { public class HttpManager : MonoBehaviour { private readonly Dictionary _requests = new Dictionary(); private readonly StringBuilder _stringBuilder = new StringBuilder(); #region instance private static HttpManager _instance; public static HttpManager Instance { get { if (_instance) return _instance; var obj = new GameObject(nameof(HttpManager)); _instance = obj.AddComponent(); DontDestroyOnLoad(obj); return _instance; } } #endregion private string _rootUrl = string.Empty; internal string _token = string.Empty; internal string _parseRevocableSession = string.Empty; private string _parseApplicationId = string.Empty; private string _parseClientKey = string.Empty; #region set get public void SetServerUrl(string url) => _rootUrl = url; public void SetToken(string token) => _token = token; public void SetParseRevocableSession(string key) => _parseRevocableSession = key; public void SetParseApplicationId(string appId) => _parseApplicationId = appId; public void SetParseClientKey(string key) => _parseClientKey = key; public SingleHttp Get(string id) => !_requests.TryGetValue(id, out var httpInfo) ? null : httpInfo.Http; #endregion #region create private SingleHttp Create(HTTPMethods type, string url, Action complete) { var http = SingleHttp.Create(type, _stringBuilder.Clear().Append(_rootUrl).Append(url).ToString(), OnResult); _requests.Add(http.Id, new HttpInfo { Http = http, Complete = complete, }); http.AddHeader("X-Parse-Application-Id", _parseApplicationId) .AddHeader("X-Parse-Client-Key", _parseClientKey); return http; } public SingleHttp Get(string url, Action complete) => Create(HTTPMethods.Get, url, complete); public SingleHttp Post(string url, Action complete) { var http = Create(HTTPMethods.Post, url, complete); http.AddHeader("Content-Type", "application/json"); return http; } public SingleHttp Put(string url, Action complete) { var http = Create(HTTPMethods.Put, url, complete); http.AddHeader("Content-Type", "application/json"); return http; } public SingleHttp Delete(string url, Action complete) { var http = Create(HTTPMethods.Delete, url, complete); http.AddHeader("Content-Type", "application/json"); return http; } #endregion private void OnResult(ResultInfo resultInfo) { if (!_requests.TryGetValue(resultInfo.Id, out var httpInfo)) return; httpInfo.Complete?.Invoke(resultInfo); _requests.Remove(resultInfo.Id); } public void Abort(string id) { if(!_requests.TryGetValue(id, out var httpInfo)) return; _requests.Remove(id); httpInfo.Http.ForceAbort(); } #region Class private class HttpInfo { public SingleHttp Http; public Action Complete; } #endregion } public static class SingleHttpExtends { public static void AddToken(this SingleHttp self) { if (!string.IsNullOrEmpty(HttpManager.Instance._token)) { self.AddHeader("X-Parse-Session-Token", HttpManager.Instance._token); } } public static void AddRevocableSession(this SingleHttp self) { self.AddHeader("X-Parse-Revocable-Session", HttpManager.Instance._parseRevocableSession); } } }