Untitled

 avatar
unknown
plain_text
2 years ago
3.1 kB
5
Indexable
#if !UNITY_WEBGL
        public async void SendApi<TResult>(string apiName, BaseRequest request, Action<TResult> successCallback,
            Action<int, string> errorCallback, Action timeoutCallback, bool standartAPIURL = true)
            where TResult : BaseResponse
        {
#else
        public void SendApi<TResult>(string apiName, BaseRequest request, Action<TResult> successCallback, Action<int, string> errorCallback, Action timeoutCallback, bool standartAPIURL
 = true) where TResult : BaseResponse
        {
#endif
            // check internet connection
            if (Application.internetReachability == NetworkReachability.NotReachable)
            {
                timeoutCallback();
                return;
            }

            //prepare request
            string apiUrlToPost = "";
            //if (!standartAPIURL)
            apiUrlToPost = apiName;
            //else
            //apiUrlToPost = ApiManager.Instance.ApiUrl + "/" + request.GetApiUrl(apiName);
            TDebugger.LogSDK("Request: " + request.GetHttpMethod() + " " + request.ToJson());
            TDebugger.LogSDK("API URL: " + apiUrlToPost);

            //Debug.LogSDK(request.GetRequestHeaders());
#if !UNITY_WEBGL
            //using (UnityWebRequest www = new UnityWebRequest(apiUrlToPost, UnityWebRequest.kHttpVerbPOST))
            using (UnityWebRequest www = new UnityWebRequest(apiUrlToPost, request.GetHttpMethod()))
            {
                if (www.method != UnityWebRequest.kHttpVerbGET)
                {
                    byte[] formData = System.Text.Encoding.UTF8.GetBytes(request.ToJson());
                   
                    www.uploadHandler = new UploadHandlerRaw(formData);
                }

                www.downloadHandler = new DownloadHandlerBuffer();
                www.timeout = ApiManager.Instance.TimeOut;

                foreach (KeyValuePair<string, string> i in request.GetRequestHeaders())
                {
                    www.SetRequestHeader(i.Key, i.Value);
                }
               // www.SetRequestHeader("Content-Type", "application/json");
                var asyncOp = www.SendWebRequest();

                //await until it's done: 
                while (asyncOp.isDone == false)
                {
                    await Task.Delay(1000 / 30); //30 hertz
                }


                ProcessApiResponse(apiName, successCallback, errorCallback, timeoutCallback, www);
            }
#else
            UnityWebRequest www = new UnityWebRequest(apiUrlToPost, request.GetHttpMethod());
            www.uploadHandler = new UploadHandlerRaw(formData);
            www.downloadHandler = new DownloadHandlerBuffer();
            www.timeout = LMSEngine.Instance.TimeOut;

            foreach (KeyValuePair<string, string> i in request.GetRequestHeaders())
            {
                www.SetRequestHeader(i.Key, i.Value);
            }

            LMSEngine.Instance.ProcessApiResponseCoroutine<TResult>(apiName, successCallback, errorCallback, timeoutCallback, www);
#endif
Editor is loading...