Exemplo n.º 1
0
        /// <summary>
        /// Not supported by reverse proxy at the moment (big delay triggers timeout).
        /// </summary>
        public async Task <string> ProductPriceListCsvAsync()
        {
            var actionPath      = "/product/pricelist/csv";
            var requestBodyJson = JsonConvert.SerializeObject(
                new
            {
                Key = _settings.Key,
            },
                OmegaJsonSerializerSettings);

            var proxyRequest = new PassRequest()
            {
                Url            = _settings.ApiUrl + actionPath,
                Method         = "POST",
                RequestHeaders = new Dictionary <string, string>()
                {
                    ["Accept"] = "text/csv",
                },
                ContentHeaders = new Dictionary <string, string>()
                {
                    ["Content-Type"] = "application/json; charset=utf-8",
                },
                ContentBody = requestBodyJson,
            };

            var apiResponse = await _kioskProxyClient.PassAsync(proxyRequest);

            if (apiResponse.StatusCode != 200)
            {
                throw new OmegaAutoBizRequestException($"Request failed (status: {apiResponse.StatusCode}, action: '{actionPath}').");
            }

            return(apiResponse.ContentBody);
        }
        /// <summary>
        /// Checks the pass.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public PassResponse CheckPass(PassRequest request)
        {
            PassResponse response = new PassResponse();

            try
            {
                if (ValidateUser(request.Token) == true)
                {
                    Entities       db = new Entities();
                    PassRequestHed PRH;

                    int ReqNo = Convert.ToInt16(request.PassNo);

                    // Get pass request details
                    PRH = db.PassRequestHeds.ToList().Where(p => p.PassReqNo == ReqNo && p.Issued == true).FirstOrDefault();

                    // Check whether there are any valid data
                    if (PRH.PassReqNo != 0)
                    {
                        response.isValid = true;

                        response.personName    = db.PassRequestDets.Where(i => i.PassReqNo == PRH.PassReqNo).Select(d => d.PersonName).First().ToString();
                        response.personNIC     = db.PassRequestDets.Where(i => i.PassReqNo == PRH.PassReqNo).Select(d => d.PersonNIC).First().ToString();
                        response.issuedCompany = db.Companies.Where(i => i.CompanyID == PRH.CompanyID).Select(d => d.CompanyName).First().ToString();
                        response.validFrom     = PRH.RequiredFrom;
                        response.validTo       = PRH.RequiredTo;
                    }
                    else
                    {
                        response.isValid = false;
                    }
                }
                else
                {
                    response.isValid = false;
                }
            }
            catch (Exception ex)
            {
                response.isValid = false;
                response.AuthenticationToken.SessionData = ex.Message;
            }

            return(response);
        }
Exemplo n.º 3
0
        private async Task <TValue[]> SendRequestAsync <TValue>(string actionPath)
        {
            Assure.ArgumentNotNull(actionPath, nameof(actionPath));
            if (!actionPath.StartsWith("/"))
            {
                actionPath = "/" + actionPath;
            }

            var proxyRequest = new PassRequest()
            {
                Url            = _settings.ApiUrl + actionPath,
                Method         = "GET",
                RequestHeaders = new Dictionary <string, string>()
                {
                    ["Accept"] = "application/json",
                },
                ContentHeaders = new Dictionary <string, string>(),
            };

            var apiResponse = await _kioskProxyClient.PassAsync(proxyRequest);

            if (apiResponse.StatusCode != 200)
            {
                throw new TecDocWsRequestException($"Request failed (status: {apiResponse.StatusCode}, request: '{actionPath}').");
            }

            if (string.IsNullOrEmpty(apiResponse.ContentBody))
            {
                return(new TValue[0]);
            }

            try
            {
                return(JsonConvert.DeserializeObject <TValue[]>(apiResponse.ContentBody, new BrokenJsonArrayConverter()));
            }
            catch (Exception ex)
            {
                throw new TecDocWsRequestException($"Response deserialization exception (request: '{actionPath}').", ex);
            }
        }
