Ejemplo n.º 1
0
        private async Task <ListPaginated <T> > DoRequestList <T>(string urlMethod, Pagination pagination, Dictionary <String, String> additionalUrlParams)
        {
            ListPaginated <T> responseObject = null;

            UrlTool urlTool = new UrlTool(_root);
            string  restUrl = urlTool.GetRestUrl(urlMethod, this._authRequired && this._includeClientId, pagination, null, _root.Config.ApiVersion);

            if (this._requestData != null)
            {
                string parameters = "";
                foreach (KeyValuePair <String, String> entry in this._requestData)
                {
                    parameters += String.Format("&{0}={1}", Uri.EscapeDataString(entry.Key), Uri.EscapeDataString(entry.Value));
                }
                if (pagination == null)
                {
                    parameters = parameters.Remove(0, 1).Insert(0, Constants.URI_QUERY_SEPARATOR);
                }

                restUrl += parameters;
            }

            string     fullUrl = urlTool.GetFullUrl(restUrl);
            RestClient client  = new RestClient(fullUrl);

            client.AddHandler(Constants.APPLICATION_JSON, new MangoPayJsonDeserializer());

            _log.Debug("FullUrl: " + urlTool.GetFullUrl(restUrl));

            Method      method      = (Method)Enum.Parse(typeof(Method), this._requestType, false);
            RestRequest restRequest = new RestRequest(method)
            {
                RequestFormat  = DataFormat.Json,
                JsonSerializer = new MangoPayJsonSerializer
                {
                    ContentType = Constants.APPLICATION_JSON
                }
            };

            foreach (KeyValuePair <string, string> h in await this.GetHttpHeaders(restUrl))
            {
                restRequest.AddHeader(h.Key, h.Value);

                if (h.Key != Constants.AUTHORIZATION)
                {
                    _log.Debug("HTTP Header: " + h.Key + ": " + h.Value);
                }
            }

            if (pagination != null)
            {
                this._pagination = pagination;
            }

            _log.Debug("RequestType: " + this._requestType);

            IRestResponse <List <T> > restResponse = client.Execute <List <T> >(restRequest);

            responseObject = new ListPaginated <T>(restResponse.Data);

            this._responseCode = (int)restResponse.StatusCode;

            if (restResponse.StatusCode == HttpStatusCode.OK || restResponse.StatusCode == HttpStatusCode.NoContent)
            {
                _log.Debug("Response OK: " + restResponse.Content);
            }
            else
            {
                _log.Debug("Response ERROR: " + restResponse.Content);
            }

            if (this._responseCode == 200)
            {
                responseObject = this.ReadResponseHeaders <T>(restResponse, responseObject);

                _log.Debug("Response object: " + responseObject.ToString());
            }

            SetLastRequestInfo(restRequest, restResponse);

            this.CheckResponseCode(restResponse);

            return(responseObject);
        }
Ejemplo n.º 2
0
        private U DoRequest <U, T>(String urlMethod, Pagination pagination, T entity)
            where U : new()
        {
            U responseObject = default(U);

            UrlTool urlTool = new UrlTool(_root);
            String  restUrl = urlTool.GetRestUrl(urlMethod, this._authRequired, pagination, null);

            string     fullUrl = urlTool.GetFullUrl(restUrl);
            RestClient client  = new RestClient(fullUrl);

            client.AddHandler(Constants.APPLICATION_JSON, new MangoPayJsonDeserializer());

            _log.Debug("FullUrl: " + urlTool.GetFullUrl(restUrl));

            Method      method      = (Method)Enum.Parse(typeof(Method), this._requestType, false);
            RestRequest restRequest = new RestRequest(method);

            restRequest.RequestFormat              = DataFormat.Json;
            restRequest.JsonSerializer             = new MangoPayJsonSerializer();
            restRequest.JsonSerializer.ContentType = Constants.APPLICATION_JSON;

            foreach (KeyValuePair <string, string> h in this.GetHttpHeaders(restUrl))
            {
                restRequest.AddHeader(h.Key, h.Value);

                if (h.Key != Constants.AUTHORIZATION)
                {
                    _log.Debug("HTTP Header: " + h.Key + ": " + h.Value);
                }
            }

            if (pagination != null)
            {
                this._pagination = pagination;
            }

            _log.Debug("RequestType: " + this._requestType);

            if (this._requestData != null || entity != null)
            {
                if (entity != null)
                {
                    restRequest.AddBody(entity);
                }
                if (this._requestData != null)
                {
                    foreach (KeyValuePair <String, String> entry in this._requestData)
                    {
                        restRequest.AddParameter(entry.Key, entry.Value);
                    }
                }

                Parameter body = restRequest.Parameters.Where(p => p.Type == ParameterType.RequestBody).FirstOrDefault();
                IEnumerable <Parameter> parameters = restRequest.Parameters.Where(p => p.Type == ParameterType.GetOrPost);
                foreach (Parameter p in parameters)
                {
                    _log.Debug(p.Name + ": " + p.Value);
                }

                if (body != null)
                {
                    _log.Debug("CurrentBody: " + body.Value);
                }
                else
                {
                    _log.Debug("CurrentBody: /body is null/");
                }
            }

            IRestResponse <U> restResponse = client.Execute <U>(restRequest);

            responseObject = restResponse.Data;

            this._responseCode = (int)restResponse.StatusCode;

            if (restResponse.StatusCode == HttpStatusCode.OK || restResponse.StatusCode == HttpStatusCode.NoContent)
            {
                _log.Debug("Response OK: " + restResponse.Content);
            }
            else
            {
                _log.Debug("Response ERROR: " + restResponse.Content);
            }

            if (this._responseCode == 200)
            {
                _log.Debug("Response object: " + responseObject.ToString());
            }

            this.CheckResponseCode(restResponse.Content);

            return(responseObject);
        }
