Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Game.Model;
- using Game.Utils;
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- using UnityEngine.Events;
- using UnityEngine.Networking;
- namespace Game.Core
- {
- public enum RequestType
- {
- GET = 1,
- POST = 2,
- TEXTURE = 3,
- JSON = 4,
- POST_JSON = 5,
- }
- public class RequestsController : MonoBehaviour
- {
- private void Awake()
- {
- GameInit.Instance.RequestsController = this;
- }
- public void CreateRequest(string url, RequestType requestType, Dictionary<string, string> data, UnityAction<object> onSuccess, UnityAction<int, string> onError, string jsonData = null)
- {
- UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
- switch (requestType)
- {
- case RequestType.GET:
- string getParams = CreateGetParams(data);
- string requestUrl = string.Format("{0}{1}", url, getParams);
- unityWebRequest = UnityWebRequest.Get(requestUrl);
- break;
- case RequestType.POST:
- unityWebRequest = UnityWebRequest.Post(url, data);
- break;
- case RequestType.TEXTURE:
- unityWebRequest.downloadHandler = new DownloadHandlerTexture();
- unityWebRequest.certificateHandler = new ManualCertificateHandler();
- break;
- case RequestType.JSON:
- unityWebRequest.certificateHandler = new ManualCertificateHandler();
- break;
- case RequestType.POST_JSON:
- unityWebRequest = new UnityWebRequest(url, "POST");
- byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
- unityWebRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
- unityWebRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
- unityWebRequest.SetRequestHeader("Content-Type", "application/json");
- break;
- }
- if (GameData.BackendConfig.timeoutLimit != 0)
- unityWebRequest.timeout = GameData.BackendConfig.timeoutLimit;
- StartCoroutine(DoRequest(unityWebRequest, onSuccess, onError));
- }
- private string CreateGetParams(Dictionary<string, string> paramsData)
- {
- if (paramsData == null)
- return string.Empty;
- string getParams = "?";
- foreach (KeyValuePair<string, string> p in paramsData)
- {
- getParams += "&" + p.Key + "=" + p.Value;
- }
- return getParams;
- }
- private IEnumerator DoRequest(UnityWebRequest unityWebRequest, UnityAction<object> onSuccess, UnityAction<int, string> onError)
- {
- if (GameData.BackendConfig.verboseRequests)
- Debug.LogFormat("Sending request to address: {0}", unityWebRequest.url);
- UnityWebRequestAsyncOperation request = unityWebRequest.SendWebRequest();
- yield return new WaitUntil(() => request.isDone);
- if (unityWebRequest.result != UnityWebRequest.Result.Success)
- {
- Debug.LogFormat("Error on request to url {0}. Response Code: {1}, Content: {2}.",
- unityWebRequest.url,
- unityWebRequest.responseCode,
- unityWebRequest.downloadHandler.text);
- OnError((int)unityWebRequest.responseCode, unityWebRequest.downloadHandler.text, onError);
- }
- else
- {
- if (unityWebRequest.downloadHandler is DownloadHandlerTexture)
- {
- DownloadHandlerTexture downloadHandler = unityWebRequest.downloadHandler as DownloadHandlerTexture;
- onSuccess?.Invoke(downloadHandler.texture);
- }
- else if (unityWebRequest.downloadHandler is DownloadHandlerAssetBundle)
- {
- DownloadHandlerAssetBundle downloadHandler = unityWebRequest.downloadHandler as DownloadHandlerAssetBundle;
- onSuccess?.Invoke(downloadHandler.assetBundle);
- }
- else
- {
- onSuccess?.Invoke(unityWebRequest.downloadHandler.text);
- }
- }
- }
- private void OnError(int responseCode, string message, UnityAction<int, string> onError)
- {
- try
- {
- onError?.Invoke(responseCode, message);
- }
- catch (Exception)
- {
- onError?.Invoke(responseCode, message);
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment