Пример #1
0
 public Petition(string url, SendMethod method = SendMethod.Get)
 {
     Url     = url;
     Headers = new Dictionary <string, string>();
     Method  = method;
     Error   = null;
 }
Пример #2
0
 public void OnResponse(JSONObject response, NetworkError error)
 {
     if (error == null)
     {
         error = ValidateResponse(response);
     }
     _onResponseReceived?.Invoke(response, error);
 }
Пример #3
0
        public void ParseResponse(string data, NetworkError receivedError)
        {
            NetworkError error          = receivedError;
            JSONObject   parsedResponse = error == null ? null : new JSONObject();

            if (error == null)
            {
                try
                {
                    parsedResponse = JSON.JSON.LoadString(data) as JSONObject;
                }
                catch (Exception gotcha)
                {
                    error          = new NetworkError(31337, gotcha.Message);
                    parsedResponse = new JSONObject();
                }
            }
            if (error != null)
            {
                using (var itr = _commands.GetEnumerator())
                {
                    while (itr.MoveNext())
                    {
                        itr.Current.Value.OnResponse(parsedResponse, error);
                    }
                }
                return;
            }
            List <string> served = new List <string>();

            using (var itr = parsedResponse.GetEnumerator())
            {
                while (itr.MoveNext())
                {
                    if (_commands.ContainsKey(itr.Current.Key))
                    {
                        _commands[itr.Current.Key].OnResponse(itr.Current.Value.AsObject(), error);
                        served.Add(itr.Current.Key);
                    }
                }
            }
            using (var itr = _commands.GetEnumerator())
            {
                while (itr.MoveNext())
                {
                    if (!served.Contains(itr.Current.Key))
                    {
                        itr.Current.Value.OnResponse(null, error ?? new NetworkError(31337, "Not Found"));
                    }
                }
            }
        }
Пример #4
0
        static IEnumerator SendCoroutine(Petition petition, Action <Petition> onFinish)
        {
            NetworkError error    = null;
            string       response = string.Empty;
            int          retries  = 0;

            do
            {
                var uploader = new UploadHandlerRaw(petition.GetRawData());
                uploader.contentType = "application/json";

                var downloadHandler = new DownloadHandlerBuffer();

                var request = new UnityWebRequest(petition.Url);
                request.method          = petition.Method == Petition.SendMethod.Post ? UnityWebRequest.kHttpVerbPOST : UnityWebRequest.kHttpVerbGET;
                request.useHttpContinue = false;
                request.redirectLimit   = 50;
                request.timeout         = 60;
                if (petition.Method == Petition.SendMethod.Post)
                {
                    request.SetRequestHeader("Content-Type", "application/json");
                }
                request.uploadHandler   = uploader;
                request.downloadHandler = downloadHandler;

                yield return(request.SendWebRequest());

                if (request.isNetworkError || request.isHttpError)
                {
                    error = new NetworkError((int)request.responseCode, request.error);
                    SharedServices.GetService <ICustomLogger>().LogError("Network error error: {" + error.Code + ": " + error.Message + "} for petition: " + petition.GetData());
                }
                else
                {
                    response = request.downloadHandler.text;
                    error    = null;
                }
                ++retries;
            }while(error != null && retries < petition.Retries);

            petition.SetResponse(response);
            petition.Error = error;
            onFinish?.Invoke(petition);
        }
Пример #5
0
 void OnResponseReceived(JSONObject data, NetworkError error)
 {
     _onResponseCallback?.Invoke(_id, error == null && data["success"]);
 }