You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
4.2 KiB
C#

using UnityEngine;
using Sirenix.OdinInspector;
public class APITest : MonoBehaviour
{
[SerializeField] UserData userToCreate = new();
// [Space(5)]
// [SerializeField] string updateUserApi = "http://localhost:8080/updateUser";
// [SerializeField] string updateUserId;
// [SerializeField] UserData userToUpdate = new();
// [Space(5)]
// [SerializeField] string deleteUserApi = "http://localhost:8080/deleteUser";
// [SerializeField] string deleteUserId;
// [Button]
// private async void GetUserRequest()
// {
// userData = await APIRequestHandler.GetRequest<UserData>(GameData.Instance.GetUserAPI + getUserId);
// }//GetUserRequest() end
// [Button]
// private async void GetUsersRequest()
// {
// usersData = await APIRequestHandler.GetRequestList<UserData>(getUsersApi);
// }//GetUsersRequest() end
// [Button]
// private async void UpdateUserRequest()
// {
// using UnityWebRequest webRequest = new(updateUserApi + $"/{updateUserId}", "PUT");
// JObject jsonObject = JObject.Parse(JsonConvert.SerializeObject(userToUpdate));
// // Remove the _id field
// jsonObject.Remove("_id");
// byte[] bodyRaw = System.Text.Encoding.UTF8.GetBytes(jsonObject.ToString(Formatting.None));
// // Set the request body
// webRequest.uploadHandler = new UploadHandlerRaw(bodyRaw);
// webRequest.downloadHandler = new DownloadHandlerBuffer();
// webRequest.SetRequestHeader("Content-Type", "application/json");
// Debug.LogWarning($"Requesting URL {updateUserApi}");
// webRequest.SendWebRequest();
// while(!webRequest.isDone)
// {
// // if(!Application.isPlaying)
// // return;
// await Task.Yield();
// }//loop end
// string[] pages = updateUserApi.Split('/');
// int page = pages.Length - 1;
// switch (webRequest.result)
// {
// case UnityWebRequest.Result.ConnectionError:
// case UnityWebRequest.Result.DataProcessingError:
// Debug.LogError(pages[page] + $"\n ({webRequest.responseCode}): Error: " + webRequest.error);
// break;
// case UnityWebRequest.Result.ProtocolError:
// Debug.LogError(pages[page] + $"\n ({webRequest.responseCode}): HTTP Error: " + webRequest.error);
// break;
// case UnityWebRequest.Result.Success:
// Debug.Log(pages[page] + $"\n ({webRequest.responseCode}) Received:" + webRequest.downloadHandler.text);
// updateUserId = null;
// break;
// }//switch end
// // return default;
// }//UpdateUserRequest() end
// [Button]
// private async void DeleteUserRequest()
// {
// using UnityWebRequest webRequest = new(deleteUserApi + $"/{deleteUserId}", "DELETE");
// Debug.LogWarning($"Requesting URL {deleteUserApi}");
// webRequest.downloadHandler = new DownloadHandlerBuffer();
// webRequest.SendWebRequest();
// while(!webRequest.isDone)
// {
// // if(!Application.isPlaying)
// // return;
// await Task.Yield();
// }//loop end
// string[] pages = updateUserApi.Split('/');
// int page = pages.Length - 1;
// switch (webRequest.result)
// {
// case UnityWebRequest.Result.ConnectionError:
// case UnityWebRequest.Result.DataProcessingError:
// Debug.LogError(pages[page] + $"\n ({webRequest.responseCode}): Error: " + webRequest.error);
// break;
// case UnityWebRequest.Result.ProtocolError:
// Debug.LogError(pages[page] + $"\n ({webRequest.responseCode}): HTTP Error: " + webRequest.error);
// break;
// case UnityWebRequest.Result.Success:
// Debug.Log(pages[page] + $"\n ({webRequest.responseCode}) Received:" + webRequest.downloadHandler.text);
// deleteUserId = null;
// break;
// }//switch end
// // return default;
// }//UpdateUserRequest() end
}//class end