API improvements

This commit is contained in:
Nick Sweeting 2024-11-18 04:27:19 -08:00
parent c7bd9449d5
commit eeb2671e4d
No known key found for this signature in database
7 changed files with 157 additions and 127 deletions

View file

@ -16,12 +16,13 @@ from ninja.errors import HttpError
from core.models import Snapshot, ArchiveResult, Tag
from api.models import APIToken, OutboundWebhook
from crawls.models import Crawl
from seeds.models import Seed
from api.v1_crawls import CrawlSchema, SeedSchema
from .auth import API_AUTH_METHODS
# from .auth import API_AUTH_METHODS
router = Router(tags=['Core Models'], auth=API_AUTH_METHODS)
router = Router(tags=['Core Models'])
@ -397,108 +398,6 @@ def get_tag(request, tag_id: str, with_snapshots: bool=True):
pass
return tag
class SeedSchema(Schema):
TYPE: str = 'seeds.models.Seed'
id: UUID
abid: str
modified_at: datetime
created_at: datetime
created_by_id: str
created_by_username: str
uri: str
tags_str: str
config: dict
@staticmethod
def resolve_created_by_id(obj):
return str(obj.created_by_id)
@staticmethod
def resolve_created_by_username(obj):
User = get_user_model()
return User.objects.get(id=obj.created_by_id).username
@router.get("/seeds", response=List[SeedSchema], url_name="get_seeds")
def get_seeds(request):
return Seed.objects.all().distinct()
@router.get("/seed/{seed_id}", response=SeedSchema, url_name="get_seed")
def get_seed(request, seed_id: str):
seed = None
request.with_snapshots = False
request.with_archiveresults = False
try:
seed = Seed.objects.get(Q(abid__icontains=seed_id) | Q(id__icontains=seed_id))
except Exception:
pass
return seed
class CrawlSchema(Schema):
TYPE: str = 'core.models.Crawl'
id: UUID
abid: str
modified_at: datetime
created_at: datetime
created_by_id: str
created_by_username: str
status: str
retry_at: datetime | None
seed: SeedSchema
max_depth: int
# snapshots: List[SnapshotSchema]
@staticmethod
def resolve_created_by_id(obj):
return str(obj.created_by_id)
@staticmethod
def resolve_created_by_username(obj):
User = get_user_model()
return User.objects.get(id=obj.created_by_id).username
@staticmethod
def resolve_snapshots(obj, context):
if context['request'].with_snapshots:
return obj.snapshot_set.all().distinct()
return Snapshot.objects.none()
@router.get("/crawls", response=List[CrawlSchema], url_name="get_crawls")
def get_crawls(request):
return Crawl.objects.all().distinct()
@router.get("/crawl/{crawl_id}", response=CrawlSchema, url_name="get_crawl")
def get_crawl(request, crawl_id: str, with_snapshots: bool=False, with_archiveresults: bool=False):
"""Get a specific Crawl by id or abid."""
crawl = None
request.with_snapshots = with_snapshots
request.with_archiveresults = with_archiveresults
try:
crawl = Crawl.objects.get(abid__icontains=crawl_id)
except Exception:
pass
try:
crawl = crawl or Crawl.objects.get(id__icontains=crawl_id)
except Exception:
pass
return crawl
@router.get("/any/{abid}", response=Union[SnapshotSchema, ArchiveResultSchema, TagSchema, SeedSchema, CrawlSchema], url_name="get_any", summary="Get any object by its ABID or ID (e.g. snapshot, archiveresult, tag, seed, crawl, etc.)")
def get_any(request, abid: str):
"""Get any object by its ABID or ID (e.g. snapshot, archiveresult, tag, seed, crawl, etc.)."""
@ -529,11 +428,13 @@ def get_any(request, abid: str):
pass
try:
from api.v1_crawls import get_seed
response = response or get_seed(request, abid)
except Exception:
pass
try:
from api.v1_crawls import get_crawl
response = response or get_crawl(request, abid)
except Exception:
pass