Beispiel #1
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);
                }
            });
        }
Beispiel #2
0
        private void performRequest(string method, Hashtable parameters = null, Action <RestifizerResponse> callback = null)
        {
            HTTP.Request 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);
                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());

                someRequest = new HTTP.Request(method, url, parameters);
            }
            else if (this.authType == AuthType.Bearer)
            {
                if (parameters == null)
                {
                    someRequest = new HTTP.Request(method, url);
                }
                else
                {
                    someRequest = new HTTP.Request(method, url, parameters);
                }
                someRequest.SetHeader("Access-Token", restifizerParams.GetAccessToken());
            }
            else
            {
                if (parameters == null)
                {
                    someRequest = new HTTP.Request(method, url);
                }
                else
                {
                    someRequest = new HTTP.Request(method, url, parameters);
                }
            }

            // Set Request Keys
            someRequest.SetHeader(SConstants.RequestTokenKey, restifizerParams.GetRequestKey());

            string tag = this.Tag;

            // Perform request
            someRequest.Send((request) => {
                if (request.response == 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;
                Debug.Log("Response Text: " + request.response.Text);
                object responseResult = JSON.JsonDecode(request.response.Text, ref result);
                if (!result)
                {
                    RestifizerError error = RestifizerErrorFactory.Create(-2, request.response.Text, tag);
                    if (errorHandler != null)
                    {
                        bool propagateResult = !errorHandler.onRestifizerError(error);
                        if (propagateResult)
                        {
                            callback(null);
                        }
                    }
                    else
                    {
                        callback(null);
                    }
                    return;
                }

                bool hasError = request.response.status >= 300;
                if (hasError)
                {
                    RestifizerError error = RestifizerErrorFactory.Create(request.response.status, 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);
                }
            });
        }