Skip to content

Commit af669d2

Browse files
committed
rename api files for clarity
1 parent 716ba52 commit af669d2

File tree

5 files changed

+116
-103
lines changed

5 files changed

+116
-103
lines changed

archivebox/api/urls.py

+7-101
Original file line numberDiff line numberDiff line change
@@ -1,111 +1,17 @@
11
__package__ = 'archivebox.api'
22

3-
# import orjson
4-
5-
from io import StringIO
6-
from traceback import format_exception
7-
from contextlib import redirect_stdout, redirect_stderr
8-
93
from django.urls import path
10-
from django.http import HttpRequest, HttpResponse
114
from django.views.generic.base import RedirectView
12-
from django.core.exceptions import ObjectDoesNotExist, EmptyResultSet, PermissionDenied
13-
14-
from ninja import NinjaAPI, Swagger
15-
16-
# TODO: explore adding https://eadwincode.github.io/django-ninja-extra/
17-
18-
from api.auth import API_AUTH_METHODS
19-
from ..config import VERSION, COMMIT_HASH
20-
21-
# from ninja.renderers import BaseRenderer
22-
23-
# class ORJSONRenderer(BaseRenderer):
24-
# media_type = "application/json"
25-
26-
# def render(self, request, data, *, response_status):
27-
# return {
28-
# "success": True,
29-
# "errors": [],
30-
# "result": data,
31-
# "stdout": ansi_to_html(stdout.getvalue().strip()),
32-
# "stderr": ansi_to_html(stderr.getvalue().strip()),
33-
# }
34-
# return orjson.dumps(data)
35-
36-
37-
class NinjaAPIWithIOCapture(NinjaAPI):
38-
def create_temporal_response(self, request: HttpRequest) -> HttpResponse:
39-
stdout, stderr = StringIO(), StringIO()
40-
41-
with redirect_stderr(stderr):
42-
with redirect_stdout(stdout):
43-
request.stdout = stdout
44-
request.stderr = stderr
45-
46-
response = super().create_temporal_response(request)
47-
48-
print('RESPONDING NOW', response)
49-
50-
return response
51-
52-
html_description=f'''
53-
<h3>Welcome to your ArchiveBox server's REST API <code>[v1 ALPHA]</code> homepage!</h3>
54-
<br/>
55-
<i><b>WARNING: This API is still in an early development stage and may change!</b></i>
56-
<br/>
57-
<ul>
58-
<li>⬅️ Manage your server: <a href="/admin/api/"><b>Setup API Keys</b></a>, <a href="/admin/">Go to your Server Admin UI</a>, <a href="/">Go to your Snapshots list</a>
59-
<li>💬 Ask questions and get help here: <a href="https://zulip.archivebox.io">ArchiveBox Chat Forum</a></li>
60-
<li>🐞 Report API bugs here: <a href="https://github.com/ArchiveBox/ArchiveBox/issues">Github Issues</a></li>
61-
<li>📚 ArchiveBox Documentation: <a href="https://github.com/ArchiveBox/ArchiveBox/wiki">Github Wiki</a></li>
62-
<li>📜 See the API source code: <a href="https://github.com/ArchiveBox/ArchiveBox/blob/dev/archivebox/api"><code>archivebox/api/</code></a></li>
63-
</ul>
64-
<small>Served by ArchiveBox v{VERSION} (<a href="https://github.com/ArchiveBox/ArchiveBox/commit/{COMMIT_HASH}"><code>{COMMIT_HASH[:8]}</code></a>), API powered by <a href="https://django-ninja.dev/"><code>django-ninja</code></a>.</small>
65-
'''
66-
67-
api = NinjaAPIWithIOCapture(
68-
title='ArchiveBox API',
69-
description=html_description,
70-
version='1.0.0',
71-
csrf=False,
72-
auth=API_AUTH_METHODS,
73-
urls_namespace="api",
74-
docs=Swagger(settings={"persistAuthorization": True}),
75-
# docs_decorator=login_required,
76-
# renderer=ORJSONRenderer(),
77-
)
78-
api.add_router('/auth/', 'api.routes_auth.router')
79-
api.add_router('/core/', 'api.routes_core.router')
80-
api.add_router('/cli/', 'api.routes_cli.router')
81-
82-
83-
@api.exception_handler(Exception)
84-
def generic_exception_handler(request, err):
85-
status = 503
86-
if isinstance(err, (ObjectDoesNotExist, EmptyResultSet, PermissionDenied)):
87-
status = 404
88-
89-
print(''.join(format_exception(err)))
90-
91-
return api.create_response(
92-
request,
93-
{
94-
"succeeded": False,
95-
"errors": [
96-
''.join(format_exception(err)),
97-
# or send simpler exception-only summary without full traceback:
98-
# f'{err.__class__.__name__}: {err}',
99-
# *([str(err.__context__)] if getattr(err, '__context__', None) else []),
100-
],
101-
},
102-
status=status,
103-
)
1045

6+
from .v1_api import urls as v1_api_urls
1057

1068
urlpatterns = [
107-
path("v1/", api.urls),
9+
path("", RedirectView.as_view(url='/api/v1')),
10810

11+
path("v1/", v1_api_urls),
10912
path("v1", RedirectView.as_view(url='/api/v1/docs')),
110-
path("", RedirectView.as_view(url='/api/v1/docs')),
13+
14+
# ... v2 can be added here ...
15+
# path("v2/", v2_api_urls),
16+
# path("v2", RedirectView.as_view(url='/api/v2/docs')),
11117
]

