Rodlaiz

Untitled

Mar 25th, 2022
1,356
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.90 KB | None | 0 0
  1. using Game.Model;
  2. using Game.Utils;
  3. using System;
  4. using System.Collections;
  5. using System.Collections.Generic;
  6. using System.Text;
  7. using UnityEngine;
  8. using UnityEngine.Events;
  9. using UnityEngine.Networking;
  10.  
  11. namespace Game.Core
  12. {
  13.     public enum RequestType
  14.     {
  15.         GET = 1,
  16.         POST = 2,
  17.         TEXTURE = 3,
  18.         JSON = 4,
  19.         POST_JSON = 5,
  20.     }
  21.  
  22.     public class RequestsController : MonoBehaviour
  23.     {
  24.  
  25.  
  26.         private void Awake()
  27.         {
  28.             GameInit.Instance.RequestsController = this;
  29.         }
  30.  
  31.         public void CreateRequest(string url, RequestType requestType, Dictionary<string, string> data, UnityAction<object> onSuccess, UnityAction<int, string> onError, string jsonData = null)
  32.         {
  33.             UnityWebRequest unityWebRequest = UnityWebRequest.Get(url);
  34.  
  35.             switch (requestType)
  36.             {
  37.                 case RequestType.GET:
  38.                     string getParams = CreateGetParams(data);
  39.                     string requestUrl = string.Format("{0}{1}", url, getParams);
  40.                     unityWebRequest = UnityWebRequest.Get(requestUrl);
  41.                     break;
  42.  
  43.                 case RequestType.POST:
  44.                     unityWebRequest = UnityWebRequest.Post(url, data);
  45.                     break;
  46.  
  47.                 case RequestType.TEXTURE:
  48.                     unityWebRequest.downloadHandler = new DownloadHandlerTexture();
  49.                     unityWebRequest.certificateHandler = new ManualCertificateHandler();
  50.                     break;
  51.  
  52.                 case RequestType.JSON:
  53.                     unityWebRequest.certificateHandler = new ManualCertificateHandler();                    
  54.                     break;
  55.  
  56.                 case RequestType.POST_JSON:
  57.                     unityWebRequest = new UnityWebRequest(url, "POST");
  58.                     byte[] bodyRaw = Encoding.UTF8.GetBytes(jsonData);
  59.                     unityWebRequest.uploadHandler = (UploadHandler)new UploadHandlerRaw(bodyRaw);
  60.                     unityWebRequest.downloadHandler = (DownloadHandler)new DownloadHandlerBuffer();
  61.                     unityWebRequest.SetRequestHeader("Content-Type", "application/json");
  62.                     break;
  63.             }
  64.  
  65.             if (GameData.BackendConfig.timeoutLimit != 0)
  66.                 unityWebRequest.timeout = GameData.BackendConfig.timeoutLimit;
  67.  
  68.             StartCoroutine(DoRequest(unityWebRequest, onSuccess, onError));
  69.         }
  70.  
  71.         private string CreateGetParams(Dictionary<string, string> paramsData)
  72.         {
  73.             if (paramsData == null)
  74.                 return string.Empty;
  75.  
  76.             string getParams = "?";
  77.             foreach (KeyValuePair<string, string> p in paramsData)
  78.             {
  79.                 getParams += "&" + p.Key + "=" + p.Value;
  80.             }
  81.             return getParams;
  82.         }
  83.  
  84.         private IEnumerator DoRequest(UnityWebRequest unityWebRequest, UnityAction<object> onSuccess, UnityAction<int, string> onError)
  85.         {
  86.             if (GameData.BackendConfig.verboseRequests)
  87.                 Debug.LogFormat("Sending request to address: {0}", unityWebRequest.url);
  88.  
  89.             UnityWebRequestAsyncOperation request = unityWebRequest.SendWebRequest();
  90.             yield return new WaitUntil(() => request.isDone);
  91.  
  92.             if (unityWebRequest.result != UnityWebRequest.Result.Success)
  93.             {
  94.                 Debug.LogFormat("Error on request to url {0}. Response Code: {1}, Content: {2}.",
  95.                         unityWebRequest.url,
  96.                         unityWebRequest.responseCode,
  97.                         unityWebRequest.downloadHandler.text);
  98.  
  99.                 OnError((int)unityWebRequest.responseCode, unityWebRequest.downloadHandler.text, onError);
  100.             }
  101.             else
  102.             {
  103.                 if (unityWebRequest.downloadHandler is DownloadHandlerTexture)
  104.                 {
  105.                     DownloadHandlerTexture downloadHandler = unityWebRequest.downloadHandler as DownloadHandlerTexture;
  106.                     onSuccess?.Invoke(downloadHandler.texture);
  107.                 }
  108.                 else if (unityWebRequest.downloadHandler is DownloadHandlerAssetBundle)
  109.                 {
  110.                     DownloadHandlerAssetBundle downloadHandler = unityWebRequest.downloadHandler as DownloadHandlerAssetBundle;
  111.                     onSuccess?.Invoke(downloadHandler.assetBundle);
  112.                 }
  113.                 else
  114.                 {
  115.                     onSuccess?.Invoke(unityWebRequest.downloadHandler.text);
  116.                 }
  117.             }
  118.         }
  119.  
  120.         private void OnError(int responseCode, string message, UnityAction<int, string> onError)
  121.         {
  122.             try
  123.             {
  124.                 onError?.Invoke(responseCode, message);
  125.             }
  126.             catch (Exception)
  127.             {
  128.                 onError?.Invoke(responseCode, message);
  129.             }
  130.         }
  131.     }
  132. }
Advertisement
Add Comment
Please, Sign In to add comment