Skip to content

Commit

Permalink
Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
itssimon committed Mar 18, 2024
1 parent 1e2e959 commit 88d69c1
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions apitally/django.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from django.conf import settings
from django.core.exceptions import ViewDoesNotExist
from django.test import RequestFactory
from django.urls import URLPattern, URLResolver, get_resolver, resolve
from django.urls import Resolver404, URLPattern, URLResolver, get_resolver, resolve
from django.utils.module_loading import import_string

from apitally.client.threading import ApitallyClient
Expand Down Expand Up @@ -41,12 +41,15 @@ def __init__(self, get_response: Callable[[HttpRequest], HttpResponse]) -> None:
config = getattr(settings, "APITALLY_MIDDLEWARE", {})
self.configure(**config)
assert self.config is not None
self.views = _extract_views_from_url_patterns(get_resolver().url_patterns)
views = _extract_views_from_url_patterns(get_resolver().url_patterns)
self.view_lookup = {
view.pattern: view for view in reversed(_extract_views_from_url_patterns(get_resolver().url_patterns))
}
self.client = ApitallyClient(client_id=self.config.client_id, env=self.config.env)
self.client.start_sync_loop()
self.client.set_app_info(
app_info=_get_app_info(
views=self.views,
views=views,
app_version=self.config.app_version,
openapi_url=self.config.openapi_url,
)
Expand Down Expand Up @@ -108,8 +111,11 @@ def __call__(self, request: HttpRequest) -> HttpResponse:
return response

def get_view(self, request: HttpRequest) -> Optional[DjangoViewInfo]:
resolver_match = resolve(request.path_info)
return next((view for view in self.views if view.pattern == resolver_match.route), None)
try:
resolver_match = resolve(request.path_info)
return self.view_lookup.get(resolver_match.route)
except Resolver404:
return None

Check warning on line 118 in apitally/django.py

View check run for this annotation

Codecov / codecov/patch

apitally/django.py#L117-L118

Added lines #L117 - L118 were not covered by tests

def get_consumer(self, request: HttpRequest) -> Optional[str]:
if hasattr(request, "consumer_identifier"):
Expand Down Expand Up @@ -255,7 +261,7 @@ def _get_versions(app_version: Optional[str]) -> Dict[str, str]:
"django": version("django"),
}
try:
versions["django-rest-framework"] = version("django-rest-framework")
versions["djangorestframework"] = version("djangorestframework")
except PackageNotFoundError: # pragma: no cover
pass
try:
Expand Down

0 comments on commit 88d69c1

Please sign in to comment.