archivebox/api/v1_api.py

+109
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
__package__ = 'archivebox.api'
2+
3+
4+
from io import StringIO
5+
from traceback import format_exception
6+
from contextlib import redirect_stdout, redirect_stderr
7+
8+
from django.http import HttpRequest, HttpResponse
9+
from django.core.exceptions import ObjectDoesNotExist, EmptyResultSet, PermissionDenied
10+
11+
from ninja import NinjaAPI, Swagger
12+
13+
# TODO: explore adding https://eadwincode.github.io/django-ninja-extra/
14+
15+
from api.auth import API_AUTH_METHODS
16+
from ..config import VERSION, COMMIT_HASH
17+
18+
19+
html_description=f'''
20+
<h3>Welcome to your ArchiveBox server's REST API <code>[v1 ALPHA]</code> homepage!</h3>
21+
<br/>
22+
<i><b>WARNING: This API is still in an early development stage and may change!</b></i>
23+
<br/>
24+
<ul>
25+
<li>⬅️ Manage your server: <a href="/admin/api/"><b>Setup API Keys</b></a>, <a href="/admin/">Go to your Server Admin UI</a>, <a href="/">Go to your Snapshots list</a>
26+
<li>💬 Ask questions and get help here: <a href="https://zulip.archivebox.io">ArchiveBox Chat Forum</a></li>
27+
<li>🐞 Report API bugs here: <a href="https://github.com/ArchiveBox/ArchiveBox/issues">Github Issues</a></li>
28+
<li>📚 ArchiveBox Documentation: <a href="https://github.com/ArchiveBox/ArchiveBox/wiki">Github Wiki</a></li>
29+
<li>📜 See the API source code: <a href="https://github.com/ArchiveBox/ArchiveBox/blob/dev/archivebox/api"><code>archivebox/api/</code></a></li>
30+
</ul>
31+
<small>Served by ArchiveBox v{VERSION} (<a href="https://github.com/ArchiveBox/ArchiveBox/commit/{COMMIT_HASH}"><code>{COMMIT_HASH[:8]}</code></a>), API powered by <a href="https://django-ninja.dev/"><code>django-ninja</code></a>.</small>
32+
'''
33+
34+
35+
def register_urls(api: NinjaAPI) -> NinjaAPI:
36+
api.add_router('/auth/', 'api.v1_auth.router')
37+
api.add_router('/core/', 'api.v1_core.router')
38+
api.add_router('/cli/', 'api.v1_cli.router')
39+
return api
40+
41+
42+
class NinjaAPIWithIOCapture(NinjaAPI):
43+
def create_temporal_response(self, request: HttpRequest) -> HttpResponse:
44+
stdout, stderr = StringIO(), StringIO()
45+
46+
with redirect_stderr(stderr):
47+
with redirect_stdout(stdout):
48+
request.stdout = stdout
49+
request.stderr = stderr
50+
51+
response = super().create_temporal_response(request)
52+
53+
print('RESPONDING NOW', response)
54+
55+
return response
56+
57+
58+
api = NinjaAPIWithIOCapture(
59+
title='ArchiveBox API',
60+
description=html_description,
61+
version='1.0.0',
62+
csrf=False,
63+
auth=API_AUTH_METHODS,
64+
urls_namespace="api",
65+
docs=Swagger(settings={"persistAuthorization": True}),
66+
# docs_decorator=login_required,
67+
# renderer=ORJSONRenderer(),
68+
)
69+
api = register_urls(api)
70+
urls = api.urls
71+
72+
73+
@api.exception_handler(Exception)
74+
def generic_exception_handler(request, err):
75+
status = 503
76+
if isinstance(err, (ObjectDoesNotExist, EmptyResultSet, PermissionDenied)):
77+
status = 404
78+
79+
print(''.join(format_exception(err)))
80+
81+
return api.create_response(
82+
request,
83+
{
84+
"succeeded": False,
85+
"message": f'{err.__class__.__name__}: {err}',
86+
"errors": [
87+
''.join(format_exception(err)),
88+
# or send simpler parent-only traceback:
89+
# *([str(err.__context__)] if getattr(err, '__context__', None) else []),
90+
],
91+
},
92+
status=status,
93+
)
94+
95+
96+
97+
# import orjson
98+
# from ninja.renderers import BaseRenderer
99+
# class ORJSONRenderer(BaseRenderer):
100+
# media_type = "application/json"
101+
# def render(self, request, data, *, response_status):
102+
# return {
103+
# "success": True,
104+
# "errors": [],
105+
# "result": data,
106+
# "stdout": ansi_to_html(stdout.getvalue().strip()),
107+
# "stderr": ansi_to_html(stderr.getvalue().strip()),
108+
# }
109+
# return orjson.dumps(data)
File renamed without changes.

archivebox/api/routes_cli.py archivebox/api/v1_cli.py

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
from typing import List, Dict, Any, Optional
44
from enum import Enum
55

6-
# from pydantic import BaseModel
7-
from archivebox.api.routes_core import paginate
86
from ninja import Router, Schema
97

108
from ..main import (
File renamed without changes.

0 commit comments

Comments
 (0)