Example #1
0
 static void ResponseFailure(RaqnRequest _req, string _error)
 {
     if (_req.OnError != null)
     {
         _req.OnError(_error);
     }
 }
Example #2
0
        public IEnumerator SendRequest(RaqnRequest _req)
        {
            if (auth != null)
            {
                _req.SetAuth(auth);
            }
            //_req.OnSuccess += this.UpdateFromResponse;
            string _url = endpoint + _req.GetUri();

            if (debug)
            {
                Debug.Log("Making Api Request to Url = " + _url + "\n" + _req.GetSerializedBody(true));
            }
            string _str = _req.GetSerializedBody();

            byte[]           _body    = Encoding.UTF8.GetBytes(_str);
            UnityWebRequest  _web_req = new UnityWebRequest(_url, UnityWebRequest.kHttpVerbPOST);
            UploadHandlerRaw _uh      = new UploadHandlerRaw(_body);

            _uh.contentType          = "application/json";
            _web_req.uploadHandler   = _uh;
            _web_req.downloadHandler = new DownloadHandlerBuffer();
            _web_req.SetRequestHeader("Content-Type", "application/json");

            yield return(_web_req.Send());

            if (_web_req.isNetworkError)
            {
                ResponseFailure(_req, "LOCAL_ERR_CONNECTION");
                yield break;
            }

            string _json = _web_req.downloadHandler.text;

            if (debug)
            {
                Debug.Log("Received response. Returned " + _json);
            }

            RaqnResponse _res = RaqnResponse.FromJson(_json);

            if (_res == null)
            {
                ResponseFailure(_req, "LOCAL_ERR_PARSING");
                yield break;
            }

            _UpdateFromResponse(_res);
            if (ResponseStatus.IsError(_res.status))
            {
                if (_req.OnError != null)
                {
                    string _err_code = _res.status;
                    if (_res.GetMessageCode() != "")
                    {
                        _err_code = _res.GetMessageCode();
                    }
                    _req.OnError(_err_code);
                }
            }
            else if (_req.OnSuccess != null)
            {
                _req.OnSuccess(_res);
            }
        }