2023-04-03 11:04:31 +08:00

122 lines
3.9 KiB
C#

using System;
using System.Collections.Generic;
using Http;
using Newtonsoft.Json;
using UnityEngine;
public class UsersRequest
{
private static UsersRequest _instance;
public static UsersRequest Instance => _instance ??= new UsersRequest();
// public void SigningUp(string body, Action<string> complete)
// {
// var http = HttpManager.Instance.Post("users",
// info => { complete?.Invoke(info.State != RequestState.Success ? "" : info.Response.DataAsText); });
// http.AddRevocableSession();
// http.AddParameter(body);
// http.Send();
// }
public void Users(string body, Action<string> complete)
{
HttpDebug.Log($"logging in body:{body}");
var http = HttpManager.Instance.Post("users", info => OnResult(info, complete));
http.AddRevocableSession();
http.AddParameter(body);
http.Send();
}
public void UsersTokenLogin(string token, string body, Action<string> complete)
{
HttpDebug.Log($"token logging in body:{body}");
var http = HttpManager.Instance.Post("functions/tokenLogin", info => OnResult(info, complete));
http.AddHeader("X-Parse-Session-Token", token);
http.AddRevocableSession();
http.AddParameter(body);
http.Send();
}
public void UsersPost(string key, string body, Action<string> complete)
{
var http = HttpManager.Instance.Post($"users/{key}", info => OnResult(info, complete));
http.AddToken();
http.AddParameter(body);
http.Send();
}
public void UsersGet(string key, string body, Action<string> complete)
{
var http = HttpManager.Instance.Get($"users/{key}", info => OnResult(info, complete));
http.AddToken();
http.AddParameter(body);
http.Send();
}
public void UsersPut(string key, string body, Action<string> complete)
{
var http = HttpManager.Instance.Put($"users/{key}", info => OnResult(info, complete));
http.AddToken();
http.AddParameter(body);
http.Send();
}
public void UsersDel(string key, string body, Action<string> complete)
{
var http = HttpManager.Instance.Delete($"users/{key}", info => OnResult(info, complete));
http.AddToken();
http.AddParameter(body);
http.Send();
}
private void OnResult(ResultInfo info, Action<string> complete)
{
if (info.State == RequestState.Success)
{
complete?.Invoke(info.Response.DataAsText);
return;
}
else if (info.Response != null)
{
if (info.Response.StatusCode >= 200 || info.Response.StatusCode < 500)
{
complete?.Invoke(info.Response.DataAsText);
return;
}
}
complete?.Invoke("");
}
// public void ValidatingToken(Action<bool> complete)
// {
// var http = HttpManager.Instance.Get("users/me", info =>
// {
// if (info.State != RequestState.Success)
// {
// complete?.Invoke(false);
// return;
// }
//
// var msg = JsonConvert.DeserializeObject<Dictionary<string, object>>(info.Response.DataAsText);
// if (msg["code"] is int code && code == 209)
// {
// complete?.Invoke(false);
// return;
// }
//
// complete?.Invoke(true);
// });
// http.AddToken();
// http.Send();
// }
//
// public void GuestLogin(Action<string> complete)
// {
// var url = $"loginAs?userId={SystemInfo.deviceUniqueIdentifier}";
// var http = HttpManager.Instance.Get(url,
// info => { complete?.Invoke(info.State != RequestState.Success ? "" : info.Response.DataAsText); });
// http.AddRevocableSession();
// http.AddHeader("X-Parse-Master-Key", "1");
// http.Send();
// }
}