Exemplo n.º 1
0
        public async Task <T> ExecuteAsync <T, U>(Request request, Interfaces.IAdapterFactory <T, U> factory)
            where U : new()
        {
            var taskCompletionSource = new TaskCompletionSource <T>();

            _RestClient.ExecuteAsync <U>(request.Build(), (response, handle) =>
            {
                try
                {
                    if (!response.IsSuccessful)
                    {
                        taskCompletionSource.SetException(new InvalidOperationException($"Exception caught! {response.StatusDescription} (status code {response.StatusCode})", response.ErrorException));
                    }
                    else
                    {
                        // If callback is successful, build the adapter from the Json source
                        taskCompletionSource.SetResult(factory.Build(response.Data));
                    }
                }
                catch (Exception ex)
                {
                    taskCompletionSource.SetException(ex);
                }
            });

            return(await taskCompletionSource.Task.ConfigureAwait(false));
        }
Exemplo n.º 2
0
        public T Execute <T, U>(Request request, Interfaces.IAdapterFactory <T, U> factory)
            where U : new()
        {
            var response = _RestClient.Execute <U>(request.Build());

            if (!response.IsSuccessful)
            {
                throw new InvalidOperationException($"Bad response! {response.StatusDescription} (code: {response.StatusCode}).", response.ErrorException);
            }

            return(factory.Build(response.Data));
        }