Esempio n. 1
0
        public async Task <JsonResponse> ExecuteAsync(JsonRequest request)
        {
            var cancelToken = new CancellationToken(false);

            Task <HttpResponseMessage> responseTask;

            request.ProcessHeaders(_rest.DefaultRequestHeaders);

            switch (request.Method.Method.ToLower())
            {
            case "post":
                responseTask = _rest.GetAsync(request.ResourcePath, HttpCompletionOption.ResponseContentRead, cancelToken);
                break;

            case "put":
                responseTask = _rest.GetAsync(request.ResourcePath, HttpCompletionOption.ResponseContentRead, cancelToken);
                break;

            case "delete":
                responseTask = _rest.GetAsync(request.ResourcePath, HttpCompletionOption.ResponseContentRead, cancelToken);
                break;

            case "get":
            default:
                responseTask = _rest.GetAsync(request.ResourcePath, HttpCompletionOption.ResponseContentRead, cancelToken);
                break;
            }

            var response = await responseTask;

            response.EnsureSuccessStatusCode();
            return(new JsonResponse
            {
                AsyncToken = cancelToken,
                StatusCode = (int)response.StatusCode,
                IsSuccess = response.IsSuccessStatusCode,
                ResponseHeaders = response.Content.Headers
                                  .ToDictionary(x => x.Key, x => string.Join("; ", x.Value)),
                ResponseBody = await response.Content.ReadAsStringAsync()
            });
        }