Ejemplo n.º 3
0
        private async Task <U> DoRequest <U, T>(String idempotencyKey, String urlMethod, Pagination pagination, T entity)
            where U : new()
        {
            U responseObject = default(U);

            UrlTool urlTool = new UrlTool(_root);
            String  restUrl = urlTool.GetRestUrl(urlMethod, this._authRequired && this._includeClientId, pagination, null, _root.Config.ApiVersion);

            string     fullUrl = urlTool.GetFullUrl(restUrl);
            RestClient client  = new RestClient(fullUrl);

            client.AddHandler(Constants.APPLICATION_JSON, new MangoPayJsonDeserializer());

            _log.Debug("FullUrl: " + urlTool.GetFullUrl(restUrl));

            Method      method      = (Method)Enum.Parse(typeof(Method), this._requestType, false);
            RestRequest restRequest = new RestRequest(method)
            {
                RequestFormat  = DataFormat.Json,
                JsonSerializer = new MangoPayJsonSerializer()
            };

            restRequest.JsonSerializer.ContentType = Constants.APPLICATION_JSON;

            if (_root.Config.Timeout > 0)
            {
                client.Timeout      = _root.Config.Timeout;
                restRequest.Timeout = _root.Config.Timeout;
            }

            foreach (KeyValuePair <string, string> h in await this.GetHttpHeaders(restUrl))
            {
                restRequest.AddHeader(h.Key, h.Value);

                if (h.Key != Constants.AUTHORIZATION)
                {
                    _log.Debug("HTTP Header: " + h.Key + ": " + h.Value);
                }
            }

            if (!String.IsNullOrWhiteSpace(idempotencyKey))
            {
                restRequest.AddHeader(Constants.IDEMPOTENCY_KEY, idempotencyKey);
            }

            if (pagination != null)
            {
                this._pagination = pagination;
            }

            _log.Debug("RequestType: " + this._requestType);

            if (this._requestData != null || entity != null)
            {
                if (entity != null)
                {
                    restRequest.AddBody(entity);
                }
                if (this._requestData != null)
                {
                    foreach (KeyValuePair <String, String> entry in this._requestData)
                    {
                        restRequest.AddParameter(entry.Key, entry.Value);
                    }
                }

                Parameter body = restRequest.Parameters.Where(p => p.Type == ParameterType.RequestBody).FirstOrDefault();
                IEnumerable <Parameter> parameters = restRequest.Parameters.Where(p => p.Type == ParameterType.GetOrPost);
                foreach (Parameter p in parameters)
                {
                    _log.Debug(p.Name + ": " + p.Value);
                }

                if (body != null)
                {
                    bool skipBytesFileContent = !this._root.Config.LogKycFileContent && entity != null &&
                                                entity.GetType() == typeof(Entities.POST.KycPagePostDTO);

                    if (!skipBytesFileContent)
                    {
                        _log.Debug("CurrentBody: " + body.Value);
                    }
                    else
                    {
                        _log.Debug("CurrentBody: " + $"{{\"File\":\"-- bytes file content skipped --\", \"Tag\":" +
                                   $"{body.Value.ToString().Substring(body.Value.ToString().IndexOf("Tag\":") + 5)}");
                    }
                }
                else
                {
                    _log.Debug("CurrentBody: /body is null/");
                }
            }

            IRestResponse <U> restResponse = await client.ExecuteTaskAsync <U>(restRequest);

            responseObject = restResponse.Data;

            this._responseCode = (int)restResponse.StatusCode;

            if (restResponse.StatusCode == HttpStatusCode.OK || restResponse.StatusCode == HttpStatusCode.NoContent)
            {
                _log.Debug("Response OK: " + restResponse.Content);
            }
            else
            {
                _log.Debug("Response ERROR: " + restResponse.Content);
            }

            if (this._responseCode == 200)
            {
                _log.Debug("Response object: " + responseObject.ToString());
            }

            SetLastRequestInfo(restRequest, restResponse);

            this.CheckResponseCode(restResponse);

            return(responseObject);
        }
