// Do is the lower level of a client call, manually sends an HTTP request to the lenses box backend based on the `Client#Config`
// and returns an HTTP response.
Do is the lower level of a client call, manually sends an HTTP request to the lenses box backend based on the `Client#Config`
and returns an HTTP response.
// Do is the lower level of a client call manually sends an HTTP request to the lenses box backend based on the ` Client#Config ` // and returns an HTTP response .
func (c *Client) Do(method, path, contentType string, send []byte, options ...RequestOption) (*http.Response, error) {
	if path[0] == '/' { 
		path = path[1:]
	}
	uri := c.Config.Host + "/" + path
	golog.Debugf("Client#Do.req:\n\turi: %s:%s\n\tsend: %s", method, uri, string(send))
	req, err := http.NewRequest(method, uri, acquireBuffer(send))
	if err != nil {
		return nil, err
	}
	u, err := url.Parse(c.Config.Host)
	hostHeader := u.Host
	userAgentHeader := "lenses-cli/" + BuildVersion
	req.Header.Set("Host", hostHeader)
	req.Header.Set("User-Agent", userAgentHeader)
	if c.Config.Token != "" {
		req.Header.Set(xKafkaLensesTokenHeaderKey, c.Config.Token)
	}
	if contentType != "" {
		req.Header.Set(contentTypeHeaderKey, contentType)
	}
	req.Header.Add(acceptEncodingHeaderKey, gzipEncodingHeaderValue)
	if c.PersistentRequestModifier != nil {
		if err := c.PersistentRequestModifier(req); err != nil {
			return nil, err
		}
	}
	for _, opt := range options {
		if err = opt(req); err != nil {
			return nil, err
		}
	}
	golog.Debugf("Client#Do.req.Headers: %#+v", req.Header)
	resp, err := c.client.Do(req)
	if err != nil {
		return nil, err
	}
	if !isAuthorized(resp) {
		resp.Body.Close() 
		return nil, ErrCredentialsMissing
	}
	if !isOK(resp) {
		defer resp.Body.Close()
		var errBody string
		if cType := resp.Header.Get(contentTypeHeaderKey); strings.Contains(cType, contentTypeJSON) ||
			strings.Contains(cType, contentTypeSchemaJSON) {
			bodyBytes, _ := ioutil.ReadAll(resp.Body)
			resp.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
			var jsonErr interface{}
			if err = c.ReadJSON(resp, &jsonErr); err != nil {
				return nil, err
			}
			errFieldsMap, ok := jsonErr.(map[string]interface{})
			if ok {
				var jsonErrResp jsonResourceErrorV2
				err = mapstructure.Decode(errFieldsMap, &jsonErrResp)
				if err != nil {
					return nil, err
				}
				if jsonErrResp.ErrorType != "" {
					errBody = fmt.Sprintf("%s ", jsonErrResp.ErrorType)
				}
				for i := range jsonErrResp.Fields {
					for k, v := range jsonErrResp.Fields[i] {
						errBody = fmt.Sprintf("%s%s:%s, ", errBody, k, v)
					}
				}
				errBody = strings.TrimSuffix(errBody, ", ")
			} else {
				var jsonErrResp []jsonResourceError
				err = mapstructure.Decode(jsonErr, &jsonErrResp)
				for _, jsonError := range jsonErrResp {
					if jsonError.ErrorType != "" {
						errBody = fmt.Sprintf("%s", jsonError.ErrorType)
					}
					errBody = fmt.Sprintf("%s", errBody)
				}
			}
		}
		if errBody == "" {
			b, err := c.ReadResponseBody(resp)
			if err != nil {
				errBody = " unable to read body: " + err.Error()
			} else {
				errBody = string(b)
			}
		}
		if errBody == "" {
			errBody = fmt.Sprintf("Response returned status code %d", resp.StatusCode)
		}
		return nil, NewResourceError(resp.StatusCode, uri, method, errBody)
	}
	return resp, nil
}
func ( c * Client ) Do ( method , path , contentType string , send [ ] byte , options ... RequestOption ) ( * http . Response , error ) { if path [ 0 ] == '/' { path = path [ 1 : ] } uri := c . Config . Host + " " + path golog . Debugf ( " \n \t \n \t " , method , uri , string ( send ) ) req , err := http . NewRequest ( method , uri , acquireBuffer ( send ) ) if err != nil { return nil , err } u , err := url . Parse ( c . Config . Host ) hostHeader := u . Host userAgentHeader := " " + BuildVersion req . Header . Set ( " " , hostHeader ) req . Header . Set ( " " , userAgentHeader ) if c . Config . Token != " " { req . Header . Set ( xKafkaLensesTokenHeaderKey , c . Config . Token ) } if contentType != " " { req . Header . Set ( contentTypeHeaderKey , contentType ) } req . Header . Add ( acceptEncodingHeaderKey , gzipEncodingHeaderValue ) if c . PersistentRequestModifier != nil { if err := c . PersistentRequestModifier ( req ) ; err != nil { return nil , err } } for _ , opt := range options { if err = opt ( req ) ; err != nil { return nil , err } } golog . Debugf ( " " , req . Header ) resp , err := c . client . Do ( req ) if err != nil { return nil , err } if ! isAuthorized ( resp ) { resp . Body . Close ( ) return nil , ErrCredentialsMissing } if ! isOK ( resp ) { defer resp . Body . Close ( ) var errBody string if cType := resp . Header . Get ( contentTypeHeaderKey ) ; strings . Contains ( cType , contentTypeJSON ) || strings . Contains ( cType , contentTypeSchemaJSON ) { bodyBytes , _ := ioutil . ReadAll ( resp . Body ) resp . Body = ioutil . NopCloser ( bytes . NewBuffer ( bodyBytes ) ) var jsonErr interface { } if err = c . ReadJSON ( resp , & jsonErr ) ; err != nil { return nil , err } errFieldsMap , ok := jsonErr . ( map [ string ] interface { } ) if ok { var jsonErrResp jsonResourceErrorV2 err = mapstructure . Decode ( errFieldsMap , & jsonErrResp ) if err != nil { return nil , err } if jsonErrResp . ErrorType != " " { errBody = fmt . Sprintf ( " " , jsonErrResp . ErrorType ) } for i := range jsonErrResp . Fields { for k , v := range jsonErrResp . Fields [ i ] { errBody = fmt . Sprintf ( " " , errBody , k , v ) } } errBody = strings . TrimSuffix ( errBody , " " ) } else { var jsonErrResp [ ] jsonResourceError err = mapstructure . Decode ( jsonErr , & jsonErrResp ) for _ , jsonError := range jsonErrResp { if jsonError . ErrorType != " " { errBody = fmt . Sprintf ( " " , jsonError . ErrorType ) } errBody = fmt . Sprintf ( " " , errBody ) } } } if errBody == " " { b , err := c . ReadResponseBody ( resp ) if err != nil { errBody = " " + err . Error ( ) } else { errBody = string ( b ) } } if errBody == " " { errBody = fmt . Sprintf ( " " , resp . StatusCode ) } return nil , NewResourceError ( resp . StatusCode , uri , method , errBody ) } return resp , nil }
Do is the lower level of a client call, manually sends an HTTP request to the lenses box backend based on the `Client#Config`
and returns an HTTP response.
Do is the lower level of a client call manually sends an HTTP request to the lenses box backend based on the ` Client#Config ` and returns an HTTP response .
// remove beginning slash, if any. // before sending requests here. // Set explicit host and user-agent header // set the token header. // set the content type if any. // response accept gzipped content. // here will print all the headers, including the token (because it may be useful for debugging) // --so bug reporters should be careful here to invalidate the token after that. // send the request and check the response for any connection & authorization errors here. // close the body here so we don't have leaks. // read it, it's an error in JSON format. // else give the whole body to the error context, i.e from "text/plain", "text/html" etc.