func (t *APIRequester) Do(req *http.Request) (*http.Response, error) {
	resp, err := t.transport.Do(req)
	if err != nil {
		return nil, errors.Trace(err)
	}

	if resp.StatusCode >= http.StatusOK && resp.StatusCode <= http.StatusNoContent {
		return resp, nil
	}

	var potentialInvalidURL bool
	if resp.StatusCode == http.StatusNotFound {
		potentialInvalidURL = true
	} else if resp.StatusCode >= http.StatusInternalServerError && resp.StatusCode <= http.StatusNetworkAuthenticationRequired {
		defer func() {
			_, _ = io.Copy(ioutil.Discard, resp.Body)
			_ = resp.Body.Close()
		}()
		return nil, errors.Errorf(`server error %q`, req.URL.String())
	}

	// We expect that we always have a valid content-type from the server, once
	// we've checked that we don't get a 5xx error. Given that we send Accept
	// header of application/json, I would only ever expect to see that.
	// Everything will be incorrectly formatted.
	if contentType := resp.Header.Get("Content-Type"); contentType != JSON {
		defer func() {
			_, _ = io.Copy(ioutil.Discard, resp.Body)
			_ = resp.Body.Close()
		}()

		if potentialInvalidURL {
			return nil, errors.Errorf(`unexpected charm-hub url %q when parsing headers`, req.URL.String())
		}
		return nil, errors.Errorf(`unexpected content-type from server %q`, contentType)
	}

	return resp, nil
}
// Do performs the *http.Request and returns a *http.Response or an error
// if it fails to construct the transport.
Do performs the *http.Request and returns a *http.Response or an error
if it fails to construct the transport.
// Do performs the * http . Request and returns a * http . Response or an error // if it fails to construct the transport .
func (t *APIRequester) Do(req *http.Request) (*http.Response, error) {
	resp, err := t.transport.Do(req)
	if err != nil {
		return nil, errors.Trace(err)
	}
	if resp.StatusCode >= http.StatusOK && resp.StatusCode <= http.StatusNoContent {
		return resp, nil
	}
	var potentialInvalidURL bool
	if resp.StatusCode == http.StatusNotFound {
		potentialInvalidURL = true
	} else if resp.StatusCode >= http.StatusInternalServerError && resp.StatusCode <= http.StatusNetworkAuthenticationRequired {
		defer func() {
			_, _ = io.Copy(ioutil.Discard, resp.Body)
			_ = resp.Body.Close()
		}()
		return nil, errors.Errorf(`server error %q`, req.URL.String())
	}
	if contentType := resp.Header.Get("Content-Type"); contentType != JSON {
		defer func() {
			_, _ = io.Copy(ioutil.Discard, resp.Body)
			_ = resp.Body.Close()
		}()
		if potentialInvalidURL {
			return nil, errors.Errorf(`unexpected charm-hub url %q when parsing headers`, req.URL.String())
		}
		return nil, errors.Errorf(`unexpected content-type from server %q`, contentType)
	}
	return resp, nil
}
func ( t * APIRequester ) Do ( req * http . Request ) ( * http . Response , error ) { resp , err := t . transport . Do ( req ) if err != nil { return nil , errors . Trace ( err ) } if resp . StatusCode >= http . StatusOK && resp . StatusCode <= http . StatusNoContent { return resp , nil } var potentialInvalidURL bool if resp . StatusCode == http . StatusNotFound { potentialInvalidURL = true } else if resp . StatusCode >= http . StatusInternalServerError && resp . StatusCode <= http . StatusNetworkAuthenticationRequired { defer func ( ) { _ , _ = io . Copy ( ioutil . Discard , resp . Body ) _ = resp . Body . Close ( ) } ( ) return nil , errors . Errorf ( `server error %q` , req . URL . String ( ) ) } if contentType := resp . Header . Get ( " " ) ; contentType != JSON { defer func ( ) { _ , _ = io . Copy ( ioutil . Discard , resp . Body ) _ = resp . Body . Close ( ) } ( ) if potentialInvalidURL { return nil , errors . Errorf ( `unexpected charm-hub url %q when parsing headers` , req . URL . String ( ) ) } return nil , errors . Errorf ( `unexpected content-type from server %q` , contentType ) } return resp , nil }
Do performs the *http.Request and returns a *http.Response or an error
if it fails to construct the transport.
Do performs the * http . Request and returns a * http . Response or an error if it fails to construct the transport .
// We expect that we always have a valid content-type from the server, once // we've checked that we don't get a 5xx error. Given that we send Accept // header of application/json, I would only ever expect to see that. // Everything will be incorrectly formatted.