api.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. # -*- coding: utf-8 -*-
  2. # Copyright 2015, 2016 OpenMarket Ltd
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from twisted.internet import defer
  16. from synapse.api.errors import CodeMessageException
  17. from synapse.http.client import SimpleHttpClient
  18. from synapse.events.utils import serialize_event
  19. import logging
  20. import urllib
  21. logger = logging.getLogger(__name__)
  22. class ApplicationServiceApi(SimpleHttpClient):
  23. """This class manages HS -> AS communications, including querying and
  24. pushing.
  25. """
  26. def __init__(self, hs):
  27. super(ApplicationServiceApi, self).__init__(hs)
  28. self.clock = hs.get_clock()
  29. @defer.inlineCallbacks
  30. def query_user(self, service, user_id):
  31. uri = service.url + ("/users/%s" % urllib.quote(user_id))
  32. response = None
  33. try:
  34. response = yield self.get_json(uri, {
  35. "access_token": service.hs_token
  36. })
  37. if response is not None: # just an empty json object
  38. defer.returnValue(True)
  39. except CodeMessageException as e:
  40. if e.code == 404:
  41. defer.returnValue(False)
  42. return
  43. logger.warning("query_user to %s received %s", uri, e.code)
  44. except Exception as ex:
  45. logger.warning("query_user to %s threw exception %s", uri, ex)
  46. defer.returnValue(False)
  47. @defer.inlineCallbacks
  48. def query_alias(self, service, alias):
  49. uri = service.url + ("/rooms/%s" % urllib.quote(alias))
  50. response = None
  51. try:
  52. response = yield self.get_json(uri, {
  53. "access_token": service.hs_token
  54. })
  55. if response is not None: # just an empty json object
  56. defer.returnValue(True)
  57. except CodeMessageException as e:
  58. logger.warning("query_alias to %s received %s", uri, e.code)
  59. if e.code == 404:
  60. defer.returnValue(False)
  61. return
  62. except Exception as ex:
  63. logger.warning("query_alias to %s threw exception %s", uri, ex)
  64. defer.returnValue(False)
  65. @defer.inlineCallbacks
  66. def push_bulk(self, service, events, txn_id=None):
  67. events = self._serialize(events)
  68. if txn_id is None:
  69. logger.warning("push_bulk: Missing txn ID sending events to %s",
  70. service.url)
  71. txn_id = str(0)
  72. txn_id = str(txn_id)
  73. uri = service.url + ("/transactions/%s" %
  74. urllib.quote(txn_id))
  75. try:
  76. yield self.put_json(
  77. uri=uri,
  78. json_body={
  79. "events": events
  80. },
  81. args={
  82. "access_token": service.hs_token
  83. })
  84. defer.returnValue(True)
  85. return
  86. except CodeMessageException as e:
  87. logger.warning("push_bulk to %s received %s", uri, e.code)
  88. except Exception as ex:
  89. logger.warning("push_bulk to %s threw exception %s", uri, ex)
  90. defer.returnValue(False)
  91. def _serialize(self, events):
  92. time_now = self.clock.time_msec()
  93. return [
  94. serialize_event(e, time_now, as_client_event=True) for e in events
  95. ]