Exemplo n.º 4
0
        private async Task <TResponse> SendRequestAsync <TResponse>(
            string actionPath,
            Dictionary <string, object> request,
            bool throwOnError)
            where TResponse : ResponseWrapperBase
        {
            Assure.ArgumentNotNull(actionPath, nameof(actionPath));
            Assure.ArgumentNotNull(request, nameof(request));

            if (!actionPath.StartsWith("/"))
            {
                actionPath = "/" + actionPath;
            }

            request["Key"] = _settings.Key;

            string requestBodyJson;

            try
            {
                requestBodyJson = JsonConvert.SerializeObject(request, OmegaJsonSerializerSettings);
            }
            catch (Exception ex)
            {
                throw new OmegaAutoBizRequestException($"Request serialization exception (action: '{actionPath}').", ex);
            }

            var proxyRequest = new PassRequest()
            {
                Url            = _settings.ApiUrl + actionPath,
                Method         = "POST",
                RequestHeaders = new Dictionary <string, string>()
                {
                    ["Accept"] = "application/json",
                },
                ContentHeaders = new Dictionary <string, string>()
                {
                    ["Content-Type"] = "application/json; charset=utf-8",
                },
                ContentBody = requestBodyJson,
            };

            var apiResponse = await _kioskProxyClient.PassAsync(proxyRequest);

            if (apiResponse.StatusCode != 200)
            {
                throw new OmegaAutoBizRequestException($"Request failed (status: {apiResponse.StatusCode}, action: '{actionPath}').");
            }

            if (string.IsNullOrEmpty(apiResponse.ContentBody))
            {
                return(null);
            }

            try
            {
                var response = JsonConvert.DeserializeObject <TResponse>(apiResponse.ContentBody, OmegaJsonSerializerSettings);
                if (!response.IsSuccess &&
                    throwOnError)
                {
                    var errors = response.Errors?
                                 .Select(x => $"{x.Error}-{x.Description}")
                                 .ToArray()
                                 ?? new string[0];
                    throw new OmegaAutoBizRequestException($"Request failed, errors: {string.Join(", ", errors)} (action: '{actionPath}').");
                }

                return(response);
            }
            catch (Exception ex)
            {
                throw new OmegaAutoBizRequestException($"Response deserialization exception (action: '{actionPath}').", ex);
            }
        }
Exemplo n.º 5
0
        public async Task <HttpResponseData> PassAsync(PassRequest passRequest)
        {
            Assure.ArgumentNotNull(passRequest, nameof(passRequest));

            string responseBody;

            try
            {
                using (var httpClient = new HttpClient())
                {
                    httpClient.DefaultRequestHeaders.Add("ProxyKey", _settings.ProxyKey);

                    var requestJson  = JsonConvert.SerializeObject(passRequest);
                    var httpResponse = await httpClient.PostAsync(
                        new Uri(_settings.Url + "/pass"),
                        new StringContent(requestJson, Encoding.UTF8, "application/json"));

                    responseBody = await httpResponse.Content.ReadAsStringAsync();

                    if (!httpResponse.IsSuccessStatusCode)
                    {
                        throw new KioskProxyRequestException($"Request to proxy failed, response code {(int)httpResponse.StatusCode}, body: {responseBody}");
                    }
                }
            }
            catch (KioskProxyRequestException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new KioskProxyRequestException("Request to proxy failed, no response.", ex);
            }

            PassResponse response;

            try
            {
                response = JsonConvert.DeserializeObject <PassResponse>(responseBody);
            }
            catch (Exception ex)
            {
                throw new KioskProxyRequestException("Bad format of proxy response.", ex);
            }

            if (response == null)
            {
                throw new KioskProxyRequestException("Empty proxy response.");
            }

            if (!response.IsReceived)
            {
                throw new KioskProxyRequestException($"Request to target resource failed, no response, error: {response.ErrorMessage}");
            }

            if (response.ReceivedResponse == null)
            {
                throw new KioskProxyRequestException($"Empty proxy response {nameof(response.ReceivedResponse)}.");
            }

            return(response.ReceivedResponse);
        }
Exemplo n.º 6
0
        private async Task <HttpResponseData> SendRequestAsync(PassRequest request)
        {
            var requestUri = new Uri(request.Url);

            using (var httpClient = new HttpClient())
            {
                httpClient.Timeout = _requestTimeout;

                var httpRequestMessage = new HttpRequestMessage()
                {
                    RequestUri = requestUri,
                    Method     = new HttpMethod(request.Method),
                };

                if (request.RequestHeaders != null)
                {
                    httpRequestMessage.Headers.Clear();

                    foreach (var(name, value) in request.RequestHeaders)
                    {
                        httpRequestMessage.Headers.Add(name, value);
                    }
                }

                if (request.ContentBody != null)
                {
                    httpRequestMessage.Content = new StringContent(request.ContentBody);

                    if (request.ContentHeaders != null)
                    {
                        httpRequestMessage.Content.Headers.Clear();

                        foreach (var(name, value) in request.ContentHeaders)
                        {
                            httpRequestMessage.Content.Headers.Add(name, value);
                        }
                    }
                }

                var response = await httpClient.SendAsync(httpRequestMessage);

                var responseData = new HttpResponseData()
                {
                    StatusCode      = (int)response.StatusCode,
                    ResponseHeaders = new Dictionary <string, string>(),
                    ContentHeaders  = new Dictionary <string, string>(),
                };

                foreach (var(name, values) in response.Headers)
                {
                    responseData.ResponseHeaders.Add(name, values.FirstOrDefault());
                }

                if (response.Content != null)
                {
                    responseData.ContentBody = await response.Content.ReadAsStringAsync();

                    foreach (var(name, values) in response.Content.Headers)
                    {
                        responseData.ContentHeaders.Add(name, values.FirstOrDefault());
                    }
                }

                return(responseData);
            }
        }