face.py 38 KB

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