method.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. from __future__ import annotations
  2. __all__ = ["IdempotencyLevel", "MethodInfo"]
  3. import enum
  4. from dataclasses import dataclass
  5. from typing import Generic, TypeVar
  6. REQ = TypeVar("REQ")
  7. RES = TypeVar("RES")
  8. class IdempotencyLevel(enum.Enum):
  9. """The level of idempotency of an RPC method.
  10. This value can affect RPC behaviors, such as determining whether it is safe to
  11. retry a request, or what kinds of request modalities are allowed for a given
  12. procedure.
  13. """
  14. UNKNOWN = enum.auto()
  15. """The default idempotency level.
  16. A method with this idempotency level may not be idempotent. This is appropriate for
  17. any kind of method.
  18. """
  19. NO_SIDE_EFFECTS = enum.auto()
  20. """The idempotency level that specifies that a given call has no side-effects.
  21. This is equivalent to [RFC 9110 § 9.2.1] "safe" methods in terms of semantics.
  22. This procedure should not mutate any state. This idempotency level is appropriate
  23. for queries, or anything that would be suitable for an HTTP GET request. In addition,
  24. due to the lack of side-effects, such a procedure would be suitable to retry and
  25. expect that the results will not be altered by preceding attempts.
  26. [RFC 9110 § 9.2.1]: https://www.rfc-editor.org/rfc/rfc9110.html#section-9.2.1
  27. """
  28. IDEMPOTENT = enum.auto()
  29. """The idempotency level that specifies that a given call is "idempotent",
  30. such that multiple instances of the same request to this procedure would have
  31. the same side-effects as a single request.
  32. This is equivalent to [RFC 9110 § 9.2.2] "idempotent" methods.
  33. This level is a subset of the previous level. This idempotency level is
  34. appropriate for any procedure that is safe to retry multiple times
  35. and be guaranteed that the response and side-effects will not be altered
  36. as a result of multiple attempts, for example, entity deletion requests.
  37. [RFC 9110 § 9.2.2]: https://www.rfc-editor.org/rfc/rfc9110.html#section-9.2.2
  38. """
  39. @dataclass(kw_only=True, frozen=True, slots=True)
  40. class MethodInfo(Generic[REQ, RES]):
  41. """Information about a RPC method within a service."""
  42. name: str
  43. """The name of the method within the service."""
  44. service_name: str
  45. """The fully qualified service name containing the method."""
  46. input: type[REQ]
  47. """The input message type of the method."""
  48. output: type[RES]
  49. """The output message type of the method."""
  50. idempotency_level: IdempotencyLevel
  51. """The [IdempotencyLevel][] of the method."""