Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
B
backend_api
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Package registry
Container registry
Operate
Terraform modules
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
videoag
backend_api
Commits
2806cebb
Commit
2806cebb
authored
1 year ago
by
Simon Künzel
Browse files
Options
Downloads
Patches
Plain Diff
Add errors for invalid route decorators
parent
fefe33eb
No related branches found
No related tags found
1 merge request
!6
Feedback page & Misc
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/api/authentication.py
+4
-0
4 additions, 0 deletions
src/api/authentication.py
src/api/routes/route.py
+8
-0
8 additions, 0 deletions
src/api/routes/route.py
with
12 additions
and
0 deletions
src/api/authentication.py
+
4
−
0
View file @
2806cebb
...
@@ -135,6 +135,10 @@ class EffectiveViewPermissions(ViewPermissions):
...
@@ -135,6 +135,10 @@ class EffectiveViewPermissions(ViewPermissions):
def
api_moderator_route
(
require_csrf_token
:
bool
=
False
):
def
api_moderator_route
(
require_csrf_token
:
bool
=
False
):
def
decorator
(
func
):
def
decorator
(
func
):
if
hasattr
(
func
,
"
is_api_route
"
)
and
func
.
is_api_route
:
raise
Exception
(
"
@api_moderator_route() seems to be applied after @api_route(). @api_moderator_route()
"
"
should be the first (lowest) decorator!
"
)
@wraps
(
func
)
@wraps
(
func
)
def
wrapper
(
*
args
,
**
kwargs
):
def
wrapper
(
*
args
,
**
kwargs
):
if
not
is_moderator
():
if
not
is_moderator
():
...
...
This diff is collapsed.
Click to expand it.
src/api/routes/route.py
+
8
−
0
View file @
2806cebb
...
@@ -124,6 +124,9 @@ def api_add_route(path: str, methods: list[str],
...
@@ -124,6 +124,9 @@ def api_add_route(path: str, methods: list[str],
min_api_version
:
int
=
API_OLDEST_ACTIVE_VERSION
,
min_api_version
:
int
=
API_OLDEST_ACTIVE_VERSION
,
max_api_version
:
int
=
API_LATEST_VERSION
):
max_api_version
:
int
=
API_LATEST_VERSION
):
def
decorator
(
func
):
def
decorator
(
func
):
if
not
hasattr
(
func
,
"
is_api_route
"
)
or
not
func
.
is_api_route
:
raise
Exception
(
"
@api_add_route() seems to be applied before @api_function()
"
)
for
version
in
range
(
min_api_version
,
max_api_version
+
1
):
for
version
in
range
(
min_api_version
,
max_api_version
+
1
):
full_path
=
get_api_path
(
version
,
path
)
full_path
=
get_api_path
(
version
,
path
)
if
DEBUG_ENABLED
:
if
DEBUG_ENABLED
:
...
@@ -139,6 +142,10 @@ def api_function(track_in_diagnostics: bool = True,
...
@@ -139,6 +142,10 @@ def api_function(track_in_diagnostics: bool = True,
allow_while_disabled
:
bool
=
False
,
allow_while_disabled
:
bool
=
False
,
rate_limiters
:
tuple
[
HostBasedCounterRateLimiter
,
...]
=
_API_GLOBAL_RATE_LIMITERS
):
rate_limiters
:
tuple
[
HostBasedCounterRateLimiter
,
...]
=
_API_GLOBAL_RATE_LIMITERS
):
def
decorator
(
func
):
def
decorator
(
func
):
if
hasattr
(
func
,
"
is_api_route
"
)
and
func
.
is_api_route
:
raise
Exception
(
"
An @api_function() decorator has already been applied. Are you using multiple @api_route()?
"
"
Use @api_add_route(...)@api_add_route(..)@api_function() instead
"
)
call_counter
=
None
call_counter
=
None
if
track_in_diagnostics
:
if
track_in_diagnostics
:
func_name
:
str
=
func
.
__name__
func_name
:
str
=
func
.
__name__
...
@@ -199,6 +206,7 @@ def api_function(track_in_diagnostics: bool = True,
...
@@ -199,6 +206,7 @@ def api_function(track_in_diagnostics: bool = True,
traceback
.
print_exception
(
e
)
traceback
.
print_exception
(
e
)
return
api_on_error
(
ERROR_INTERNAL_SERVER_ERROR
)
return
api_on_error
(
ERROR_INTERNAL_SERVER_ERROR
)
wrapper
.
is_api_route
=
True
return
wrapper
return
wrapper
return
decorator
return
decorator
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment