Exemplo n.º 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);
        }
Exemplo n.º 2
0
        private void _connection_MessageReceived(object sender, MessageReceivedEventArgs e)
        {
            var message = APIHelpers.DeserializeResponse(e.Message);

            if (APIHelpers.HasAttr(message, "reconnect"))
            {
                // Server has asked clients to reconnect
                Reconnect(false, false, false);
            }
            else if (APIHelpers.HasAttr(message, "status"))
            {
                DataSiftMessageStatus status;

                if (System.Enum.TryParse <DataSiftMessageStatus>(message.status, true, out status))
                {
                    // Special case when subscription has succeeded
                    if (status == DataSiftMessageStatus.Success && APIHelpers.HasAttr(message, "hash"))
                    {
                        // Fire message at subscription level
                        if (_subscribedHandlers.ContainsKey(message.hash))
                        {
                            _subscribedHandlers[message.hash](message.hash);
                        }

                        // Fire message at connection level
                        if (OnSubscribed != null)
                        {
                            OnSubscribed(message.hash);
                        }
                    }
                    else
                    {
                        if (OnDataSiftMessage != null)
                        {
                            OnDataSiftMessage(status, message.message);
                        }
                    }
                }
            }
            else if (APIHelpers.HasAttr(message, "hash"))
            {
                // Check for deletes
                if (APIHelpers.HasAttr(message, "data"))
                {
                    if (APIHelpers.HasAttr(message.data, "deleted"))
                    {
                        // Fire delete at subscription level
                        if (_deleteHandlers.ContainsKey(message.hash))
                        {
                            _deleteHandlers[message.hash](message.hash, message.data);
                        }

                        // Fire delete at connection level
                        if (OnDelete != null)
                        {
                            OnDelete(message.hash, message.data);
                        }

                        return;
                    }
                }

                // Otherwise normal interaction

                // Fire message at subscription level
                if (_messageHandlers.ContainsKey(message.hash))
                {
                    _messageHandlers[message.hash](message.hash, message.data);
                }

                // Fire message at connection level
                if (OnMessage != null)
                {
                    OnMessage(message.hash, message.data);
                }
            }
        }
Exemplo n.º 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);
        }