From 4d0bbfccfc78d41efd0aa86514460e06064668d0 Mon Sep 17 00:00:00 2001 From: Nick Sweeting Date: Sat, 17 Aug 2024 21:56:23 -0700 Subject: [PATCH] add uri salt and fix api url namespaces --- archivebox/abid_utils/abid.py | 4 ++++ archivebox/api/v1_api.py | 2 +- archivebox/api/v1_core.py | 18 +++++++++--------- archivebox/core/admin.py | 23 +++++++++++++---------- archivebox/core/models.py | 28 +++++++++++++++++++++++++++- archivebox/core/urls.py | 2 +- 6 files changed, 55 insertions(+), 22 deletions(-) diff --git a/archivebox/abid_utils/abid.py b/archivebox/abid_utils/abid.py index 23d6dec5..f6f9d153 100644 --- a/archivebox/abid_utils/abid.py +++ b/archivebox/abid_utils/abid.py @@ -72,6 +72,10 @@ class ABID(NamedTuple): subtype=suffix[18:20].upper(), rand=suffix[20:26].upper(), ) + + @property + def uri_salt(self) -> str: + return DEFAULT_ABID_URI_SALT @property def suffix(self): diff --git a/archivebox/api/v1_api.py b/archivebox/api/v1_api.py index 4fa5d94b..546ef8a0 100644 --- a/archivebox/api/v1_api.py +++ b/archivebox/api/v1_api.py @@ -63,7 +63,7 @@ api = NinjaAPIWithIOCapture( version='1.0.0', csrf=False, auth=API_AUTH_METHODS, - urls_namespace="api", + urls_namespace="api-1", docs=Swagger(settings={"persistAuthorization": True}), # docs_decorator=login_required, # renderer=ORJSONRenderer(), diff --git a/archivebox/api/v1_core.py b/archivebox/api/v1_core.py index 9046c361..595ec047 100644 --- a/archivebox/api/v1_core.py +++ b/archivebox/api/v1_core.py @@ -33,7 +33,7 @@ class ArchiveResultSchema(Schema): snapshot_tags: str extractor: str - cmd_version: str + cmd_version: Optional[str] cmd: List[str] pwd: str status: str @@ -93,16 +93,16 @@ class ArchiveResultFilterSchema(FilterSchema): created__lt: Optional[datetime] = Field(None, q='updated__lt') -@router.get("/archiveresults", response=List[ArchiveResultSchema]) +@router.get("/archiveresults", response=List[ArchiveResultSchema], url_name="get_archiveresult") @paginate -def list_archiveresults(request, filters: ArchiveResultFilterSchema = Query(...)): +def get_archiveresults(request, filters: ArchiveResultFilterSchema = Query(...)): """List all ArchiveResult entries matching these filters.""" qs = ArchiveResult.objects.all() results = filters.filter(qs) return results -@router.get("/archiveresult/{archiveresult_id}", response=ArchiveResultSchema) +@router.get("/archiveresult/{archiveresult_id}", response=ArchiveResultSchema, url_name="get_archiveresult") def get_archiveresult(request, archiveresult_id: str): """Get a specific ArchiveResult by abid, uuid, or pk.""" return ArchiveResult.objects.get(Q(pk__icontains=archiveresult_id) | Q(abid__icontains=archiveresult_id) | Q(uuid__icontains=archiveresult_id)) @@ -211,9 +211,9 @@ class SnapshotFilterSchema(FilterSchema): -@router.get("/snapshots", response=List[SnapshotSchema]) +@router.get("/snapshots", response=List[SnapshotSchema], url_name="get_snapshots") @paginate -def list_snapshots(request, filters: SnapshotFilterSchema = Query(...), with_archiveresults: bool=True): +def get_snapshots(request, filters: SnapshotFilterSchema = Query(...), with_archiveresults: bool=True): """List all Snapshot entries matching these filters.""" request.with_archiveresults = with_archiveresults @@ -221,7 +221,7 @@ def list_snapshots(request, filters: SnapshotFilterSchema = Query(...), with_arc results = filters.filter(qs) return results -@router.get("/snapshot/{snapshot_id}", response=SnapshotSchema) +@router.get("/snapshot/{snapshot_id}", response=SnapshotSchema, url_name="get_snapshot") def get_snapshot(request, snapshot_id: str, with_archiveresults: bool=True): """Get a specific Snapshot by abid, uuid, or pk.""" request.with_archiveresults = with_archiveresults @@ -286,6 +286,6 @@ class TagSchema(Schema): def resolve_created_by_id(obj): return str(obj.created_by_id) -@router.get("/tags", response=List[TagSchema]) -def list_tags(request): +@router.get("/tags", response=List[TagSchema], url_name="get_tags") +def get_tags(request): return Tag.objects.all() diff --git a/archivebox/core/admin.py b/archivebox/core/admin.py index 7e1aa7f9..3fa86816 100644 --- a/archivebox/core/admin.py +++ b/archivebox/core/admin.py @@ -168,28 +168,31 @@ def get_abid_info(self, obj): return format_html( # URL Hash: {}
''' -     DB ID:      {}
-        .id:                      {}    
-        .uuid:                  {}    
+     DB ID:                {}
+        .id:                    {}    
+        .uuid:                {}    

-     ABID:       {}
-         TS:                  {} ({})
-         URI:                 {} ({})
+     ABID:         {}_{}                            /api/v1 GET JSON     API DOCS
+         TS:                  {}        ({})
+         URI:                 {}           ({})
        SUBTYPE:       {} ({})     -         RAND:              {} ({})

-         as ULID:               {}
-         as UUID:              {}

+   RAND:   {} ({})     +   SALT:   {} +

+         .ulid:                    {}
+         .uuid:                   {}

''', obj.pk, obj.id, obj.uuid, - obj.abid, + *obj.abid.split('_', 1), obj.api_url, obj.api_docs_url, obj.ABID.ts, obj.abid_values['ts'].isoformat() if isinstance(obj.abid_values['ts'], datetime) else obj.abid_values['ts'], obj.ABID.uri, str(obj.abid_values['uri']), obj.ABID.subtype, str(obj.abid_values['subtype']), obj.ABID.rand, str(obj.abid_values['rand'])[-7:], + obj.ABID.uri_salt, obj.ABID.ulid, obj.ABID.uuid, ) diff --git a/archivebox/core/models.py b/archivebox/core/models.py index 2c9a9969..dfd826f2 100644 --- a/archivebox/core/models.py +++ b/archivebox/core/models.py @@ -14,7 +14,7 @@ from django.db import models from django.utils.functional import cached_property from django.utils.text import slugify from django.core.cache import cache -from django.urls import reverse +from django.urls import reverse, reverse_lazy from django.db.models import Case, When, Value, IntegerField from django.contrib.auth.models import User # noqa @@ -103,6 +103,15 @@ class Tag(ABIDModel): i = 1 if i is None else i+1 else: return super().save(*args, **kwargs) + + @property + def api_url(self) -> str: + # /api/v1/core/snapshot/{uulid} + return reverse_lazy('api-1:get_tag', args=[self.abid]) + + @property + def api_docs_url(self) -> str: + return f'/api/v1/docs#/Core%20Models/api_v1_core_get_tag' class Snapshot(ABIDModel): @@ -167,6 +176,15 @@ class Snapshot(ABIDModel): def icons(self) -> str: return snapshot_icons(self) + + @property + def api_url(self) -> str: + # /api/v1/core/snapshot/{uulid} + return reverse_lazy('api-1:get_snapshot', args=[self.abid]) + + @property + def api_docs_url(self) -> str: + return f'/api/v1/docs#/Core%20Models/api_v1_core_get_snapshot' @cached_property def extension(self) -> str: @@ -353,6 +371,14 @@ class ArchiveResult(ABIDModel): def snapshot_dir(self): return Path(self.snapshot.link_dir) + @property + def api_url(self) -> str: + # /api/v1/core/archiveresult/{uulid} + return reverse_lazy('api-1:get_archiveresult', args=[self.abid]) + + @property + def api_docs_url(self) -> str: + return f'/api/v1/docs#/Core%20Models/api_v1_core_get_archiveresult' @property def extractor_module(self): diff --git a/archivebox/core/urls.py b/archivebox/core/urls.py index 14b3d774..04382c99 100644 --- a/archivebox/core/urls.py +++ b/archivebox/core/urls.py @@ -38,7 +38,7 @@ urlpatterns = [ path('accounts/', include('django.contrib.auth.urls')), path('admin/', archivebox_admin.urls), - path("api/", include('api.urls')), + path("api/", include('api.urls'), name='api'), path('health/', HealthCheckView.as_view(), name='healthcheck'), path('error/', lambda *_: 1/0),