Beispiel #1
0
        private IFlurlRequest BuildBaseRequest(string url, object?query, object?cookies)
        {
            IFlurlRequest req = new FlurlRequest(url);

            if (query != null)
            {
                req = req.SetQueryParams(query);
            }
            if (cookies != null)
            {
                req = req.WithCookies(cookies);
            }

            return(req);
        }
Beispiel #2
0
        private async Task <T> ProcessGetAsync <T>(string url, Dictionary <string, string> headers = null, object requestParams = null, string accessToken = "", int timeout = 10, bool isThrowException = true)
        {
            var result = new RestApiResponse <T>();

            try
            {
                var _request      = new Url(url);
                var _flurlRequest = new FlurlRequest(_request);

                if (!string.IsNullOrEmpty(accessToken))
                {
                    _flurlRequest = _flurlRequest.WithHeader("Authorization", accessToken);
                }

                if (headers != null && headers.Count > 0)
                {
                    foreach (var item in headers)
                    {
                        _flurlRequest = _flurlRequest.WithHeader(item.Key, item.Value);
                    }
                }

                var response = requestParams != null ? await _flurlRequest.SetQueryParams(requestParams).WithTimeout(timeout).GetAsync() : await _flurlRequest.WithTimeout(timeout).GetAsync();

                if (response.IsSuccessStatusCode)
                {
                    var stringResponse = await response.Content.ReadAsStringAsync();

                    result.Result    = JsonConvert.DeserializeObject <T>(stringResponse);
                    result.IsSuccess = true;
                }
            }
            catch (FlurlHttpTimeoutException timeoutex)
            {
                result.IsSuccess        = false;
                result.ExceptionMessage = "Time out";
                result.Exception        = timeoutex;
            }
            catch (FlurlHttpException ex)
            {
                var errorResponse = await ex.GetResponseStringAsync();

                result.Exception        = ex;
                result.ExceptionMessage = ex.Message;
                result.IsSuccess        = false;
                //Try parse response to Custom Exception
                try
                {
                    var customException = JsonConvert.DeserializeObject <CustomExceptionResponse>(errorResponse);
                    if (customException != null)
                    {
                        result.IsCustomException = true;
                        result.CustomException   = customException;
                        result.IsSuccess         = false;
                    }
                }
                catch
                {
                    // KhuongDang : No content
                }
            }
            catch (Exception ex)
            {
                result.IsSuccess = false;
                result.Exception = ex;
            }

            WriteLog(url, result, requestParams, isThrowException);

            return(result.Result);
        }