face.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. # Copyright 2015 gRPC authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Interfaces defining the Face layer of RPC Framework."""
  15. import abc
  16. import collections
  17. import enum
  18. # cardinality, style, abandonment, future, and stream are
  19. # referenced from specification in this module.
  20. from grpc.framework.common import cardinality # pylint: disable=unused-import
  21. from grpc.framework.common import style # pylint: disable=unused-import
  22. from grpc.framework.foundation import future # pylint: disable=unused-import
  23. from grpc.framework.foundation import stream # pylint: disable=unused-import
  24. import six
  25. # pylint: disable=too-many-arguments
  26. class NoSuchMethodError(Exception):
  27. """Raised by customer code to indicate an unrecognized method.
  28. Attributes:
  29. group: The group of the unrecognized method.
  30. name: The name of the unrecognized method.
  31. """
  32. def __init__(self, group, method):
  33. """Constructor.
  34. Args:
  35. group: The group identifier of the unrecognized RPC name.
  36. method: The method identifier of the unrecognized RPC name.
  37. """
  38. super(NoSuchMethodError, self).__init__()
  39. self.group = group
  40. self.method = method
  41. def __repr__(self):
  42. return 'face.NoSuchMethodError(%s, %s)' % (
  43. self.group,
  44. self.method,
  45. )
  46. class Abortion(
  47. collections.namedtuple('Abortion', (
  48. 'kind',
  49. 'initial_metadata',
  50. 'terminal_metadata',
  51. 'code',
  52. 'details',
  53. ))):
  54. """A value describing RPC abortion.
  55. Attributes:
  56. kind: A Kind value identifying how the RPC failed.
  57. initial_metadata: The initial metadata from the other side of the RPC or
  58. None if no initial metadata value was received.
  59. terminal_metadata: The terminal metadata from the other side of the RPC or
  60. None if no terminal metadata value was received.
  61. code: The code value from the other side of the RPC or None if no code value
  62. was received.
  63. details: The details value from the other side of the RPC or None if no
  64. details value was received.
  65. """
  66. @enum.unique
  67. class Kind(enum.Enum):
  68. """Types of RPC abortion."""
  69. CANCELLED = 'cancelled'
  70. EXPIRED = 'expired'
  71. LOCAL_SHUTDOWN = 'local shutdown'
  72. REMOTE_SHUTDOWN = 'remote shutdown'
  73. NETWORK_FAILURE = 'network failure'
  74. LOCAL_FAILURE = 'local failure'
  75. REMOTE_FAILURE = 'remote failure'
  76. class AbortionError(six.with_metaclass(abc.ABCMeta, Exception)):
  77. """Common super type for exceptions indicating RPC abortion.
  78. initial_metadata: The initial metadata from the other side of the RPC or
  79. None if no initial metadata value was received.
  80. terminal_metadata: The terminal metadata from the other side of the RPC or
  81. None if no terminal metadata value was received.
  82. code: The code value from the other side of the RPC or None if no code value
  83. was received.
  84. details: The details value from the other side of the RPC or None if no
  85. details value was received.
  86. """
  87. def __init__(self, initial_metadata, terminal_metadata, code, details):
  88. super(AbortionError, self).__init__()
  89. self.initial_metadata = initial_metadata
  90. self.terminal_metadata = terminal_metadata
  91. self.code = code
  92. self.details = details
  93. def __str__(self):
  94. return '%s(code=%s, details="%s")' % (self.__class__.__name__,
  95. self.code, self.details)
  96. class CancellationError(AbortionError):
  97. """Indicates that an RPC has been cancelled."""
  98. class ExpirationError(AbortionError):
  99. """Indicates that an RPC has expired ("timed out")."""
  100. class LocalShutdownError(AbortionError):
  101. """Indicates that an RPC has terminated due to local shutdown of RPCs."""
  102. class RemoteShutdownError(AbortionError):
  103. """Indicates that an RPC has terminated due to remote shutdown of RPCs."""
  104. class NetworkError(AbortionError):
  105. """Indicates that some error occurred on the network."""
  106. class LocalError(AbortionError):
  107. """Indicates that an RPC has terminated due to a local defect."""
  108. class RemoteError(AbortionError):
  109. """Indicates that an RPC has terminated due to a remote defect."""
  110. class RpcContext(six.with_metaclass(abc.ABCMeta)):
  111. """Provides RPC-related information and action."""
  112. @abc.abstractmethod
  113. def is_active(self):
  114. """Describes whether the RPC is active or has terminated."""
  115. raise NotImplementedError()
  116. @abc.abstractmethod
  117. def time_remaining(self):
  118. """Describes the length of allowed time remaining for the RPC.
  119. Returns:
  120. A nonnegative float indicating the length of allowed time in seconds
  121. remaining for the RPC to complete before it is considered to have timed
  122. out.
  123. """
  124. raise NotImplementedError()
  125. @abc.abstractmethod
  126. def add_abortion_callback(self, abortion_callback):
  127. """Registers a callback to be called if the RPC is aborted.
  128. Args:
  129. abortion_callback: A callable to be called and passed an Abortion value
  130. in the event of RPC abortion.
  131. """
  132. raise NotImplementedError()
  133. @abc.abstractmethod
  134. def cancel(self):
  135. """Cancels the RPC.
  136. Idempotent and has no effect if the RPC has already terminated.
  137. """
  138. raise NotImplementedError()
  139. @abc.abstractmethod
  140. def protocol_context(self):
  141. """Accesses a custom object specified by an implementation provider.
  142. Returns:
  143. A value specified by the provider of a Face interface implementation
  144. affording custom state and behavior.
  145. """
  146. raise NotImplementedError()
  147. class Call(six.with_metaclass(abc.ABCMeta, RpcContext)):
  148. """Invocation-side utility object for an RPC."""
  149. @abc.abstractmethod
  150. def initial_metadata(self):
  151. """Accesses the initial metadata from the service-side of the RPC.
  152. This method blocks until the value is available or is known not to have been
  153. emitted from the service-side of the RPC.
  154. Returns:
  155. The initial metadata object emitted by the service-side of the RPC, or
  156. None if there was no such value.
  157. """
  158. raise NotImplementedError()
  159. @abc.abstractmethod
  160. def terminal_metadata(self):
  161. """Accesses the terminal metadata from the service-side of the RPC.
  162. This method blocks until the value is available or is known not to have been
  163. emitted from the service-side of the RPC.
  164. Returns:
  165. The terminal metadata object emitted by the service-side of the RPC, or
  166. None if there was no such value.
  167. """
  168. raise NotImplementedError()
  169. @abc.abstractmethod
  170. def code(self):
  171. """Accesses the code emitted by the service-side of the RPC.
  172. This method blocks until the value is available or is known not to have been
  173. emitted from the service-side of the RPC.
  174. Returns:
  175. The code object emitted by the service-side of the RPC, or None if there
  176. was no such value.
  177. """
  178. raise NotImplementedError()
  179. @abc.abstractmethod
  180. def details(self):
  181. """Accesses the details value emitted by the service-side of the RPC.
  182. This method blocks until the value is available or is known not to have been
  183. emitted from the service-side of the RPC.
  184. Returns:
  185. The details value emitted by the service-side of the RPC, or None if there
  186. was no such value.
  187. """
  188. raise NotImplementedError()
  189. class ServicerContext(six.with_metaclass(abc.ABCMeta, RpcContext)):
  190. """A context object passed to method implementations."""
  191. @abc.abstractmethod
  192. def invocation_metadata(self):
  193. """Accesses the metadata from the invocation-side of the RPC.
  194. This method blocks until the value is available or is known not to have been
  195. emitted from the invocation-side of the RPC.
  196. Returns:
  197. The metadata object emitted by the invocation-side of the RPC, or None if
  198. there was no such value.
  199. """
  200. raise NotImplementedError()
  201. @abc.abstractmethod
  202. def initial_metadata(self, initial_metadata):
  203. """Accepts the service-side initial metadata value of the RPC.
  204. This method need not be called by method implementations if they have no
  205. service-side initial metadata to transmit.
  206. Args:
  207. initial_metadata: The service-side initial metadata value of the RPC to
  208. be transmitted to the invocation side of the RPC.
  209. """
  210. raise NotImplementedError()
  211. @abc.abstractmethod
  212. def terminal_metadata(self, terminal_metadata):
  213. """Accepts the service-side terminal metadata value of the RPC.
  214. This method need not be called by method implementations if they have no
  215. service-side terminal metadata to transmit.
  216. Args:
  217. terminal_metadata: The service-side terminal metadata value of the RPC to
  218. be transmitted to the invocation side of the RPC.
  219. """
  220. raise NotImplementedError()
  221. @abc.abstractmethod
  222. def code(self, code):
  223. """Accepts the service-side code of the RPC.
  224. This method need not be called by method implementations if they have no
  225. code to transmit.
  226. Args:
  227. code: The code of the RPC to be transmitted to the invocation side of the
  228. RPC.
  229. """
  230. raise NotImplementedError()
  231. @abc.abstractmethod
  232. def details(self, details):
  233. """Accepts the service-side details of the RPC.
  234. This method need not be called by method implementations if they have no
  235. service-side details to transmit.
  236. Args:
  237. details: The service-side details value of the RPC to be transmitted to
  238. the invocation side of the RPC.
  239. """
  240. raise NotImplementedError()
  241. class ResponseReceiver(six.with_metaclass(abc.ABCMeta)):
  242. """Invocation-side object used to accept the output of an RPC."""
  243. @abc.abstractmethod
  244. def initial_metadata(self, initial_metadata):
  245. """Receives the initial metadata from the service-side of the RPC.
  246. Args:
  247. initial_metadata: The initial metadata object emitted from the
  248. service-side of the RPC.
  249. """
  250. raise NotImplementedError()
  251. @abc.abstractmethod
  252. def response(self, response):
  253. """Receives a response from the service-side of the RPC.
  254. Args:
  255. response: A response object emitted from the service-side of the RPC.
  256. """
  257. raise NotImplementedError()
  258. @abc.abstractmethod
  259. def complete(self, terminal_metadata, code, details):
  260. """Receives the completion values emitted from the service-side of the RPC.
  261. Args:
  262. terminal_metadata: The terminal metadata object emitted from the
  263. service-side of the RPC.
  264. code: The code object emitted from the service-side of the RPC.
  265. details: The details object emitted from the service-side of the RPC.
  266. """
  267. raise NotImplementedError()
  268. class UnaryUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
  269. """Affords invoking a unary-unary RPC in any call style."""
  270. @abc.abstractmethod
  271. def __call__(self,
  272. request,
  273. timeout,
  274. metadata=None,
  275. with_call=False,
  276. protocol_options=None):
  277. """Synchronously invokes the underlying RPC.
  278. Args:
  279. request: The request value for the RPC.
  280. timeout: A duration of time in seconds to allow for the RPC.
  281. metadata: A metadata value to be passed to the service-side of
  282. the RPC.
  283. with_call: Whether or not to include return a Call for the RPC in addition
  284. to the response.
  285. protocol_options: A value specified by the provider of a Face interface
  286. implementation affording custom state and behavior.
  287. Returns:
  288. The response value for the RPC, and a Call for the RPC if with_call was
  289. set to True at invocation.
  290. Raises:
  291. AbortionError: Indicating that the RPC was aborted.
  292. """
  293. raise NotImplementedError()
  294. @abc.abstractmethod
  295. def future(self, request, timeout, metadata=None, protocol_options=None):
  296. """Asynchronously invokes the underlying RPC.
  297. Args:
  298. request: The request value for the RPC.
  299. timeout: A duration of time in seconds to allow for the RPC.
  300. metadata: A metadata value to be passed to the service-side of
  301. the RPC.
  302. protocol_options: A value specified by the provider of a Face interface
  303. implementation affording custom state and behavior.
  304. Returns:
  305. An object that is both a Call for the RPC and a future.Future. In the
  306. event of RPC completion, the return Future's result value will be the
  307. response value of the RPC. In the event of RPC abortion, the returned
  308. Future's exception value will be an AbortionError.
  309. """
  310. raise NotImplementedError()
  311. @abc.abstractmethod
  312. def event(self,
  313. request,
  314. receiver,
  315. abortion_callback,
  316. timeout,
  317. metadata=None,
  318. protocol_options=None):
  319. """Asynchronously invokes the underlying RPC.
  320. Args:
  321. request: The request value for the RPC.
  322. receiver: A ResponseReceiver to be passed the response data of the RPC.
  323. abortion_callback: A callback to be called and passed an Abortion value
  324. in the event of RPC abortion.
  325. timeout: A duration of time in seconds to allow for the RPC.
  326. metadata: A metadata value to be passed to the service-side of
  327. the RPC.
  328. protocol_options: A value specified by the provider of a Face interface
  329. implementation affording custom state and behavior.
  330. Returns:
  331. A Call for the RPC.
  332. """
  333. raise NotImplementedError()
  334. class UnaryStreamMultiCallable(six.with_metaclass(abc.ABCMeta)):
  335. """Affords invoking a unary-stream RPC in any call style."""
  336. @abc.abstractmethod
  337. def __call__(self, request, timeout, metadata=None, protocol_options=None):
  338. """Invokes the underlying RPC.
  339. Args:
  340. request: The request value for the RPC.
  341. timeout: A duration of time in seconds to allow for the RPC.
  342. metadata: A metadata value to be passed to the service-side of
  343. the RPC.
  344. protocol_options: A value specified by the provider of a Face interface
  345. implementation affording custom state and behavior.
  346. Returns:
  347. An object that is both a Call for the RPC and an iterator of response
  348. values. Drawing response values from the returned iterator may raise
  349. AbortionError indicating abortion of the RPC.
  350. """
  351. raise NotImplementedError()
  352. @abc.abstractmethod
  353. def event(self,
  354. request,
  355. receiver,
  356. abortion_callback,
  357. timeout,
  358. metadata=None,
  359. protocol_options=None):
  360. """Asynchronously invokes the underlying RPC.
  361. Args:
  362. request: The request value for the RPC.
  363. receiver: A ResponseReceiver to be passed the response data of the RPC.
  364. abortion_callback: A callback to be called and passed an Abortion value
  365. in the event of RPC abortion.
  366. timeout: A duration of time in seconds to allow for the RPC.
  367. metadata: A metadata value to be passed to the service-side of
  368. the RPC.
  369. protocol_options: A value specified by the provider of a Face interface
  370. implementation affording custom state and behavior.
  371. Returns:
  372. A Call object for the RPC.
  373. """
  374. raise NotImplementedError()
  375. class StreamUnaryMultiCallable(six.with_metaclass(abc.ABCMeta)):
  376. """Affords invoking a stream-unary RPC in any call style."""
  377. @abc.abstractmethod
  378. def __call__(self,
  379. request_iterator,
  380. timeout,
  381. metadata=None,
  382. with_call=False,
  383. protocol_options=None):
  384. """Synchronously invokes the underlying RPC.
  385. Args:
  386. request_iterator: An iterator that yields request values for the RPC.
  387. timeout: A duration of time in seconds to allow for the RPC.
  388. metadata: A metadata value to be passed to the service-side of
  389. the RPC.
  390. with_call: Whether or not to include return a Call for the RPC in addition
  391. to the response.
  392. protocol_options: A value specified by the provider of a Face interface
  393. implementation affording custom state and behavior.
  394. Returns:
  395. The response value for the RPC, and a Call for the RPC if with_call was
  396. set to True at invocation.
  397. Raises:
  398. AbortionError: Indicating that the RPC was aborted.
  399. """
  400. raise NotImplementedError()
  401. @abc.abstractmethod
  402. def future(self,
  403. request_iterator,
  404. timeout,
  405. metadata=None,
  406. protocol_options=None):
  407. """Asynchronously invokes the underlying RPC.
  408. Args:
  409. request_iterator: An iterator that yields request values for the RPC.
  410. timeout: A duration of time in seconds to allow for the RPC.
  411. metadata: A metadata value to be passed to the service-side of
  412. the RPC.
  413. protocol_options: A value specified by the provider of a Face interface
  414. implementation affording custom state and behavior.
  415. Returns:
  416. An object that is both a Call for the RPC and a future.Future. In the
  417. event of RPC completion, the return Future's result value will be the
  418. response value of the RPC. In the event of RPC abortion, the returned
  419. Future's exception value will be an AbortionError.
  420. """
  421. raise NotImplementedError()
  422. @abc.abstractmethod
  423. def event(self,
  424. receiver,
  425. abortion_callback,
  426. timeout,
  427. metadata=None,
  428. protocol_options=None):
  429. """Asynchronously invokes the underlying RPC.
  430. Args:
  431. receiver: A ResponseReceiver to be passed the response data of the RPC.
  432. abortion_callback: A callback to be called and passed an Abortion value
  433. in the event of RPC abortion.
  434. timeout: A duration of time in seconds to allow for the RPC.
  435. metadata: A metadata value to be passed to the service-side of
  436. the RPC.
  437. protocol_options: A value specified by the provider of a Face interface
  438. implementation affording custom state and behavior.
  439. Returns:
  440. A single object that is both a Call object for the RPC and a
  441. stream.Consumer to which the request values of the RPC should be passed.
  442. """
  443. raise NotImplementedError()
  444. class StreamStreamMultiCallable(six.with_metaclass(abc.ABCMeta)):
  445. """Affords invoking a stream-stream RPC in any call style."""
  446. @abc.abstractmethod
  447. def __call__(self,
  448. request_iterator,
  449. timeout,
  450. metadata=None,
  451. protocol_options=None):
  452. """Invokes the underlying RPC.
  453. Args:
  454. request_iterator: An iterator that yields request values for the RPC.
  455. timeout: A duration of time in seconds to allow for the RPC.
  456. metadata: A metadata value to be passed to the service-side of
  457. the RPC.
  458. protocol_options: A value specified by the provider of a Face interface
  459. implementation affording custom state and behavior.
  460. Returns:
  461. An object that is both a Call for the RPC and an iterator of response
  462. values. Drawing response values from the returned iterator may raise
  463. AbortionError indicating abortion of the RPC.
  464. """
  465. raise NotImplementedError()
  466. @abc.abstractmethod
  467. def event(self,
  468. receiver,
  469. abortion_callback,
  470. timeout,
  471. metadata=None,
  472. protocol_options=None):
  473. """Asynchronously invokes the underlying RPC.
  474. Args:
  475. receiver: A ResponseReceiver to be passed the response data of the RPC.
  476. abortion_callback: A callback to be called and passed an Abortion value
  477. in the event of RPC abortion.
  478. timeout: A duration of time in seconds to allow for the RPC.
  479. metadata: A metadata value to be passed to the service-side of
  480. the RPC.
  481. protocol_options: A value specified by the provider of a Face interface
  482. implementation affording custom state and behavior.
  483. Returns:
  484. A single object that is both a Call object for the RPC and a
  485. stream.Consumer to which the request values of the RPC should be passed.
  486. """
  487. raise NotImplementedError()
  488. class MethodImplementation(six.with_metaclass(abc.ABCMeta)):
  489. """A sum type that describes a method implementation.
  490. Attributes:
  491. cardinality: A cardinality.Cardinality value.
  492. style: A style.Service value.
  493. unary_unary_inline: The implementation of the method as a callable value
  494. that takes a request value and a ServicerContext object and returns a
  495. response value. Only non-None if cardinality is
  496. cardinality.Cardinality.UNARY_UNARY and style is style.Service.INLINE.
  497. unary_stream_inline: The implementation of the method as a callable value
  498. that takes a request value and a ServicerContext object and returns an
  499. iterator of response values. Only non-None if cardinality is
  500. cardinality.Cardinality.UNARY_STREAM and style is style.Service.INLINE.
  501. stream_unary_inline: The implementation of the method as a callable value
  502. that takes an iterator of request values and a ServicerContext object and
  503. returns a response value. Only non-None if cardinality is
  504. cardinality.Cardinality.STREAM_UNARY and style is style.Service.INLINE.
  505. stream_stream_inline: The implementation of the method as a callable value
  506. that takes an iterator of request values and a ServicerContext object and
  507. returns an iterator of response values. Only non-None if cardinality is
  508. cardinality.Cardinality.STREAM_STREAM and style is style.Service.INLINE.
  509. unary_unary_event: The implementation of the method as a callable value that
  510. takes a request value, a response callback to which to pass the response
  511. value of the RPC, and a ServicerContext. Only non-None if cardinality is
  512. cardinality.Cardinality.UNARY_UNARY and style is style.Service.EVENT.
  513. unary_stream_event: The implementation of the method as a callable value
  514. that takes a request value, a stream.Consumer to which to pass the
  515. response values of the RPC, and a ServicerContext. Only non-None if
  516. cardinality is cardinality.Cardinality.UNARY_STREAM and style is
  517. style.Service.EVENT.
  518. stream_unary_event: The implementation of the method as a callable value
  519. that takes a response callback to which to pass the response value of the
  520. RPC and a ServicerContext and returns a stream.Consumer to which the
  521. request values of the RPC should be passed. Only non-None if cardinality
  522. is cardinality.Cardinality.STREAM_UNARY and style is style.Service.EVENT.
  523. stream_stream_event: The implementation of the method as a callable value
  524. that takes a stream.Consumer to which to pass the response values of the
  525. RPC and a ServicerContext and returns a stream.Consumer to which the
  526. request values of the RPC should be passed. Only non-None if cardinality
  527. is cardinality.Cardinality.STREAM_STREAM and style is
  528. style.Service.EVENT.
  529. """
  530. class MultiMethodImplementation(six.with_metaclass(abc.ABCMeta)):
  531. """A general type able to service many methods."""
  532. @abc.abstractmethod
  533. def service(self, group, method, response_consumer, context):
  534. """Services an RPC.
  535. Args:
  536. group: The group identifier of the RPC.
  537. method: The method identifier of the RPC.
  538. response_consumer: A stream.Consumer to be called to accept the response
  539. values of the RPC.
  540. context: a ServicerContext object.
  541. Returns:
  542. A stream.Consumer with which to accept the request values of the RPC. The
  543. consumer returned from this method may or may not be invoked to
  544. completion: in the case of RPC abortion, RPC Framework will simply stop
  545. passing values to this object. Implementations must not assume that this
  546. object will be called to completion of the request stream or even called
  547. at all.
  548. Raises:
  549. abandonment.Abandoned: May or may not be raised when the RPC has been
  550. aborted.
  551. NoSuchMethodError: If this MultiMethod does not recognize the given group
  552. and name for the RPC and is not able to service the RPC.
  553. """
  554. raise NotImplementedError()
  555. class GenericStub(six.with_metaclass(abc.ABCMeta)):
  556. """Affords RPC invocation via generic methods."""
  557. @abc.abstractmethod
  558. def blocking_unary_unary(self,
  559. group,
  560. method,
  561. request,
  562. timeout,
  563. metadata=None,
  564. with_call=False,
  565. protocol_options=None):
  566. """Invokes a unary-request-unary-response method.
  567. This method blocks until either returning the response value of the RPC
  568. (in the event of RPC completion) or raising an exception (in the event of
  569. RPC abortion).
  570. Args:
  571. group: The group identifier of the RPC.
  572. method: The method identifier of the RPC.
  573. request: The request value for the RPC.
  574. timeout: A duration of time in seconds to allow for the RPC.
  575. metadata: A metadata value to be passed to the service-side of the RPC.
  576. with_call: Whether or not to include return a Call for the RPC in addition
  577. to the response.
  578. protocol_options: A value specified by the provider of a Face interface
  579. implementation affording custom state and behavior.
  580. Returns:
  581. The response value for the RPC, and a Call for the RPC if with_call was
  582. set to True at invocation.
  583. Raises:
  584. AbortionError: Indicating that the RPC was aborted.
  585. """
  586. raise NotImplementedError()
  587. @abc.abstractmethod
  588. def future_unary_unary(self,
  589. group,
  590. method,
  591. request,
  592. timeout,
  593. metadata=None,
  594. protocol_options=None):
  595. """Invokes a unary-request-unary-response method.
  596. Args:
  597. group: The group identifier of the RPC.
  598. method: The method identifier of the RPC.
  599. request: The request value for the RPC.
  600. timeout: A duration of time in seconds to allow for the RPC.
  601. metadata: A metadata value to be passed to the service-side of the RPC.
  602. protocol_options: A value specified by the provider of a Face interface
  603. implementation affording custom state and behavior.
  604. Returns:
  605. An object that is both a Call for the RPC and a future.Future. In the
  606. event of RPC completion, the return Future's result value will be the
  607. response value of the RPC. In the event of RPC abortion, the returned
  608. Future's exception value will be an AbortionError.
  609. """
  610. raise NotImplementedError()
  611. @abc.abstractmethod
  612. def inline_unary_stream(self,
  613. group,
  614. method,
  615. request,
  616. timeout,
  617. metadata=None,
  618. protocol_options=None):
  619. """Invokes a unary-request-stream-response method.
  620. Args:
  621. group: The group identifier of the RPC.
  622. method: The method identifier of the RPC.
  623. request: The request value for the RPC.
  624. timeout: A duration of time in seconds to allow for the RPC.
  625. metadata: A metadata value to be passed to the service-side of the RPC.
  626. protocol_options: A value specified by the provider of a Face interface
  627. implementation affording custom state and behavior.
  628. Returns:
  629. An object that is both a Call for the RPC and an iterator of response
  630. values. Drawing response values from the returned iterator may raise
  631. AbortionError indicating abortion of the RPC.
  632. """
  633. raise NotImplementedError()
  634. @abc.abstractmethod
  635. def blocking_stream_unary(self,
  636. group,
  637. method,
  638. request_iterator,
  639. timeout,
  640. metadata=None,
  641. with_call=False,
  642. protocol_options=None):
  643. """Invokes a stream-request-unary-response method.
  644. This method blocks until either returning the response value of the RPC
  645. (in the event of RPC completion) or raising an exception (in the event of
  646. RPC abortion).
  647. Args:
  648. group: The group identifier of the RPC.
  649. method: The method identifier of the RPC.
  650. request_iterator: An iterator that yields request values for the RPC.
  651. timeout: A duration of time in seconds to allow for the RPC.
  652. metadata: A metadata value to be passed to the service-side of the RPC.
  653. with_call: Whether or not to include return a Call for the RPC in addition
  654. to the response.
  655. protocol_options: A value specified by the provider of a Face interface
  656. implementation affording custom state and behavior.
  657. Returns:
  658. The response value for the RPC, and a Call for the RPC if with_call was
  659. set to True at invocation.
  660. Raises:
  661. AbortionError: Indicating that the RPC was aborted.
  662. """
  663. raise NotImplementedError()
  664. @abc.abstractmethod
  665. def future_stream_unary(self,
  666. group,
  667. method,
  668. request_iterator,
  669. timeout,
  670. metadata=None,
  671. protocol_options=None):
  672. """Invokes a stream-request-unary-response method.
  673. Args:
  674. group: The group identifier of the RPC.
  675. method: The method identifier of the RPC.
  676. request_iterator: An iterator that yields request values for the RPC.
  677. timeout: A duration of time in seconds to allow for the RPC.
  678. metadata: A metadata value to be passed to the service-side of the RPC.
  679. protocol_options: A value specified by the provider of a Face interface
  680. implementation affording custom state and behavior.
  681. Returns:
  682. An object that is both a Call for the RPC and a future.Future. In the
  683. event of RPC completion, the return Future's result value will be the
  684. response value of the RPC. In the event of RPC abortion, the returned
  685. Future's exception value will be an AbortionError.
  686. """
  687. raise NotImplementedError()
  688. @abc.abstractmethod
  689. def inline_stream_stream(self,
  690. group,
  691. method,
  692. request_iterator,
  693. timeout,
  694. metadata=None,
  695. protocol_options=None):
  696. """Invokes a stream-request-stream-response method.
  697. Args:
  698. group: The group identifier of the RPC.
  699. method: The method identifier of the RPC.
  700. request_iterator: An iterator that yields request values for the RPC.
  701. timeout: A duration of time in seconds to allow for the RPC.
  702. metadata: A metadata value to be passed to the service-side of the RPC.
  703. protocol_options: A value specified by the provider of a Face interface
  704. implementation affording custom state and behavior.
  705. Returns:
  706. An object that is both a Call for the RPC and an iterator of response
  707. values. Drawing response values from the returned iterator may raise
  708. AbortionError indicating abortion of the RPC.
  709. """
  710. raise NotImplementedError()
  711. @abc.abstractmethod
  712. def event_unary_unary(self,
  713. group,
  714. method,
  715. request,
  716. receiver,
  717. abortion_callback,
  718. timeout,
  719. metadata=None,
  720. protocol_options=None):
  721. """Event-driven invocation of a unary-request-unary-response method.
  722. Args:
  723. group: The group identifier of the RPC.
  724. method: The method identifier of the RPC.
  725. request: The request value for the RPC.
  726. receiver: A ResponseReceiver to be passed the response data of the RPC.
  727. abortion_callback: A callback to be called and passed an Abortion value
  728. in the event of RPC abortion.
  729. timeout: A duration of time in seconds to allow for the RPC.
  730. metadata: A metadata value to be passed to the service-side of the RPC.
  731. protocol_options: A value specified by the provider of a Face interface
  732. implementation affording custom state and behavior.
  733. Returns:
  734. A Call for the RPC.
  735. """
  736. raise NotImplementedError()
  737. @abc.abstractmethod
  738. def event_unary_stream(self,
  739. group,
  740. method,
  741. request,
  742. receiver,
  743. abortion_callback,
  744. timeout,
  745. metadata=None,
  746. protocol_options=None):
  747. """Event-driven invocation of a unary-request-stream-response method.
  748. Args:
  749. group: The group identifier of the RPC.
  750. method: The method identifier of the RPC.
  751. request: The request value for the RPC.
  752. receiver: A ResponseReceiver to be passed the response data of the RPC.
  753. abortion_callback: A callback to be called and passed an Abortion value
  754. in the event of RPC abortion.
  755. timeout: A duration of time in seconds to allow for the RPC.
  756. metadata: A metadata value to be passed to the service-side of the RPC.
  757. protocol_options: A value specified by the provider of a Face interface
  758. implementation affording custom state and behavior.
  759. Returns:
  760. A Call for the RPC.
  761. """
  762. raise NotImplementedError()
  763. @abc.abstractmethod
  764. def event_stream_unary(self,
  765. group,
  766. method,
  767. receiver,
  768. abortion_callback,
  769. timeout,
  770. metadata=None,
  771. protocol_options=None):
  772. """Event-driven invocation of a unary-request-unary-response method.
  773. Args:
  774. group: The group identifier of the RPC.
  775. method: The method identifier of the RPC.
  776. receiver: A ResponseReceiver to be passed the response data of the RPC.
  777. abortion_callback: A callback to be called and passed an Abortion value
  778. in the event of RPC abortion.
  779. timeout: A duration of time in seconds to allow for the RPC.
  780. metadata: A metadata value to be passed to the service-side of the RPC.
  781. protocol_options: A value specified by the provider of a Face interface
  782. implementation affording custom state and behavior.
  783. Returns:
  784. A pair of a Call object for the RPC and a stream.Consumer to which the
  785. request values of the RPC should be passed.
  786. """
  787. raise NotImplementedError()
  788. @abc.abstractmethod
  789. def event_stream_stream(self,
  790. group,
  791. method,
  792. receiver,
  793. abortion_callback,
  794. timeout,
  795. metadata=None,
  796. protocol_options=None):
  797. """Event-driven invocation of a unary-request-stream-response method.
  798. Args:
  799. group: The group identifier of the RPC.
  800. method: The method identifier of the RPC.
  801. receiver: A ResponseReceiver to be passed the response data of the RPC.
  802. abortion_callback: A callback to be called and passed an Abortion value
  803. in the event of RPC abortion.
  804. timeout: A duration of time in seconds to allow for the RPC.
  805. metadata: A metadata value to be passed to the service-side of the RPC.
  806. protocol_options: A value specified by the provider of a Face interface
  807. implementation affording custom state and behavior.
  808. Returns:
  809. A pair of a Call object for the RPC and a stream.Consumer to which the
  810. request values of the RPC should be passed.
  811. """
  812. raise NotImplementedError()
  813. @abc.abstractmethod
  814. def unary_unary(self, group, method):
  815. """Creates a UnaryUnaryMultiCallable for a unary-unary method.
  816. Args:
  817. group: The group identifier of the RPC.
  818. method: The method identifier of the RPC.
  819. Returns:
  820. A UnaryUnaryMultiCallable value for the named unary-unary method.
  821. """
  822. raise NotImplementedError()
  823. @abc.abstractmethod
  824. def unary_stream(self, group, method):
  825. """Creates a UnaryStreamMultiCallable for a unary-stream method.
  826. Args:
  827. group: The group identifier of the RPC.
  828. method: The method identifier of the RPC.
  829. Returns:
  830. A UnaryStreamMultiCallable value for the name unary-stream method.
  831. """
  832. raise NotImplementedError()
  833. @abc.abstractmethod
  834. def stream_unary(self, group, method):
  835. """Creates a StreamUnaryMultiCallable for a stream-unary method.
  836. Args:
  837. group: The group identifier of the RPC.
  838. method: The method identifier of the RPC.
  839. Returns:
  840. A StreamUnaryMultiCallable value for the named stream-unary method.
  841. """
  842. raise NotImplementedError()
  843. @abc.abstractmethod
  844. def stream_stream(self, group, method):
  845. """Creates a StreamStreamMultiCallable for a stream-stream method.
  846. Args:
  847. group: The group identifier of the RPC.
  848. method: The method identifier of the RPC.
  849. Returns:
  850. A StreamStreamMultiCallable value for the named stream-stream method.
  851. """
  852. raise NotImplementedError()
  853. class DynamicStub(six.with_metaclass(abc.ABCMeta)):
  854. """Affords RPC invocation via attributes corresponding to afforded methods.
  855. Instances of this type may be scoped to a single group so that attribute
  856. access is unambiguous.
  857. Instances of this type respond to attribute access as follows: if the
  858. requested attribute is the name of a unary-unary method, the value of the
  859. attribute will be a UnaryUnaryMultiCallable with which to invoke an RPC; if
  860. the requested attribute is the name of a unary-stream method, the value of the
  861. attribute will be a UnaryStreamMultiCallable with which to invoke an RPC; if
  862. the requested attribute is the name of a stream-unary method, the value of the
  863. attribute will be a StreamUnaryMultiCallable with which to invoke an RPC; and
  864. if the requested attribute is the name of a stream-stream method, the value of
  865. the attribute will be a StreamStreamMultiCallable with which to invoke an RPC.
  866. """