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 struct Response { public long responseCode { get; set; } public string responseText { get; set; } public T responseData { get; set; } } public class NetworkService { #if !UNITY_EDITOR private string _apiUrl = "https://yandexquest.graff.tech/"; #else private string _apiUrl = "http://localhost:5000/"; #endif public async UniTask> GetAsync(string requestString) { Response responseStruct = new Response(); using var getRequest = UnityWebRequest.Get(_apiUrl + requestString); getRequest.SetRequestHeader("Content-type", "application/json"); UnityWebRequest operation = new UnityWebRequest(); try { operation = await getRequest.SendWebRequest(); } catch { } responseStruct.responseCode = getRequest.responseCode; if (getRequest.responseCode != 200) { responseStruct.responseText = getRequest.downloadHandler.text; return responseStruct; } try { TValue result = JsonConvert.DeserializeObject(operation.downloadHandler.text); responseStruct.responseData = result; return responseStruct; } catch (Exception ex) { responseStruct.responseCode = -1; responseStruct.responseText = ex.Message; return responseStruct; } } public async UniTask> GetAsync(string requestString, params string[] query) { Response responseStruct = new Response(); 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 { } responseStruct.responseCode = getRequest.responseCode; if (getRequest.responseCode != 200) { responseStruct.responseText = getRequest.downloadHandler.text; return responseStruct; } try { TValue result = JsonConvert.DeserializeObject(operation.downloadHandler.text); responseStruct.responseData = result; return responseStruct; } catch (Exception ex) { responseStruct.responseCode = -1; responseStruct.responseText = ex.Message; return responseStruct; } } public async UniTask> GetAsync(string requestString, Dictionary headers = null, params string[] query) { Response responseStruct = new Response(); 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(); } catch { } responseStruct.responseCode = getRequest.responseCode; if (getRequest.responseCode != 200) { responseStruct.responseText = getRequest.downloadHandler.text; return responseStruct; } try { TValue result = JsonConvert.DeserializeObject(operation.downloadHandler.text); responseStruct.responseData = result; return responseStruct; } catch (Exception ex) { responseStruct.responseCode = -1; responseStruct.responseText = ex.Message; return default; } } public async UniTask PostAsync(string requestString, TValue data, Dictionary headers = null) { string sendingData = JsonConvert.SerializeObject(data); using var postRequest = new UnityWebRequest(_apiUrl + requestString, "POST"); byte[] dataBytes = new UTF8Encoding().GetBytes(sendingData); postRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(dataBytes); postRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); postRequest.SetRequestHeader("Content-Type", "application/json"); if (headers != null) { foreach (var header in headers) postRequest.SetRequestHeader(header.Key, header.Value); } UnityWebRequest operation = new UnityWebRequest(); try { operation = await postRequest.SendWebRequest(); } catch { } } public async UniTask> PostAsync(string requestString, TSendingValue data) { Response responseStruct = new Response(); string sendingData = JsonConvert.SerializeObject(data); using var postRequest = new UnityWebRequest(_apiUrl + requestString, "POST"); byte[] dataBytes = new UTF8Encoding().GetBytes(sendingData); postRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(dataBytes); postRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer(); postRequest.SetRequestHeader("Content-Type", "application/json"); UnityWebRequest operation = new UnityWebRequest(); try { operation = await postRequest.SendWebRequest(); } catch { } responseStruct.responseCode = postRequest.responseCode; if (postRequest.responseCode != 200) { responseStruct.responseText = postRequest.downloadHandler.text; return responseStruct; } try { TResponseValue result = JsonConvert.DeserializeObject(operation.downloadHandler.text); responseStruct.responseData = result; return responseStruct; } catch (Exception ex) { responseStruct.responseCode = -1; responseStruct.responseText = ex.Message; return responseStruct; } } 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 { } } }