api.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from __future__ import absolute_import
  2. import abc
  3. from .struct import Struct
  4. from .types import Int16, Int32, String, Schema
  5. class RequestHeader(Struct):
  6. SCHEMA = Schema(
  7. ('api_key', Int16),
  8. ('api_version', Int16),
  9. ('correlation_id', Int32),
  10. ('client_id', String('utf-8'))
  11. )
  12. def __init__(self, request, correlation_id=0, client_id='kafka-python'):
  13. super(RequestHeader, self).__init__(
  14. request.API_KEY, request.API_VERSION, correlation_id, client_id
  15. )
  16. class Request(Struct):
  17. __metaclass__ = abc.ABCMeta
  18. @abc.abstractproperty
  19. def API_KEY(self):
  20. """Integer identifier for api request"""
  21. pass
  22. @abc.abstractproperty
  23. def API_VERSION(self):
  24. """Integer of api request version"""
  25. pass
  26. @abc.abstractproperty
  27. def SCHEMA(self):
  28. """An instance of Schema() representing the request structure"""
  29. pass
  30. @abc.abstractproperty
  31. def RESPONSE_TYPE(self):
  32. """The Response class associated with the api request"""
  33. pass
  34. def expect_response(self):
  35. """Override this method if an api request does not always generate a response"""
  36. return True
  37. class Response(Struct):
  38. __metaclass__ = abc.ABCMeta
  39. @abc.abstractproperty
  40. def API_KEY(self):
  41. """Integer identifier for api request/response"""
  42. pass
  43. @abc.abstractproperty
  44. def API_VERSION(self):
  45. """Integer of api request/response version"""
  46. pass
  47. @abc.abstractproperty
  48. def SCHEMA(self):
  49. """An instance of Schema() representing the response structure"""
  50. pass