Example #1
0
        private static async Task <byte[]> SendAsync(IFlurlRequest flurlRequest, WebRequestContext requestContext)
        {
            byte[] rawData = null;

            try
            {
                requestContext.AttemptCnt++;

                if (requestContext.MethodType == WebMethodType.GET)
                {
                    rawData = await flurlRequest.GetBytesAsync();
                }
                else if (requestContext.MethodType == WebMethodType.POST)
                {
                    rawData = await flurlRequest
                              .PostAsync(new ByteArrayContent(requestContext))
                              .ReceiveBytes();
                }
            }
            catch (FlurlHttpException flurlException)
            {
                if (requestContext.AttemptCnt < WebConfig.ReTryCount)
                {
                    rawData = await RequestRetryPolicy(flurlException, flurlRequest, requestContext);
                }
                else
                {
                    if (_exceptionHandler != null)
                    {
                        await _exceptionHandler.Invoke(flurlException);
                    }
                }
            }
            catch (Exception ex)
            {
                if (_exceptionHandler != null)
                {
                    await _exceptionHandler.Invoke(ex);
                }
            }

            return(rawData);
        }
Example #2
0
        private static async Task <TOut> EncryptSendAsync <TOut>(IFlurlRequest flurlRequest, WebRequestContext requestContext)
        {
            TOut result = default;

            try
            {
                requestContext.AttemptCnt++;

                byte[] e_rawData = null;
                if (requestContext.MethodType == WebMethodType.GET)
                {
                    e_rawData = await flurlRequest.GetBytesAsync();
                }
                else if (requestContext.MethodType == WebMethodType.POST)
                {
                    e_rawData = await flurlRequest
                                .PostAsync(new ByteArrayContent(requestContext))
                                .ReceiveBytes();
                }

                var rawData = CryptoFacade.Instance.Decrypt_AES(e_rawData);
                result = MessagePackSerializer.Deserialize <TOut>(rawData);
            }
            catch (FlurlHttpException flurlException)
            {
                if (requestContext.AttemptCnt < WebConfig.ReTryCount)
                {
                    result = await RequestRetryPolicy <TOut>(flurlException, flurlRequest, requestContext);
                }
                else
                {
                    if (_exceptionHandler != null)
                    {
                        await _exceptionHandler.Invoke(flurlException);
                    }
                }
            }
            catch (CryptographicException cryptoEx)
            {
                if (requestContext.AttemptCnt < WebConfig.ReTryCount)
                {
                    result = await EncryptSendAsync <TOut>(flurlRequest, requestContext);
                }
                else
                {
                    if (_exceptionHandler != null)
                    {
                        await _exceptionHandler.Invoke(cryptoEx);
                    }
                }
            }
            catch (Exception ex)
            {
                if (_exceptionHandler != null)
                {
                    await _exceptionHandler.Invoke(ex);
                }
            }

            return(result);
        }
Example #3
0
        private static async Task <TOut> SendAsync <TOut>(IFlurlRequest flurlRequest, WebRequestContext requestContext)
        {
            TOut result = default;

            try
            {
                requestContext.AttemptCnt++;

                if (requestContext.MethodType == WebMethodType.GET)
                {
                    if (requestContext.SerializeType == SerializeType.Json)
                    {
                        result = await flurlRequest.GetJsonAsync <TOut>();
                    }
                    else
                    {
                        var rawData = await flurlRequest.GetBytesAsync();

                        result = MessagePackSerializer.Deserialize <TOut>(rawData);
                    }
                }
                else if (requestContext.MethodType == WebMethodType.POST)
                {
                    if (requestContext.SerializeType == SerializeType.Json)
                    {
                        result = await flurlRequest
                                 .PostJsonAsync((string)requestContext)
                                 .ReceiveJson <TOut>();
                    }
                    else
                    {
                        var rawData = await flurlRequest
                                      .PostAsync(new ByteArrayContent(requestContext))
                                      .ReceiveBytes();

                        result = MessagePackSerializer.Deserialize <TOut>(rawData);
                    }
                }
            }
            catch (FlurlHttpException flurlException)
            {
                if (requestContext.AttemptCnt < WebConfig.ReTryCount)
                {
                    result = await RequestRetryPolicy <TOut>(flurlException, flurlRequest, requestContext);
                }
                else
                {
                    if (_exceptionHandler != null)
                    {
                        await _exceptionHandler.Invoke(flurlException);
                    }
                }
            }
            catch (Exception ex)
            {
                if (_exceptionHandler != null)
                {
                    await _exceptionHandler.Invoke(ex);
                }
            }

            return(result);
        }