138 lines
5.0 KiB
C#
138 lines
5.0 KiB
C#
using Best.HTTP;
|
|
using Newtonsoft.Json;
|
|
using Newtonsoft.Json.Linq;
|
|
using Studio.Auth;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Net.Http;
|
|
using System.Security.Policy;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using UnityEngine;
|
|
|
|
namespace Studio.Conifg
|
|
{
|
|
public class RestAPI
|
|
{
|
|
private static Dictionary<string, string> headers = new();
|
|
public static async Task<T> RequestPost<T>(string url, Dictionary<string, object> body = null, bool useAuth = true)
|
|
{
|
|
return await Request<T>(url, body, HTTPMethods.Post, useAuth);
|
|
}
|
|
public static async Task<T> RequestGet<T>(string url, Dictionary<string, object> body = null, bool useAuth = true)
|
|
{
|
|
return await Request<T>(url, body, HTTPMethods.Get, useAuth);
|
|
}
|
|
private static async Task<T> Request<T>(string url, object body = null, HTTPMethods method = HTTPMethods.Post, bool useAuth = true)
|
|
{
|
|
string bodyString = "";
|
|
if (body != null)
|
|
{
|
|
if (body is string)
|
|
{
|
|
bodyString = body.ToString();
|
|
}
|
|
else if (body is Dictionary<string, object>)
|
|
{
|
|
bodyString = JsonUtility.ToJson(body);
|
|
}
|
|
}
|
|
// url = $"http://{ConfigConnected.APIDomain}:{ConfigConnected.APIPort}{url}";
|
|
|
|
var request = SelectHTTPRequest(method, url);
|
|
//
|
|
string requestLine = $"{method} {url} HTTP/1.1\r\n";
|
|
int requestLineSize = Encoding.UTF8.GetBytes(requestLine).Length;
|
|
|
|
request.DownloadSettings = new Best.HTTP.Request.Settings.DownloadSettings() { ContentStreamMaxBuffered = 1024 * 1024 * 200 };
|
|
request.MethodType = method;
|
|
headers.Clear();
|
|
AddHeader(request, "Content-Type", "application/json; charset=utf-8");
|
|
|
|
if (useAuth) AddHeader(request,"access-token", AuthService.Instance.Entiti.accessToken);
|
|
|
|
var bodySize = 0;
|
|
if (body != null)
|
|
{
|
|
if (body is string)
|
|
{
|
|
if(!string.IsNullOrEmpty(body as string))
|
|
{
|
|
request.UploadSettings.UploadStream =
|
|
new MemoryStream(Encoding.UTF8.GetBytes(body as string));
|
|
bodySize = Encoding.UTF8.GetBytes(body as string).Length;
|
|
}
|
|
}
|
|
else if (body is Dictionary<string, object>)
|
|
{
|
|
var stringJson = JsonConvert.SerializeObject(body);
|
|
request.UploadSettings.UploadStream =
|
|
new MemoryStream(Encoding.UTF8.GetBytes(stringJson));
|
|
bodySize = Encoding.UTF8.GetBytes(stringJson).Length;
|
|
}
|
|
}
|
|
var packetSize = Size(url, method);
|
|
packetSize += bodySize+ requestLineSize;
|
|
var response = await request.GetAsStringAsync();
|
|
headers.Clear();
|
|
response = RemoveUnnecessary(response);
|
|
JsonTextReader reader = new JsonTextReader(new StringReader(response));
|
|
//ReadJson<T>(JsonConvert.DeserializeObject<T>(response));
|
|
//response.Replace("{", "{" + $"requestsize{packetSize},");
|
|
return JsonConvert.DeserializeObject<T>(response);
|
|
}
|
|
|
|
private static string RemoveUnnecessary(string respone)
|
|
{
|
|
var result = respone.Replace("\\", "");
|
|
return result;
|
|
}
|
|
|
|
private static void AddHeader(HTTPRequest request,string key,string value)
|
|
{
|
|
request.SetHeader(key, value);
|
|
headers.Add(key, value);
|
|
}
|
|
|
|
private static int Size(string url, HTTPMethods method)
|
|
{
|
|
int packetSize = 0;
|
|
|
|
// (1) Request Line
|
|
Uri uri = new Uri(url);
|
|
string requestLine = $"{method} {uri.PathAndQuery}";
|
|
packetSize += Encoding.UTF8.GetByteCount(requestLine);
|
|
|
|
// (2) Headers
|
|
foreach (var header in headers)
|
|
{
|
|
string headerLine = $"{header.Key}: {header.Value}\r\n";
|
|
packetSize += Encoding.UTF8.GetByteCount(headerLine);
|
|
}
|
|
|
|
packetSize += 2; // 헤더 끝의 "\r\n"
|
|
|
|
return packetSize;
|
|
}
|
|
|
|
private static HTTPRequest SelectHTTPRequest(HTTPMethods methods, string url, OnRequestFinishedDelegate onRequest = null)
|
|
{
|
|
switch (methods)
|
|
{
|
|
case HTTPMethods.Get:
|
|
|
|
if (onRequest != null)
|
|
return HTTPRequest.CreateGet(url, onRequest);
|
|
else
|
|
return HTTPRequest.CreateGet(url);
|
|
case HTTPMethods.Post:
|
|
return HTTPRequest.CreatePost(url);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
}
|
|
}
|