139 lines
4.7 KiB
C#
139 lines
4.7 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Text;
|
|
using BestHTTP;
|
|
using UnityEngine;
|
|
|
|
namespace Http
|
|
{
|
|
public class HttpManager : MonoBehaviour
|
|
{
|
|
private readonly Dictionary<string, HttpInfo> _requests = new Dictionary<string, HttpInfo>();
|
|
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<HttpManager>();
|
|
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;
|
|
private string _parseClientPlatform = string.Empty;
|
|
private string _parseClientVersion = 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 void SetParseClientPlatform(string key) => _parseClientPlatform = key;
|
|
public void SetParseClientVersion(string key) => _parseClientVersion = 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<ResultInfo> 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)
|
|
.AddHeader("platform", _parseClientPlatform)
|
|
.AddHeader("version", _parseClientVersion);
|
|
return http;
|
|
}
|
|
|
|
public SingleHttp Get(string url, Action<ResultInfo> complete) =>
|
|
Create(HTTPMethods.Get, url, complete);
|
|
public SingleHttp Post(string url, Action<ResultInfo> complete)
|
|
{
|
|
var http = Create(HTTPMethods.Post, url, complete);
|
|
http.AddHeader("Content-Type", "application/json");
|
|
return http;
|
|
}
|
|
|
|
public SingleHttp Put(string url, Action<ResultInfo> complete)
|
|
{
|
|
var http = Create(HTTPMethods.Put, url, complete);
|
|
http.AddHeader("Content-Type", "application/json");
|
|
return http;
|
|
}
|
|
|
|
public SingleHttp Delete(string url, Action<ResultInfo> 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<ResultInfo> 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)
|
|
{
|
|
if (!string.IsNullOrEmpty(HttpManager.Instance._parseRevocableSession))
|
|
{
|
|
self.AddHeader("X-Parse-Revocable-Session", HttpManager.Instance._parseRevocableSession);
|
|
}
|
|
}
|
|
}
|
|
} |