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 OnGetResponse; #if !UNITY_EDITOR private string _apiUrl = "https://yandexquest.graff.tech/"; #else private string _apiUrl = "http://localhost:5000/"; #endif public async UniTask GetAsync(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(operation.downloadHandler.text); return result; } catch(Exception ex) { Debug.LogError(ex.Message); OnGetResponse?.Invoke(-1); return default; } } public async UniTask GetAsync(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(operation.downloadHandler.text); return result; } catch(Exception ex) { Debug.LogError(ex.Message); OnGetResponse?.Invoke(-1); return default; } } public async UniTask GetAsync(string requestString, Dictionary 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(operation.downloadHandler.text); return result; } catch(Exception ex) { Debug.LogError(ex.Message); OnGetResponse?.Invoke(-1); return default; } } public async UniTask PostAsync (string requestString, TValue data, Dictionary 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 PostAsync (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(operation.downloadHandler.text); } catch(Exception ex) { Debug.LogError(ex.Message); OnGetResponse?.Invoke(-1); return default; } } public async UniTask PutAsync(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); } }