示例#1
0
        private void HandleIfErrorResponse(RegistryApiResponse response)
        {
            // If no customer handlers just default the response.
            foreach (var handler in _errorHandlers)
            {
                handler(response);
            }

            // No custom handler was fired. Default the response for generic success/failures.
            if (response.StatusCode < HttpStatusCode.OK || response.StatusCode >= HttpStatusCode.BadRequest)
            {
                throw new RegistryApiException(response);
            }
        }
示例#2
0
        internal async Task <RegistryApiResponse <Stream> > MakeRequestForStreamedResponseAsync(
            CancellationToken cancellationToken,
            HttpMethod method,
            string path,
            IQueryString queryString = null)
        {
            var response = await InternalMakeRequestAsync(s_InfiniteTimeout, HttpCompletionOption.ResponseHeadersRead, method, path, queryString, null, null, cancellationToken);

            var body = await response.Content.ReadAsStreamAsync();

            var apiResponse = new RegistryApiResponse <Stream>(response.StatusCode, body, response.Headers);

            HandleIfErrorResponse(apiResponse);

            return(apiResponse);
        }
示例#3
0
        internal async Task <RegistryApiResponse <string> > MakeRequestAsync(
            CancellationToken cancellationToken,
            HttpMethod method,
            string path,
            IQueryString queryString             = null,
            IDictionary <string, string> headers = null,
            Func <HttpContent> content           = null)
        {
            using (var response = await InternalMakeRequestAsync(DefaultTimeout,
                                                                 HttpCompletionOption.ResponseContentRead, method, path, queryString, headers, content, cancellationToken))
            {
                var responseBody = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                var apiResponse = new RegistryApiResponse <string>(response.StatusCode, responseBody, response.Headers);

                HandleIfErrorResponse(apiResponse);

                return(apiResponse);
            }
        }
 internal RegistryApiException(RegistryApiResponse response)
     : base($"Docker API responded with status code={response.StatusCode}")
 {
     StatusCode = response.StatusCode;
     Headers    = response.Headers;
 }
示例#5
0
 /// <summary>
 /// Attempts to retrieve the value of a response header.
 /// </summary>
 /// <param name="response"></param>
 /// <param name="name"></param>
 /// <returns>The first value if one is found, null otherwise.</returns>
 public static string GetHeader(this RegistryApiResponse response, string name)
 {
     return(response.Headers
            .FirstOrDefault(h => h.Key == name).Value?.FirstOrDefault());
 }