apiversions.go 1.7 KB

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