Beispiel #1
0
 public RestifizerResponse(RestRequest request, RestifizerError error, string tag)
 {
     this.Status   = (int)request.responseCode;
     this.HasError = true;
     this.Error    = error;
     this.Request  = request;
     this.Tag      = tag;
 }
Beispiel #2
0
        private void performRequest(string method, Hashtable parameters = null, Action <RestifizerResponse> callback = null)
        {
            RestRequest someRequest;

            string url      = Path;
            string queryStr = "";

            // paging
            if (PageNumber != -1)
            {
                if (queryStr.Length > 0)
                {
                    queryStr += "&";
                }
                queryStr += "per_page=" + PageNumber;
            }
            if (PageSize != -1)
            {
                if (queryStr.Length > 0)
                {
                    queryStr += "&";
                }
                queryStr += "page=" + PageSize;
            }

            // filtering
            if (filterParams != null && filterParams.Count > 0)
            {
                if (queryStr.Length > 0)
                {
                    queryStr += "&";
                }
                //string filterValue = JSON.JsonEncode(filterParams);
                JSON.JSONSerialize.Serialize("", filterParams);
                //TODO zmenit na nejake rozumnejsie hodnoty. Resp. su tam vobec potrebne tak ako su?
                string filterValue = JSON.JSONSerialize.GetJsonFile("json", "json");
                queryStr += "filter=" + filterValue;
            }

            // extra params
            if (extraQuery != null && extraQuery.Count > 0)
            {
                foreach (string key in extraQuery.Keys)
                {
                    if (queryStr.Length > 0)
                    {
                        queryStr += "&";
                    }
                    queryStr += key + "=" + extraQuery[key];
                }
            }

            if (queryStr.Length > 0)
            {
                url += "?" + queryStr;
            }


            // Handle authentication
            if (this.authType == AuthType.Client)
            {
                if (parameters == null)
                {
                    parameters = new Hashtable();
                }
                parameters.Add("client_id", restifizerParams.GetClientId());
                parameters.Add("client_secret", restifizerParams.GetClientSecret());

                //TODO newGameObject nie su uplne stastne :)
                someRequest = new RestRequest(method, url, new GameObject(), parameters);
            }
            else if (this.authType == AuthType.Bearer)
            {
                if (parameters == null)
                {
                    someRequest = new RestRequest(method, url, new GameObject(), new Hashtable());
                }
                else
                {
                    someRequest = new RestRequest(method, url, new GameObject(), parameters);
                }
                //TODO kuknut someRequest.SetHeader("Authorization", "Bearer " + restifizerParams.GetAccessToken());
            }
            else
            {
                if (parameters == null)
                {
                    someRequest = new RestRequest(method, url, new GameObject(), parameters);
                }
                else
                {
                    someRequest = new RestRequest(method, url, new GameObject(), parameters);
                }
            }

            string tag = this.Tag;

            // Perform request
            someRequest.Send((request) => {
                //TODO opravit

                /*
                 * if (request.responseCode == null) {
                 *      RestifizerError error = RestifizerErrorFactory.Create(-1, null, tag);
                 *      if (errorHandler != null) {
                 *              bool propagateResult = !errorHandler.onRestifizerError(error);
                 *              if (propagateResult) {
                 *                      callback(null);
                 *              }
                 *      } else {
                 *              callback(null);
                 *      }
                 *      return;
                 * }
                 */
                bool result = false;
                //object responseResult = JSON.JsonDecode(request.responseText, ref result);
                object responseResult = JSON.JSONSerialize.Deserialize <object>(request.responseText);
                if (!result)
                {
                    RestifizerError error = RestifizerErrorFactory.Create(-2, request.responseText, tag);
                    if (errorHandler != null)
                    {
                        bool propagateResult = !errorHandler.onRestifizerError(error);
                        if (propagateResult)
                        {
                            callback(null);
                        }
                    }
                    else
                    {
                        callback(null);
                    }
                    return;
                }

                bool hasError = request.responseCode >= 300;
                if (hasError)
                {
                    RestifizerError error = RestifizerErrorFactory.Create(request.responseCode, responseResult, tag);
                    if (errorHandler != null)
                    {
                        bool propagateResult = !errorHandler.onRestifizerError(error);
                        if (propagateResult)
                        {
                            callback(new RestifizerResponse(request, error, tag));
                        }
                    }
                    else
                    {
                        callback(new RestifizerResponse(request, error, tag));
                    }
                }
                else if (responseResult is ArrayList)
                {
                    callback(new RestifizerResponse(request, (ArrayList)responseResult, tag));
                }
                else if (responseResult is Hashtable)
                {
                    callback(new RestifizerResponse(request, (Hashtable)responseResult, tag));
                }
                else
                {
                    Debug.LogWarning("Unsupported type in response: " + responseResult.GetType());
                    callback(null);
                }
            });
        }