コード例 #1
0
        public IEnumerable <WeatherForecast> Get(Action <HttpResponseMessage> ResponseCallback = null, Action <FlurlHttpException> ExceptionCallback = null, IDictionary <String, Object> headers = null, IEnumerable <Cookie> cookies = null, TimeSpan?timeout = null, CancellationToken cancellationToken = default)
        {
            var    controller            = "WeatherForecast";
            string url                   = $@"{controller}/";
            HttpResponseMessage response = null;

            response = HttpOverride.GetResponseAsync(HttpMethod.Get, url, null, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
            bool responseHandled = response != null;

            if (response == null)
            {
                try
                {
                    response = Client.ClientWrapper.Request(url).WithRequestModifiers(Modifier).WithCookies(cookies).WithHeaders(headers).WithTimeout(timeout ?? Client.Timeout).AllowAnyHttpStatus().GetAsync(cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (FlurlHttpException fhex)
                {
                    if (ExceptionCallback != null && ExceptionCallback.Method.IsDefined(typeof(AsyncStateMachineAttribute), true))
                    {
                        throw new NotSupportedException("Async void action delegates for ExceptionCallback are not supported.As they will run out of the scope of this call.");
                    }

                    if (ExceptionCallback != null)
                    {
                        responseHandled = true;
                        ExceptionCallback?.Invoke(fhex);
                    }
                    else
                    {
                        throw fhex;
                    }

                    return(default(IEnumerable <WeatherForecast>));
                }

                HttpOverride.OnNonOverridedResponseAsync(HttpMethod.Get, url, null, response, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
            }

            if (ResponseCallback != null && ResponseCallback.Method.IsDefined(typeof(AsyncStateMachineAttribute), true))
            {
                throw new NotSupportedException("Async void action delegates for ResponseCallback are not supported.As they will run out of the scope of this call.");
            }

            if (ResponseCallback != null)
            {
                responseHandled = true;
                ResponseCallback.Invoke(response);
            }

            if (response.IsSuccessStatusCode)
            {
                return(Serializer.Deserialize <IEnumerable <WeatherForecast> >(response.Content).ConfigureAwait(false).GetAwaiter().GetResult());
            }
            else
            {
                return(default(IEnumerable <WeatherForecast>));
            }
        }
コード例 #2
0
        public void Function1_GET(string name, Guid ID, String AuthKey, Action <string> OKCallback = null, Action <string> BadRequestCallback = null, Action <HttpResponseMessage> ResponseCallback = null, Action <FlurlHttpException> ExceptionCallback = null, Action <HttpResponseMessage> UnauthorizedCallback = null, IDictionary <String, Object> headers = null, IEnumerable <Cookie> cookies = null, TimeSpan?timeout = null, CancellationToken cancellationToken = default)
        {
            string url = $@"/api/helloMe?{nameof(name)}={name.EncodeForUrl()}";
            HttpResponseMessage response = null;

            response = HttpOverride.GetResponseAsync(HttpMethod.Get, url, null, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
            bool responseHandled = response != null;

            if (response == null)
            {
                try
                {
                    response = Client.ClientWrapper.Request(url).WithCookies(cookies).WithHeaders(headers).WithTimeout(timeout ?? Client.Timeout).WithFunctionAuthorizationKey(AuthKey).WithHeader("ID", ID).AllowAnyHttpStatus().GetAsync(cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
                }
                catch (FlurlHttpException fhex)
                {
                    if (ExceptionCallback != null && ExceptionCallback.Method.IsDefined(typeof(AsyncStateMachineAttribute), true))
                    {
                        throw new NotSupportedException("Async void action delegates for ExceptionCallback are not supported.As they will run out of the scope of this call.");
                    }

                    if (ExceptionCallback != null)
                    {
                        responseHandled = true;
                        ExceptionCallback?.Invoke(fhex);
                    }
                    else
                    {
                        throw fhex;
                    }

                    return;
                }

                HttpOverride.OnNonOverridedResponseAsync(HttpMethod.Get, url, null, response, cancellationToken).ConfigureAwait(false).GetAwaiter().GetResult();
            }

            if (OKCallback != null && OKCallback.Method.IsDefined(typeof(AsyncStateMachineAttribute), true))
            {
                throw new NotSupportedException("Async void action delegates for OKCallback are not supported.As they will run out of the scope of this call.");
            }

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                if (OKCallback != null)
                {
                    responseHandled = true;
                    OKCallback.Invoke(Serializer.Deserialize <string>(response.Content).ConfigureAwait(false).GetAwaiter().GetResult());
                }
            }

            if (BadRequestCallback != null && BadRequestCallback.Method.IsDefined(typeof(AsyncStateMachineAttribute), true))
            {
                throw new NotSupportedException("Async void action delegates for BadRequestCallback are not supported.As they will run out of the scope of this call.");
            }

            if (response.StatusCode == System.Net.HttpStatusCode.BadRequest)
            {
                if (BadRequestCallback != null)
                {
                    responseHandled = true;
                    BadRequestCallback.Invoke(Serializer.Deserialize <string>(response.Content).ConfigureAwait(false).GetAwaiter().GetResult());
                }
            }

            if (ResponseCallback != null && ResponseCallback.Method.IsDefined(typeof(AsyncStateMachineAttribute), true))
            {
                throw new NotSupportedException("Async void action delegates for ResponseCallback are not supported.As they will run out of the scope of this call.");
            }

            if (ResponseCallback != null)
            {
                responseHandled = true;
                ResponseCallback.Invoke(response);
            }

            if (UnauthorizedCallback != null && UnauthorizedCallback.Method.IsDefined(typeof(AsyncStateMachineAttribute), true))
            {
                throw new NotSupportedException("Async void action delegates for UnauthorizedCallback are not supported.As they will run out of the scope of this call.");
            }

            if (response.StatusCode == System.Net.HttpStatusCode.Unauthorized)
            {
                if (UnauthorizedCallback != null)
                {
                    responseHandled = true;
                    UnauthorizedCallback.Invoke(Serializer.Deserialize <HttpResponseMessage>(response.Content).ConfigureAwait(false).GetAwaiter().GetResult());
                }
            }

            if (!responseHandled)
            {
                throw new System.InvalidOperationException($"Response Status of {response.StatusCode} was not handled properly.");
            }

            return;
        }