update web requset data
This commit is contained in:
@@ -7,18 +7,25 @@ using UnityEngine.Networking;
|
||||
using System.Collections.Generic;
|
||||
using Cysharp.Threading.Tasks;
|
||||
|
||||
public struct Response<T>
|
||||
{
|
||||
public long responseCode { get; set; }
|
||||
public string responseText { get; set; }
|
||||
public T responseData { get; set; }
|
||||
}
|
||||
|
||||
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)
|
||||
public async UniTask<Response<TValue>> GetAsync<TValue>(string requestString)
|
||||
{
|
||||
Response<TValue> responseStruct = new Response<TValue>();
|
||||
|
||||
using var getRequest = UnityWebRequest.Get(_apiUrl + requestString);
|
||||
getRequest.SetRequestHeader("Content-type", "application/json");
|
||||
UnityWebRequest operation = new UnityWebRequest();
|
||||
@@ -29,27 +36,33 @@ public class NetworkService
|
||||
}
|
||||
catch { }
|
||||
|
||||
OnGetResponse?.Invoke((int)getRequest.responseCode);
|
||||
responseStruct.responseCode = getRequest.responseCode;
|
||||
if(getRequest.responseCode != 200)
|
||||
{
|
||||
return default;
|
||||
responseStruct.responseText = getRequest.downloadHandler.text;
|
||||
return responseStruct;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
TValue result = JsonConvert.DeserializeObject<TValue>(operation.downloadHandler.text);
|
||||
return result;
|
||||
responseStruct.responseData = result;
|
||||
|
||||
return responseStruct;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Debug.LogError(ex.Message);
|
||||
OnGetResponse?.Invoke(-1);
|
||||
return default;
|
||||
responseStruct.responseCode = -1;
|
||||
responseStruct.responseText = ex.Message;
|
||||
|
||||
return responseStruct;
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask<TValue> GetAsync<TValue>(string requestString, params string[] query)
|
||||
public async UniTask<Response<TValue>> GetAsync<TValue>(string requestString, params string[] query)
|
||||
{
|
||||
Response<TValue> responseStruct = new Response<TValue>();
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append(requestString);
|
||||
stringBuilder.Append("?");
|
||||
@@ -72,27 +85,31 @@ public class NetworkService
|
||||
}
|
||||
catch { }
|
||||
|
||||
OnGetResponse?.Invoke((int)getRequest.responseCode);
|
||||
responseStruct.responseCode = getRequest.responseCode;
|
||||
if(getRequest.responseCode != 200)
|
||||
{
|
||||
return default;
|
||||
responseStruct.responseText = getRequest.downloadHandler.text;
|
||||
return responseStruct;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
TValue result = JsonConvert.DeserializeObject<TValue>(operation.downloadHandler.text);
|
||||
return result;
|
||||
responseStruct.responseData = result;
|
||||
return responseStruct;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Debug.LogError(ex.Message);
|
||||
OnGetResponse?.Invoke(-1);
|
||||
return default;
|
||||
responseStruct.responseCode = -1;
|
||||
responseStruct.responseText = ex.Message;
|
||||
return responseStruct;
|
||||
}
|
||||
}
|
||||
|
||||
public async UniTask<TValue> GetAsync<TValue>(string requestString, Dictionary<string, string> headers = null, params string[] query)
|
||||
public async UniTask<Response<TValue>> GetAsync<TValue>(string requestString, Dictionary<string, string> headers = null, params string[] query)
|
||||
{
|
||||
Response<TValue> responseStruct = new Response<TValue>();
|
||||
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
stringBuilder.Append(requestString);
|
||||
stringBuilder.Append("?");
|
||||
@@ -115,31 +132,34 @@ public class NetworkService
|
||||
try
|
||||
{
|
||||
operation = await getRequest.SendWebRequest();
|
||||
OnGetResponse?.Invoke((int)operation.responseCode);
|
||||
}
|
||||
catch { }
|
||||
|
||||
OnGetResponse?.Invoke((int)getRequest.responseCode);
|
||||
responseStruct.responseCode = getRequest.responseCode;
|
||||
if(getRequest.responseCode != 200)
|
||||
{
|
||||
return default;
|
||||
responseStruct.responseText = getRequest.downloadHandler.text;
|
||||
return responseStruct;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
TValue result = JsonConvert.DeserializeObject<TValue>(operation.downloadHandler.text);
|
||||
return result;
|
||||
responseStruct.responseData = result;
|
||||
|
||||
return responseStruct;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Debug.LogError(ex.Message);
|
||||
OnGetResponse?.Invoke(-1);
|
||||
responseStruct.responseCode = -1;
|
||||
responseStruct.responseText = ex.Message;
|
||||
|
||||
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);
|
||||
@@ -160,12 +180,12 @@ public class NetworkService
|
||||
operation = await postRequest.SendWebRequest();
|
||||
}
|
||||
catch { }
|
||||
|
||||
OnGetResponse?.Invoke((int)postRequest.responseCode);
|
||||
}
|
||||
|
||||
public async UniTask<TResponseValue> PostAsync<TSendingValue, TResponseValue> (string requestString, TSendingValue data)
|
||||
public async UniTask<Response<TResponseValue>> PostAsync<TSendingValue, TResponseValue> (string requestString, TSendingValue data)
|
||||
{
|
||||
Response<TResponseValue> responseStruct = new Response<TResponseValue>();
|
||||
|
||||
string sendingData = JsonConvert.SerializeObject(data);
|
||||
using var postRequest = UnityWebRequest.Post(_apiUrl + requestString, sendingData);
|
||||
byte[] dataBytes = new UTF8Encoding().GetBytes(sendingData);
|
||||
@@ -181,22 +201,26 @@ public class NetworkService
|
||||
}
|
||||
catch { }
|
||||
|
||||
OnGetResponse?.Invoke((int)postRequest.responseCode);
|
||||
if(postRequest.responseCode == 0)
|
||||
responseStruct.responseCode = postRequest.responseCode;
|
||||
if(postRequest.responseCode != 200)
|
||||
{
|
||||
return default;
|
||||
responseStruct.responseText = postRequest.downloadHandler.text;
|
||||
return responseStruct;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
return JsonConvert.DeserializeObject<TResponseValue>(operation.downloadHandler.text);
|
||||
TResponseValue result = JsonConvert.DeserializeObject<TResponseValue>(operation.downloadHandler.text);
|
||||
responseStruct.responseData = result;
|
||||
|
||||
return responseStruct;
|
||||
}
|
||||
catch(Exception ex)
|
||||
{
|
||||
Debug.LogError(ex.Message);
|
||||
OnGetResponse?.Invoke(-1);
|
||||
responseStruct.responseCode = -1;
|
||||
responseStruct.responseText = ex.Message;
|
||||
|
||||
return default;
|
||||
return responseStruct;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,7 +241,5 @@ public class NetworkService
|
||||
operation = await putRequest.SendWebRequest();
|
||||
}
|
||||
catch { }
|
||||
|
||||
OnGetResponse?.Invoke((int)putRequest.responseCode);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user