Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit 7d103a5

Browse files
authored
Convert appservice code to async/await. (#8207)
1 parent 5615eb5 commit 7d103a5

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

changelog.d/8207.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Convert various parts of the codebase to async/await.

synapse/appservice/api.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414
# limitations under the License.
1515
import logging
1616
import urllib
17+
from typing import TYPE_CHECKING, Optional
1718

1819
from prometheus_client import Counter
1920

20-
from twisted.internet import defer
21-
2221
from synapse.api.constants import EventTypes, ThirdPartyEntityKind
2322
from synapse.api.errors import CodeMessageException
2423
from synapse.events.utils import serialize_event
2524
from synapse.http.client import SimpleHttpClient
26-
from synapse.types import ThirdPartyInstanceID
25+
from synapse.types import JsonDict, ThirdPartyInstanceID
2726
from synapse.util.caches.response_cache import ResponseCache
2827

28+
if TYPE_CHECKING:
29+
from synapse.appservice import ApplicationService
30+
2931
logger = logging.getLogger(__name__)
3032

3133
sent_transactions_counter = Counter(
@@ -163,19 +165,20 @@ async def query_3pe(self, service, kind, protocol, fields):
163165
logger.warning("query_3pe to %s threw exception %s", uri, ex)
164166
return []
165167

166-
def get_3pe_protocol(self, service, protocol):
168+
async def get_3pe_protocol(
169+
self, service: "ApplicationService", protocol: str
170+
) -> Optional[JsonDict]:
167171
if service.url is None:
168172
return {}
169173

170-
@defer.inlineCallbacks
171-
def _get():
174+
async def _get() -> Optional[JsonDict]:
172175
uri = "%s%s/thirdparty/protocol/%s" % (
173176
service.url,
174177
APP_SERVICE_PREFIX,
175178
urllib.parse.quote(protocol),
176179
)
177180
try:
178-
info = yield defer.ensureDeferred(self.get_json(uri, {}))
181+
info = await self.get_json(uri, {})
179182

180183
if not _is_valid_3pe_metadata(info):
181184
logger.warning(
@@ -196,7 +199,7 @@ def _get():
196199
return None
197200

198201
key = (service.id, protocol)
199-
return self.protocol_meta_cache.wrap(key, _get)
202+
return await self.protocol_meta_cache.wrap(key, _get)
200203

201204
async def push_bulk(self, service, events, txn_id=None):
202205
if service.url is None:

0 commit comments

Comments
 (0)