Ejemplo n.º 4
0
        private U DoRequest <U, T>(String idempotencyKey, String urlMethod, Pagination pagination, T entity)
            where U : new()
        {
            U responseObject = default(U);

            UrlTool urlTool = new UrlTool(_root);
            String  restUrl = urlTool.GetRestUrl(urlMethod, this._authRequired && this._includeClientId, pagination, null, _root.Config.ApiVersion);

            string     fullUrl = urlTool.GetFullUrl(restUrl);
            RestClient client  = new RestClient(fullUrl);

            client.AddHandler(Constants.APPLICATION_JSON, () => { return(new MangoPayJsonDeserializer()); });

            _log.Debug("FullUrl: " + urlTool.GetFullUrl(restUrl));

            Method      method      = (Method)Enum.Parse(typeof(Method), this._requestType, false);
            RestRequest restRequest = new RestRequest(method)
            {
                RequestFormat  = DataFormat.Json,
                JsonSerializer = new MangoPayJsonSerializer()
            };

            restRequest.JsonSerializer.ContentType = Constants.APPLICATION_JSON;

            if (_root.Config.Timeout > 0)
            {
                client.Timeout      = _root.Config.Timeout;
                restRequest.Timeout = _root.Config.Timeout;
            }

            var headers = this.GetHttpHeaders(restUrl);

            foreach (KeyValuePair <string, string> h in headers)
            {
                restRequest.AddHeader(h.Key, h.Value);

                if (h.Key != Constants.AUTHORIZATION)
                {
                    _log.Debug("HTTP Header: " + h.Key + ": " + h.Value);
                }
            }

            if (!String.IsNullOrWhiteSpace(idempotencyKey))
            {
                restRequest.AddHeader(Constants.IDEMPOTENCY_KEY, idempotencyKey);
            }

            if (pagination != null)
            {
                this._pagination = pagination;
            }

            _log.Debug("RequestType: " + this._requestType);

            if (this._requestData != null || entity != null)
            {
                if (entity != null)
                {
                    restRequest.AddJsonBody(entity);
                }
                if (this._requestData != null)
                {
                    foreach (KeyValuePair <String, String> entry in this._requestData)
                    {
                        restRequest.AddParameter(entry.Key, entry.Value);
                    }
                }

                var body       = restRequest.Parameters.Where(p => p.Type == ParameterType.RequestBody).FirstOrDefault();
                var parameters = restRequest.Parameters.Where(p => p.Type == ParameterType.GetOrPost);
                foreach (var p in parameters)
                {
                    _log.Debug(p.Name + ": " + p.Value);
                }

                if (body != null)
                {
                    _log.Debug("CurrentBody: " + body.Value);
                }
                else
                {
                    _log.Debug("CurrentBody: /body is null/");
                }
            }

            IRestResponse <U> restResponse = client.Execute <U>(restRequest);

            responseObject = restResponse.Data;

            this._responseCode = (int)restResponse.StatusCode;

            if (restResponse.StatusCode == HttpStatusCode.OK || restResponse.StatusCode == HttpStatusCode.NoContent)
            {
                _log.Debug("Response OK: " + restResponse.Content);
            }
            else
            {
                _log.Debug("Response ERROR: " + restResponse.Content);
            }

            if (this._responseCode == 200)
            {
                _log.Debug("Response object: " + responseObject.ToString());
            }

            SetLastRequestInfo(restRequest, restResponse);

            this.CheckResponseCode(restResponse);

            return(responseObject);
        }