예제 #1
0
        /// <summary>
        /// Sends a raw REST request to the API. Includes error handling logic but no retry logic.
        /// </summary>
        /// <param name="method">The request method.</param>
        /// <param name="path">The request path.</param>
        /// <param name="content">The request content, if any.</param>
        /// <param name="formatBaseApiUri">Whether to format the provided path with the client's <see cref="BaseApiUri "/>.</param>
        async Task <HttpResponseMessage> IApiRequestor.RawRequestAsync(HttpMethod method, string path, HttpContent content, bool formatBaseApiUri, CancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();

            if (Authentication == null)
            {
                throw new NullReferenceException("No authentication provided.");
            }

            var request = new HttpRequestMessage(method, formatBaseApiUri ? BaseApiUri + path : path);

            Authentication.ApplyToRequest(request);

            if (method != HttpMethod.Get)
            {
                request.Content = content;
            }

            var response = await HttpClient.SendAsync(request, cancellationToken).ConfigureAwait(false);

            if (response.IsSuccessStatusCode)
            {
                return(response);
            }

            cancellationToken.ThrowIfCancellationRequested();

            ApiError error;

            try
            {
                error = JsonConvert.DeserializeObject <ApiError>(await response.Content.ReadAsStringAsync().ConfigureAwait(false));
            }
            catch (Exception)
            {
                throw ApiException.InvalidServerResponse(response);
            }

            throw new ApiException("An API exception occurred.", response, error);
        }