Skip to content

Commit

Permalink
migrate all models and managers to async_to_sync_method for sync (#24)
Browse files Browse the repository at this point in the history
* migrate all models to use `async_to_sync_method` for sync methods

* ah, the solution was staring me right in the face

the whole time!
  • Loading branch information
joshuadavidthomas authored Nov 20, 2024
1 parent f8b417d commit 947caea
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions src/django_github_app/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
from enum import Enum
from typing import Any

from asgiref.sync import async_to_sync
from django.db import models
from django.utils import timezone
from gidgethub import abc
from gidgethub import sansio
from gidgethub.apps import get_installation_access_token
from gidgethub.apps import get_jwt

from ._sync import async_to_sync_method
from ._typing import override
from .conf import app_settings
from .github import AsyncGitHubAPI
Expand All @@ -27,9 +27,6 @@ async def acreate_from_event(self, event: sansio.Event):
received_at=timezone.now(),
)

def create_from_event(self, event: sansio.Event):
return async_to_sync(self.acreate_from_event)(event)

async def acleanup_events(
self, days_to_keep: int = app_settings.DAYS_TO_KEEP_EVENTS
):
Expand All @@ -38,8 +35,8 @@ async def acleanup_events(
).adelete()
return deleted

def cleanup_events(self, days_to_keep: int = 7):
return async_to_sync(self.acleanup_events)(days_to_keep)
create_from_event = async_to_sync_method(acreate_from_event)
cleanup_events = async_to_sync_method(acleanup_events)


class EventLog(models.Model):
Expand Down Expand Up @@ -77,24 +74,19 @@ async def acreate_from_event(self, event: sansio.Event):

return installation

def create_from_event(self, event: sansio.Event):
return async_to_sync(self.acreate_from_event)(event)

async def acreate_from_gh_data(self, data: dict[str, str]):
return await self.acreate(installation_id=data["id"], data=data)

def create_from_gh_data(self, data: dict[str, str]):
return async_to_sync(self.acreate_from_gh_data)(data)

async def aget_from_event(self, event: sansio.Event):
try:
installation_id = event.data["installation"]["id"]
return await self.aget(installation_id=installation_id)
except (Installation.DoesNotExist, KeyError):
return None

def get_from_event(self, event: sansio.Event):
return async_to_sync(self.aget_from_event)(event)
create_from_event = async_to_sync_method(acreate_from_event)
create_from_gh_data = async_to_sync_method(acreate_from_gh_data)
get_from_event = async_to_sync_method(aget_from_event)


class InstallationStatus(models.IntegerChoices):
Expand Down Expand Up @@ -147,9 +139,6 @@ async def aget_access_token(self, gh: abc.GitHubAPI): # pragma: no cover
)
return data.get("token")

def get_access_token(self, gh: abc.GitHubAPI): # pragma: no cover
return async_to_sync(self.aget_access_token)(gh)

async def arefresh_from_gh(self, account_type: AccountType, account_name: str):
match account_type:
case AccountType.ORG:
Expand All @@ -171,9 +160,6 @@ async def arefresh_from_gh(self, account_type: AccountType, account_name: str):
self.data = data
await self.asave()

def refresh_from_gh(self, account_type: AccountType, account_name: str):
return async_to_sync(self.arefresh_from_gh)(account_type, account_name)

async def aget_repos(self, params: dict[str, Any] | None = None):
url = GitHubAPIUrl(
GitHubAPIEndpoint.INSTALLATION_REPOS,
Expand All @@ -186,13 +172,14 @@ async def aget_repos(self, params: dict[str, Any] | None = None):
]
return repos

def get_repos(self, params: dict[str, Any] | None = None):
return async_to_sync(self.aget_repos)(params)

@property
def app_slug(self):
return self.data.get("app_slug", app_settings.SLUG)

get_access_token = async_to_sync_method(aget_access_token)
refresh_from_gh = async_to_sync_method(arefresh_from_gh)
get_repos = async_to_sync_method(aget_repos)


class RepositoryManager(models.Manager["Repository"]):
async def acreate_from_gh_data(
Expand All @@ -217,20 +204,15 @@ async def acreate_from_gh_data(
full_name=data["full_name"],
)

def create_from_gh_data(
self, data: dict[str, str] | list[dict[str, str]], installation: Installation
):
return async_to_sync(self.acreate_from_gh_data)(data, installation)

async def aget_from_event(self, event: sansio.Event):
try:
repository_id = event.data["repository"]["id"]
return await self.aget(repository_id=repository_id)
except Repository.DoesNotExist:
return None

def get_from_event(self, event: sansio.Event):
return async_to_sync(self.aget_from_event)(event)
create_from_gh_data = async_to_sync_method(acreate_from_gh_data)
get_from_event = async_to_sync_method(aget_from_event)


class Repository(models.Model):
Expand Down Expand Up @@ -266,13 +248,12 @@ async def aget_issues(self, params: dict[str, Any] | None = None):
issues = [issue async for issue in gh.getiter(url.full_url)]
return issues

def get_issues(self, params: dict[str, Any] | None = None):
return async_to_sync(self.aget_issues)(params)

@property
def owner(self):
return self.full_name.split("/")[0]

@property
def repo(self):
return self.full_name.split("/")[1]

get_issues = async_to_sync_method(aget_issues)

0 comments on commit 947caea

Please sign in to comment.