Beispiel #1
0
        public RestAPIResponse Ingest(string endpoint, dynamic data, Method method = Method.POST)
        {
            RestClient client;

            client = new RestClient(_baseUrl);
            client.Authenticator = _auth;
            client.UserAgent     = _userAgent;

            var request = new RestRequest(endpoint, method);

            RestAPIResponse result = null;

            if (data != null)
            {
                request.AddParameter("application/json", APIHelpers.SerializeToJsonLD(data), ParameterType.RequestBody);
            }

            IRestResponse response = client.Execute(request);

            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);
        }
        public RestAPIResponse Ingest(string endpoint, dynamic data, Method method = Method.POST)
        {
            string response = null;
            RestAPIResponse result = new RestAPIResponse();

            string body = APIHelpers.SerializeToJsonLD(data);

            response = "{\"accepted\":1, \"total_message_bytes\": 1691 }";

            result.StatusCode = HttpStatusCode.OK;
            result.Data = APIHelpers.DeserializeResponse(response);

            return result;
        }
        public RestAPIResponse Ingest(string endpoint, dynamic data, Method method = Method.POST)
        {
            RestClient client;
            client = new RestClient(_baseUrl);
            client.Authenticator = _auth;
            client.UserAgent = _userAgent;

            var request = new RestRequest(endpoint, method);

            RestAPIResponse result = null;

            if (data != null)
            {
                request.AddParameter("application/json", APIHelpers.SerializeToJsonLD(data), ParameterType.RequestBody);
            }

            IRestResponse response = client.Execute(request);

            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;
        }
        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;
        }
        public RestAPIResponse Request(string endpoint, dynamic parameters = null, Method method = Method.GET)
        {
            string response = null;
            RestAPIResponse result = new RestAPIResponse();

            List<Parameter> prms = new List<Parameter>();
            if(parameters != null) prms = APIHelpers.ParseParameters(endpoint, parameters);

            switch (endpoint)
            {
                case "validate":
                    response = MockAPIResponses.Default.Validate;
                    result.StatusCode = HttpStatusCode.OK;
                    break;
                case "compile":
                    response = MockAPIResponses.Default.Compile;
                    result.StatusCode = HttpStatusCode.OK;
                    break;
                case "usage":
                    response = MockAPIResponses.Default.Usage;
                    result.StatusCode = HttpStatusCode.OK;
                    break;
                case "dpu":
                    response = MockAPIResponses.Default.DPU;
                    result.StatusCode = HttpStatusCode.OK;
                    break;
                case "balance":
                    response = MockAPIResponses.Default.Balance;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "pull":

                    return PullRequest(prms);

                case "historics/get":

                    if (prms.Exists(p => p.Name == "id"))
                        response = MockAPIResponses.Default.HistoricsGetById;
                    else if (prms.Exists(p => p.Name == "max"))
                        response = MockAPIResponses.Default.HistoricsGetMax1;
                    else if (prms.Exists(p => p.Name == "with_estimate"))
                        response = MockAPIResponses.Default.HistoricsGetWithCompletion;
                    else
                        response = MockAPIResponses.Default.HistoricsGet;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "historics/prepare":
                    response = MockAPIResponses.Default.HistoricsPrepare;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "historics/start":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/stop":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/pause":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/resume":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/delete":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/status":
                    response = MockAPIResponses.Default.HistoricsStatus;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "historics/update":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "push/get":

                    if (prms.Exists(p => p.Name == "id"))
                        response = MockAPIResponses.Default.PushGetById;
                    else if (prms.Exists(p => p.Name == "hash"))
                        response = MockAPIResponses.Default.PushGetByHash;
                    else if (prms.Exists(p => p.Name == "historics_id"))
                        response = MockAPIResponses.Default.PushGetByHistoricsId;
                    else if (prms.Exists(p => p.Name == "page"))
                        response = MockAPIResponses.Default.PushGetPage;
                    else if (prms.Exists(p => p.Name == "per_page"))
                        response = MockAPIResponses.Default.PushGetPage;
                    else
                        response = MockAPIResponses.Default.PushGet;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/create":
                    response = MockAPIResponses.Default.PushCreate;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/delete":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "push/stop":
                    response = MockAPIResponses.Default.PushStop;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/pause":
                    response = MockAPIResponses.Default.PushPause;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/resume":
                    response = MockAPIResponses.Default.PushResume;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/log":

                    if (prms.Exists(p => p.Name == "id"))
                        response = MockAPIResponses.Default.PushLogById;
                    else if (prms.Exists(p => p.Name == "page"))
                        response = MockAPIResponses.Default.PushLogPage;
                    else if (prms.Exists(p => p.Name == "per_page"))
                        response = MockAPIResponses.Default.PushLogPage;
                    else
                        response = MockAPIResponses.Default.PushLog;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/update":
                    response = MockAPIResponses.Default.PushUpdate;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "preview/get":
                    var id = (string)prms.First(p => p.Name == "id").Value;

                    switch (id)
                    {
                        case "e25d533cf287ec44fe66e8362running":
                            response = MockAPIResponses.Default.HistoricsPreviewRunning;
                            result.StatusCode = HttpStatusCode.Accepted;
                            break;
                        case "e25d533cf287ec44fe66e8362finished":
                            response = MockAPIResponses.Default.HistoricsPreviewFinished;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                    }
                    break;

                case "preview/create":
                    response = MockAPIResponses.Default.HistoricsPreviewCreate;
                    result.StatusCode = HttpStatusCode.Accepted;
                    break;

                case "source/get":

                    if (prms.Exists(p => p.Name == "id"))
                        response = MockAPIResponses.Default.SourceGetById;
                    else if (prms.Exists(p => p.Name == "page"))
                        response = MockAPIResponses.Default.SourceGetPage;
                    else if (prms.Exists(p => p.Name == "per_page"))
                        response = MockAPIResponses.Default.SourceGetPage;
                    else
                        response = MockAPIResponses.Default.SourceGet;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/create":
                    response = MockAPIResponses.Default.SourceCreate;
                    result.StatusCode = HttpStatusCode.Created;
                    break;

                case "source/delete":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "source/start":
                    response = MockAPIResponses.Default.SourceStart;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/stop":
                    response = MockAPIResponses.Default.SourceStop;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/update":
                    response = MockAPIResponses.Default.SourceUpdate;
                    result.StatusCode = HttpStatusCode.Accepted;
                    break;

                case "source/log":

                    if (prms.Exists(p => p.Name == "page"))
                        response = MockAPIResponses.Default.SourceLogPage;
                    else if (prms.Exists(p => p.Name == "per_page"))
                        response = MockAPIResponses.Default.SourceLogPage;
                    else
                        response = MockAPIResponses.Default.SourceLog;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/resource/add":
                    response = MockAPIResponses.Default.SourceResourceAdd;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/resource/remove":
                    response = MockAPIResponses.Default.SourceResourceRemove;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/auth/add":
                    response = MockAPIResponses.Default.SourceAuthAdd;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/auth/remove":
                    response = MockAPIResponses.Default.SourceAuthRemove;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "list/get":
                    response = MockAPIResponses.Default.ListGet;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "list/create":
                    response = MockAPIResponses.Default.ListCreate;
                    result.StatusCode = HttpStatusCode.Created;
                    break;

                case "list/delete":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "list/add":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "list/remove":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "list/exists":
                    response = MockAPIResponses.Default.ListExists;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "list/replace/start":
                    response = MockAPIResponses.Default.ListReplaceStart;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "list/replace/abort":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "list/replace/commit":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "list/replace/add":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;
            }

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

            return result;
        }
