public PullAPIResponse PullRequest(List<Parameter> prms)
        {
            string response = null;
            PullAPIResponse result = new PullAPIResponse() { PullDetails = new PullInfo() };

            var id = (string)prms.First(p => p.Name == "id").Value;

            switch (id)
            {
                case "08b923395b6ce8bfa4d96f57jsonmeta":
                    response = MockAPIResponses.Default.PullJsonMetaFormat;
                    result.PullDetails.Format = "json_meta";
                    break;
                case "08b923395b6ce8bfa4d96f5jsonarray":
                    response = MockAPIResponses.Default.PullJsonArrayFormat;
                    result.PullDetails.Format = "json_array";
                    break;
                case "08b923395b6ce8bfa4d96jsonnewline":
                    response = MockAPIResponses.Default.PullJsonNewLineFormat;
                    result.PullDetails.Format = "json_new_line";
                    break;
            }

            result.StatusCode = HttpStatusCode.OK;

            if (response != null)
            {
                result.Data = APIHelpers.DeserializeResponse(response, result.PullDetails.Format);
            }

            return result;
        }
Пример #2
0
        public RestAPIResponse Request(string endpoint, dynamic parameters = null, RestSharp.Method method = Method.GET)
        {
            var request = new RestRequest(endpoint, method);
            RestAPIResponse result = null;

            if (parameters != null) request.Parameters.AddRange(APIHelpers.ParseParameters(endpoint, parameters));

            IRestResponse response = _client.Execute(request);

            if(endpoint == "pull")
            {
                result = new PullAPIResponse() { RateLimit = APIHelpers.ParseRateLimitHeaders(response.Headers), StatusCode = response.StatusCode, PullDetails = APIHelpers.ParsePullDetailHeaders(response.Headers) };
                result.Data = APIHelpers.DeserializeResponse(response.Content, ((PullAPIResponse)result).PullDetails.Format);
            }
            else
            {
                result = new RestAPIResponse() { RateLimit = APIHelpers.ParseRateLimitHeaders(response.Headers), StatusCode = response.StatusCode };
                result.Data = APIHelpers.DeserializeResponse(response.Content);
            }

            switch((int)response.StatusCode)
            {
                // Ok status codes
                case 200:
                case 201:
                case 202:
                case 204:
                    break;

                //Error status codes
                case 400:
                case 401:
                case 403:
                case 404:
                case 405:
                case 409:
                case 413:
                case 416:
                case 500:
                case 503:
                     throw new RestAPIException(result, (APIHelpers.HasAttr(result.Data, "error")) ? result.Data.error : "The request failed, please see the Data & StatusCode properties for more details.");
            }

            return result;
        }
Пример #3
0
        public RestAPIResponse Request(string endpoint, dynamic parameters = null, RestSharp.Method method = Method.GET)
        {
            RestClient client;
            client = new RestClient(_baseUrl + "v" + _apiVersion);
            client.Authenticator = _auth;
            client.UserAgent = _userAgent;

            var request = new RestRequest(endpoint, method);

            RestAPIResponse result = null;

            if(parameters != null)
            {
                var parsedParams = APIHelpers.ParseParameters(endpoint, parameters);

                if (method == Method.POST || method == Method.PUT)
                {
                    request.RequestFormat = DataFormat.Json;
                    request.AddBody(parsedParams);
                }
                else
                {
                    foreach (var prm in (IDictionary<string, object>)parsedParams)
                    {
                        request.AddParameter(prm.Key, prm.Value, ParameterType.GetOrPost);
                    }
                }
            }

            IRestResponse response = client.Execute(request);

            if(endpoint == "pull")
            {
                result = new PullAPIResponse() { RateLimit = APIHelpers.ParseRateLimitHeaders(response.Headers), StatusCode = response.StatusCode, PullDetails = APIHelpers.ParsePullDetailHeaders(response.Headers) };
                result.Data = APIHelpers.DeserializeResponse(response.Content, ((PullAPIResponse)result).PullDetails.Format);
            }
            else
            {
                result = new RestAPIResponse() { RateLimit = APIHelpers.ParseRateLimitHeaders(response.Headers), StatusCode = response.StatusCode };
                result.Data = APIHelpers.DeserializeResponse(response.Content);
            }

            switch((int)response.StatusCode)
            {
                // Ok status codes
                case 200:
                case 201:
                case 202:
                case 204:
                    break;

                //Error status codes
                case 400:
                case 401:
                case 403:
                case 404:
                case 405:
                case 409:
                case 413:
                case 416:
                case 500:
                case 503:
                    throw new RestAPIException(result, (APIHelpers.HasAttr(result.Data, "error")) ? result.Data.error : "The request failed, please see the Data & StatusCode properties for more details.");

                case 429:
                    throw new TooManyRequestsException(result, (APIHelpers.HasAttr(result.Data, "error")) ? result.Data.error : "The request failed because you've exceeded your API request limit.");
            }

            return result;
        }
Пример #4
0
        public RestAPIResponse Request(string endpoint, dynamic parameters = null, RestSharp.Method method = Method.GET)
        {
            RestClient client;

            client = new RestClient(_baseUrl + "v" + _apiVersion);
            client.Authenticator = _auth;
            client.UserAgent     = _userAgent;

            var request = new RestRequest(endpoint, method);

            RestAPIResponse result = null;

            if (parameters != null)
            {
                var parsedParams = APIHelpers.ParseParameters(endpoint, parameters);

                if (method == Method.POST || method == Method.PUT)
                {
                    request.RequestFormat = DataFormat.Json;
                    request.AddBody(parsedParams);
                }
                else
                {
                    foreach (var prm in (IDictionary <string, object>)parsedParams)
                    {
                        request.AddParameter(prm.Key, prm.Value, ParameterType.GetOrPost);
                    }
                }
            }


            IRestResponse response = client.Execute(request);

            if (endpoint == "pull")
            {
                result = new PullAPIResponse()
                {
                    RateLimit = APIHelpers.ParseRateLimitHeaders(response.Headers), StatusCode = response.StatusCode, PullDetails = APIHelpers.ParsePullDetailHeaders(response.Headers)
                };
                result.Data = APIHelpers.DeserializeResponse(response.Content, ((PullAPIResponse)result).PullDetails.Format);
            }
            else
            {
                result = new RestAPIResponse()
                {
                    RateLimit = APIHelpers.ParseRateLimitHeaders(response.Headers), StatusCode = response.StatusCode
                };
                result.Data = APIHelpers.DeserializeResponse(response.Content);
            }

            switch ((int)response.StatusCode)
            {
            // Ok status codes
            case 200:
            case 201:
            case 202:
            case 204:
                break;

            //Error status codes
            case 400:
            case 401:
            case 403:
            case 404:
            case 405:
            case 409:
            case 413:
            case 416:
            case 500:
            case 503:
                throw new RestAPIException(result, (APIHelpers.HasAttr(result.Data, "error")) ? result.Data.error : "The request failed, please see the Data & StatusCode properties for more details.");

            case 429:
                throw new TooManyRequestsException(result, (APIHelpers.HasAttr(result.Data, "error")) ? result.Data.error : "The request failed because you've exceeded your API request limit.");
            }

            return(result);
        }