Example #1
0
        private void GetApiResponse <T>(ApiUrlBuilder ub, HttpMethod httpMethod, string backoffKey, Action <StacManResponse <T> > callback) where T : StacManType
        {
            var response = new StacManResponse <T>();

            ub.AddParameter("key", Key);

            Action <string> successCallback = rawData =>
            {
                try
                {
                    response.RawData = rawData;
                    response.Data    = ParseApiResponse <Wrapper <T> >(Serializer.Deserialize <Dictionary <string, object> >(response.RawData), backoffKey);

                    if (response.Data.ErrorId.HasValue)
                    {
                        throw new Exceptions.StackExchangeApiException(response.Data.ErrorId.Value, response.Data.ErrorName, response.Data.ErrorMessage);
                    }

                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Error   = ex;
                }

                callback(response);
            };

            Action <Exception> errorCallback = ex =>
            {
                response.Success = false;
                response.Error   = ex;
                callback(response);
            };


            var urlManager = this.urlManager; // snapshot

            if (httpMethod == HttpMethod.POST)
            {
                var url = ub.BaseUrl;
                if (urlManager != null)
                {
                    url = urlManager(url);
                }
                response.ApiUrl = url;
                FetchApiResponseWithPOST(url, ub.QueryString(), successCallback, errorCallback);
            }
            else
            {
                var url = ub.ToString();
                if (urlManager != null)
                {
                    url = urlManager(url);
                }
                response.ApiUrl = url;
                FetchApiResponseWithGET(url, successCallback, errorCallback);
            }
        }
Example #2
0
        private void GetApiResponse <T>(ApiUrlBuilder ub, string backoffKey, Action <StacManResponse <T> > callback) where T : StacManType
        {
            var response = new StacManResponse <T>();

            ub.AddParameter("key", Key);

            response.ApiUrl = ub.ToString();

            FetchApiResponse(response.ApiUrl,
                             rawData =>
            {
                try
                {
                    response.RawData = rawData;
                    response.Data    = ParseApiResponse <Wrapper <T> >(Serializer.Deserialize <Dictionary <string, object> >(response.RawData), backoffKey);

                    if (response.Data.ErrorId.HasValue)
                    {
                        throw new Exceptions.StackExchangeApiException(response.Data.ErrorId.Value, response.Data.ErrorName, response.Data.ErrorMessage);
                    }

                    response.Success = true;
                }
                catch (Exception ex)
                {
                    response.Success = false;
                    response.Error   = ex;
                }

                callback(response);
            },
                             ex =>
            {
                response.Success = false;
                response.Error   = ex;
                callback(response);
            });
        }