Beispiel #6
0
 private static void WriteResult(RestAPIResponse result)
 {
     Console.WriteLine(JsonConvert.SerializeObject(result));
 }
 public TooManyRequestsException(RestAPIResponse response, string message)
     : base(response, message)
 {
 }
 public RestAPIException(RestAPIResponse response, string message) : base(message)
 {
     this.Response = response;
 }
        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;
        }
 public RestAPIException(RestAPIResponse response, string message)
     : base(message)
 {
     this.Response = response;
 }
 public TooManyRequestsException(RestAPIResponse response, string message)
     : base(response, message)
 {
 }
        public RestAPIResponse Request(string endpoint, dynamic parameters = null, Method method = Method.GET)
        {
            string response = null;
            RestAPIResponse result = new RestAPIResponse();

            IDictionary<string, object> prms = null;
            if(parameters != null) prms = APIHelpers.ParseParameters(endpoint, parameters);

            switch (endpoint)
            {
                case "validate":
                    response = MockAPIResponses.Default.Validate;
                    result.StatusCode = HttpStatusCode.OK;
                    break;
                case "compile":
                    response = MockAPIResponses.Default.Compile;
                    result.StatusCode = HttpStatusCode.OK;
                    break;
                case "usage":
                    response = MockAPIResponses.Default.Usage;
                    result.StatusCode = HttpStatusCode.OK;
                    break;
                case "dpu":
                    response = MockAPIResponses.Default.DPU;
                    result.StatusCode = HttpStatusCode.OK;
                    break;
                case "balance":
                    response = MockAPIResponses.Default.Balance;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "pull":

                    return PullRequest(prms);

                case "historics/get":

                    if (prms.ContainsKey("id"))
                        response = MockAPIResponses.Default.HistoricsGetById;
                    else if (prms.ContainsKey("max"))
                        response = MockAPIResponses.Default.HistoricsGetMax1;
                    else if (prms.ContainsKey("with_estimate"))
                        response = MockAPIResponses.Default.HistoricsGetWithCompletion;
                    else
                        response = MockAPIResponses.Default.HistoricsGet;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "historics/prepare":
                    response = MockAPIResponses.Default.HistoricsPrepare;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "historics/start":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/stop":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/pause":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/resume":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/delete":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "historics/status":
                    response = MockAPIResponses.Default.HistoricsStatus;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "historics/update":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "push/get":

                    if (prms.ContainsKey("id"))
                        response = MockAPIResponses.Default.PushGetById;
                    else if (prms.ContainsKey("hash"))
                        response = MockAPIResponses.Default.PushGetByHash;
                    else if (prms.ContainsKey("historics_id"))
                        response = MockAPIResponses.Default.PushGetByHistoricsId;
                    else if (prms.ContainsKey("page"))
                        response = MockAPIResponses.Default.PushGetPage;
                    else if (prms.ContainsKey("per_page"))
                        response = MockAPIResponses.Default.PushGetPage;
                    else
                        response = MockAPIResponses.Default.PushGet;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/create":
                    response = MockAPIResponses.Default.PushCreate;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/delete":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "push/stop":
                    response = MockAPIResponses.Default.PushStop;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/pause":
                    response = MockAPIResponses.Default.PushPause;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/resume":
                    response = MockAPIResponses.Default.PushResume;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/log":

                    if (prms.ContainsKey("id"))
                        response = MockAPIResponses.Default.PushLogById;
                    else if (prms.ContainsKey("page"))
                        response = MockAPIResponses.Default.PushLogPage;
                    else if (prms.ContainsKey("per_page"))
                        response = MockAPIResponses.Default.PushLogPage;
                    else
                        response = MockAPIResponses.Default.PushLog;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "push/update":
                    response = MockAPIResponses.Default.PushUpdate;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "preview/get":
                    var id = (string)prms.First(p => p.Key == "id").Value;

                    switch (id)
                    {
                        case "e25d533cf287ec44fe66e8362running":
                            response = MockAPIResponses.Default.HistoricsPreviewRunning;
                            result.StatusCode = HttpStatusCode.Accepted;
                            break;
                        case "e25d533cf287ec44fe66e8362finished":
                            response = MockAPIResponses.Default.HistoricsPreviewFinished;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                    }
                    break;

                case "preview/create":
                    response = MockAPIResponses.Default.HistoricsPreviewCreate;
                    result.StatusCode = HttpStatusCode.Accepted;
                    break;

                case "source/get":

                    if (prms.ContainsKey("id"))
                        response = MockAPIResponses.Default.SourceGetById;
                    else if (prms.ContainsKey("page"))
                        response = MockAPIResponses.Default.SourceGetPage;
                    else if (prms.ContainsKey("per_page"))
                        response = MockAPIResponses.Default.SourceGetPage;
                    else
                        response = MockAPIResponses.Default.SourceGet;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/create":
                    response = MockAPIResponses.Default.SourceCreate;
                    result.StatusCode = HttpStatusCode.Created;
                    break;

                case "source/delete":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "source/start":
                    response = MockAPIResponses.Default.SourceStart;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/stop":
                    response = MockAPIResponses.Default.SourceStop;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/update":
                    response = MockAPIResponses.Default.SourceUpdate;
                    result.StatusCode = HttpStatusCode.Accepted;
                    break;

                case "source/log":

                    if (prms.ContainsKey("page"))
                        response = MockAPIResponses.Default.SourceLogPage;
                    else if (prms.ContainsKey("per_page"))
                        response = MockAPIResponses.Default.SourceLogPage;
                    else
                        response = MockAPIResponses.Default.SourceLog;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/resource/add":
                    response = MockAPIResponses.Default.SourceResourceAdd;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/resource/remove":
                    response = MockAPIResponses.Default.SourceResourceRemove;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/auth/add":
                    response = MockAPIResponses.Default.SourceAuthAdd;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "source/auth/remove":
                    response = MockAPIResponses.Default.SourceAuthRemove;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "pylon/get":
                    if (prms.ContainsKey("hash"))
                        response = PylonAPIResponses.Default.GetOne;
                    else
                        response = PylonAPIResponses.Default.Get;

                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "pylon/validate":
                    response = PylonAPIResponses.Default.Validate;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "pylon/compile":
                    response = PylonAPIResponses.Default.Compile;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "pylon/start":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "pylon/stop":
                    result.StatusCode = HttpStatusCode.NoContent;
                    break;

                case "pylon/analyze":
                    var hash = (string)prms.First(p => p.Key == "hash").Value;

                    if (hash.IndexOf("nested") != -1)
                    {
                        response = PylonAPIResponses.Default.AnalyzeNested;
                        result.StatusCode = HttpStatusCode.OK;
                    }
                    else
                    {
                        response = PylonAPIResponses.Default.Analyze;
                        result.StatusCode = HttpStatusCode.OK;
                    }
                    break;

                case "pylon/tags":
                    response = PylonAPIResponses.Default.Tags;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "account/usage":
                    response = AccountAPIResponses.Default.Account_Usage;
                    result.StatusCode = HttpStatusCode.OK;
                    break;

                case "account/identity":
                    switch (method) {
                        case Method.POST:
                            response = AccountAPIResponses.Default.Identity_Create;
                            result.StatusCode = HttpStatusCode.Created;
                            break;
                        case Method.GET:
                            if (prms.ContainsKey("label"))
                                response = AccountAPIResponses.Default.Identity_Get_Label;
                            else if (prms.ContainsKey("page") || prms.ContainsKey("per_page"))
                                response = AccountAPIResponses.Default.Identity_Get_Page;
                            else
                                response = AccountAPIResponses.Default.Identity_Get;

                            result.StatusCode = HttpStatusCode.OK;
                            break;
                    }

                    break;

                case "account/identity/" + Account.Identity.VALID_ID:
                    switch (method)
                    {
                        case Method.GET:
                            response = AccountAPIResponses.Default.Identity_Get_Id;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                        case Method.PUT:
                            response = AccountAPIResponses.Default.Identity_Update;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                        case Method.DELETE:
                            result.StatusCode = HttpStatusCode.NoContent;
                            break;
                    }

                    break;

                case "account/identity/" + Account.Token.VALID_IDENTITY + "/token":
                    switch (method)
                    {
                        case Method.POST:
                            response = AccountAPIResponses.Default.Token_Create;
                            result.StatusCode = HttpStatusCode.Created;
                            break;
                        case Method.GET:
                            if (prms.ContainsKey("service"))
                                response = AccountAPIResponses.Default.Token_GetAll;
                            else if (prms.ContainsKey("page") || prms.ContainsKey("per_page"))
                                response = AccountAPIResponses.Default.Token_GetAll;
                            else
                                response = AccountAPIResponses.Default.Token_GetAll;

                            result.StatusCode = HttpStatusCode.OK;
                            break;
                    }

                    break;

                case "account/identity/" + Account.Token.VALID_IDENTITY + "/token/" + Account.Token.VALID_SERVICE:
                    switch (method)
                    {
                        case Method.GET:
                            response = AccountAPIResponses.Default.Token_GetAll;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                        case Method.PUT:
                            response = AccountAPIResponses.Default.Token_Update;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                        case Method.DELETE:
                            result.StatusCode = HttpStatusCode.NoContent;
                            break;
                    }

                    break;

                case "account/identity/" + Account.Limit.VALID_IDENTITY + "/limit":
                    switch (method)
                    {
                        case Method.POST:
                            response = AccountAPIResponses.Default.Limit_Create;
                            result.StatusCode = HttpStatusCode.Created;
                            break;
                    }

                    break;

                case "account/identity/" + Account.Limit.VALID_IDENTITY + "/limit/" + Account.Token.VALID_SERVICE:
                    switch (method)
                    {
                        case Method.GET:
                            response = AccountAPIResponses.Default.Limit_GetByIdentityAndService;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                        case Method.PUT:
                            response = AccountAPIResponses.Default.Limit_Update;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                        case Method.DELETE:
                            result.StatusCode = HttpStatusCode.NoContent;
                            break;
                    }

                    break;

                case "account/identity/limit/" + Account.Token.VALID_SERVICE:
                    switch (method)
                    {
                        case Method.GET:
                            response = AccountAPIResponses.Default.Limit_AllByService;
                            result.StatusCode = HttpStatusCode.OK;
                            break;
                    }

                    break;
            }

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

            return result;
        }
Beispiel #13
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);
        }