github.com/hoveychen/kafka-go@v0.4.42/apiversions.go (about)

     1  package kafka
     2  
     3  import (
     4  	"context"
     5  	"net"
     6  
     7  	"github.com/hoveychen/kafka-go/protocol"
     8  	"github.com/hoveychen/kafka-go/protocol/apiversions"
     9  )
    10  
    11  // ApiVersionsRequest is a request to the ApiVersions API.
    12  type ApiVersionsRequest struct {
    13  	// Address of the kafka broker to send the request to.
    14  	Addr net.Addr
    15  }
    16  
    17  // ApiVersionsResponse is a response from the ApiVersions API.
    18  type ApiVersionsResponse struct {
    19  	// Error is set to a non-nil value if an error was encountered.
    20  	Error error
    21  
    22  	// ApiKeys contains the specific details of each supported API.
    23  	ApiKeys []ApiVersionsResponseApiKey
    24  }
    25  
    26  // ApiVersionsResponseApiKey includes the details of which versions are supported for a single API.
    27  type ApiVersionsResponseApiKey struct {
    28  	// ApiKey is the ID of the API.
    29  	ApiKey int
    30  
    31  	// ApiName is a human-friendly description of the API.
    32  	ApiName string
    33  
    34  	// MinVersion is the minimum API version supported by the broker.
    35  	MinVersion int
    36  
    37  	// MaxVersion is the maximum API version supported by the broker.
    38  	MaxVersion int
    39  }
    40  
    41  func (c *Client) ApiVersions(
    42  	ctx context.Context,
    43  	req *ApiVersionsRequest,
    44  ) (*ApiVersionsResponse, error) {
    45  	apiReq := &apiversions.Request{}
    46  	protoResp, err := c.roundTrip(
    47  		ctx,
    48  		req.Addr,
    49  		apiReq,
    50  	)
    51  	if err != nil {
    52  		return nil, err
    53  	}
    54  	apiResp := protoResp.(*apiversions.Response)
    55  
    56  	resp := &ApiVersionsResponse{
    57  		Error: makeError(apiResp.ErrorCode, ""),
    58  	}
    59  	for _, apiKey := range apiResp.ApiKeys {
    60  		resp.ApiKeys = append(
    61  			resp.ApiKeys,
    62  			ApiVersionsResponseApiKey{
    63  				ApiKey:     int(apiKey.ApiKey),
    64  				ApiName:    protocol.ApiKey(apiKey.ApiKey).String(),
    65  				MinVersion: int(apiKey.MinVersion),
    66  				MaxVersion: int(apiKey.MaxVersion),
    67  			},
    68  		)
    69  	}
    70  
    71  	return resp, err
    72  }