Files
yandex-quest/Assets/Scripts/Services/NetworkService.cs
T
2023-07-03 13:42:10 +05:00

223 lines
6.8 KiB
C#

using System;
using System.Text;
using Newtonsoft.Json;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.Networking;
using System.Collections.Generic;
using Cysharp.Threading.Tasks;
public class NetworkService
{
public event Action<int> OnGetResponse;
#if !UNITY_EDITOR
private string _apiUrl = "https://yandexquest.graff.tech/";
#else
private string _apiUrl = "http://localhost:5000/";
#endif
public async UniTask<TValue> GetAsync<TValue>(string requestString)
{
using var getRequest = UnityWebRequest.Get(_apiUrl + requestString);
getRequest.SetRequestHeader("Content-type", "application/json");
UnityWebRequest operation = new UnityWebRequest();
try
{
operation = await getRequest.SendWebRequest();
}
catch { }
OnGetResponse?.Invoke((int)getRequest.responseCode);
if(getRequest.responseCode != 200)
{
return default;
}
try
{
TValue result = JsonConvert.DeserializeObject<TValue>(operation.downloadHandler.text);
return result;
}
catch(Exception ex)
{
Debug.LogError(ex.Message);
OnGetResponse?.Invoke(-1);
return default;
}
}
public async UniTask<TValue> GetAsync<TValue>(string requestString, params string[] query)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(requestString);
stringBuilder.Append("?");
for (int i = 0; i < query.Length; i++)
{
stringBuilder.Append(query[i]);
if (i != query.Length - 1)
stringBuilder.Append("&");
}
using var getRequest = UnityWebRequest.Get(_apiUrl + stringBuilder.ToString());
getRequest.SetRequestHeader("Content-type", "application/json");
UnityWebRequest operation = new UnityWebRequest();
try
{
operation = await getRequest.SendWebRequest();
}
catch { }
OnGetResponse?.Invoke((int)getRequest.responseCode);
if(getRequest.responseCode != 200)
{
return default;
}
try
{
TValue result = JsonConvert.DeserializeObject<TValue>(operation.downloadHandler.text);
return result;
}
catch(Exception ex)
{
Debug.LogError(ex.Message);
OnGetResponse?.Invoke(-1);
return default;
}
}
public async UniTask<TValue> GetAsync<TValue>(string requestString, Dictionary<string, string> headers = null, params string[] query)
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(requestString);
stringBuilder.Append("?");
for (int i = 0; i < query.Length; i++)
{
stringBuilder.Append(query[i]);
if (i != query.Length - 1)
stringBuilder.Append("&");
}
using var getRequest = UnityWebRequest.Get(_apiUrl + stringBuilder.ToString());
if(headers != null)
foreach(var header in headers)
getRequest.SetRequestHeader(header.Key, header.Value);
getRequest.SetRequestHeader("Content-type", "application/json");
UnityWebRequest operation = new UnityWebRequest();
try
{
operation = await getRequest.SendWebRequest();
OnGetResponse?.Invoke((int)operation.responseCode);
}
catch { }
OnGetResponse?.Invoke((int)getRequest.responseCode);
if(getRequest.responseCode != 200)
{
return default;
}
try
{
TValue result = JsonConvert.DeserializeObject<TValue>(operation.downloadHandler.text);
return result;
}
catch(Exception ex)
{
Debug.LogError(ex.Message);
OnGetResponse?.Invoke(-1);
return default;
}
}
public async UniTask PostAsync<TValue> (string requestString, TValue data, Dictionary<string, string> headers = null)
{
string sendingData = JsonConvert.SerializeObject(data);
using var postRequest = UnityWebRequest.Post(_apiUrl + requestString, sendingData);
byte[] dataBytes = new UTF8Encoding().GetBytes(sendingData);
postRequest.uploadHandler = new UploadHandlerRaw(dataBytes);
postRequest.downloadHandler = new DownloadHandlerBuffer();
if(headers != null)
{
foreach(var header in headers)
postRequest.SetRequestHeader(header.Key, header.Value);
}
postRequest.SetRequestHeader("Content-Type", "application/json");
UnityWebRequest operation = new UnityWebRequest();
try
{
operation = await postRequest.SendWebRequest();
}
catch { }
OnGetResponse?.Invoke((int)postRequest.responseCode);
}
public async UniTask<TResponseValue> PostAsync<TSendingValue, TResponseValue> (string requestString, TSendingValue data)
{
string sendingData = JsonConvert.SerializeObject(data);
using var postRequest = UnityWebRequest.Post(_apiUrl + requestString, sendingData);
byte[] dataBytes = new UTF8Encoding().GetBytes(sendingData);
postRequest.uploadHandler = new UploadHandlerRaw(dataBytes);
postRequest.downloadHandler = new DownloadHandlerBuffer();
postRequest.SetRequestHeader("Content-Type", "application/json");
UnityWebRequest operation = new UnityWebRequest();
try
{
operation = await postRequest.SendWebRequest();
}
catch { }
OnGetResponse?.Invoke((int)postRequest.responseCode);
if(postRequest.responseCode == 0)
{
return default;
}
try
{
return JsonConvert.DeserializeObject<TResponseValue>(operation.downloadHandler.text);
}
catch(Exception ex)
{
Debug.LogError(ex.Message);
OnGetResponse?.Invoke(-1);
return default;
}
}
public async UniTask PutAsync<TValue>(string requestString, TValue data)
{
string sendingData = JsonConvert.SerializeObject(data);
using var putRequest = UnityWebRequest.Put(_apiUrl + requestString, sendingData);
byte[] dataBytes = new UTF8Encoding().GetBytes(sendingData);
putRequest.uploadHandler = new UploadHandlerRaw(dataBytes);
putRequest.downloadHandler = new DownloadHandlerBuffer();
putRequest.SetRequestHeader("Content-Type", "application/json");
UnityWebRequest operation = new UnityWebRequest();
try
{
operation = await putRequest.SendWebRequest();
}
catch { }
OnGetResponse?.Invoke((int)putRequest.responseCode);
}
}