Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
nopaque
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Monitor
Service Desk
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SFB 1288 - INF
nopaque
Commits
92721502
Commit
92721502
authored
2 years ago
by
Patrick Jentsch
Browse files
Options
Downloads
Patches
Plain Diff
combine content_negotiation related decorators
parent
cb830a6f
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
app/decorators.py
+16
-34
16 additions, 34 deletions
app/decorators.py
with
16 additions
and
34 deletions
app/decorators.py
+
16
−
34
View file @
92721502
...
...
@@ -65,52 +65,34 @@ def background(f):
return
wrapped
def
consumes
(
mime_type
:
str
,
*
_mime_types
:
str
):
def
decorator
(
f
):
@wraps
(
f
)
def
decorated_function
(
*
args
,
**
kwargs
):
provided
=
request
.
mimetype
consumeables
=
{
mime_type
,
*
_mime_types
}
if
provided
not
in
consumeables
:
raise
NotAcceptable
()
return
f
(
*
args
,
**
kwargs
)
return
decorated_function
return
decorator
def
produces
(
mime_type
:
str
,
*
_mime_types
:
str
):
def
decorator
(
f
):
@wraps
(
f
)
def
decorated_function
(
*
args
,
**
kwargs
):
accepted
=
{
*
request
.
accept_mimetypes
.
values
()}
produceables
=
{
mime_type
,
*
_mime_types
}
if
len
(
produceables
&
accepted
)
==
0
:
raise
NotAcceptable
()
return
f
(
*
args
,
**
kwargs
)
return
decorated_function
return
decorator
def
content_negotiation
(
produces
:
Union
[
str
,
List
[
str
]
]
,
consumes
:
Union
[
str
,
List
[
str
]
]
produces
:
Union
[
str
,
List
[
str
]
,
None
]
=
None
,
consumes
:
Union
[
str
,
List
[
str
]
,
None
]
=
None
):
def
decorator
(
f
):
@wraps
(
f
)
def
decorated_function
(
*
args
,
**
kwargs
):
provided
=
request
.
mimetype
if
isinstance
(
consumes
,
str
):
if
consumes
is
None
:
consumeables
=
None
elif
isinstance
(
consumes
,
str
):
consumeables
=
{
consumes
}
el
se
:
el
if
isinstance
(
consumes
,
list
)
and
all
(
isinstance
(
x
,
str
)
for
x
in
consumes
)
:
consumeables
=
{
*
consumes
}
else
:
raise
TypeError
()
accepted
=
{
*
request
.
accept_mimetypes
.
values
()}
if
isinstance
(
produces
,
str
):
if
produces
is
None
:
produceables
=
None
elif
isinstance
(
produces
,
str
):
produceables
=
{
produces
}
el
se
:
el
if
isinstance
(
produces
,
list
)
and
all
(
isinstance
(
x
,
str
)
for
x
in
produces
)
:
produceables
=
{
*
produces
}
if
len
(
produceables
&
accepted
)
==
0
:
else
:
raise
TypeError
()
if
produceables
is
not
None
and
len
(
produceables
&
accepted
)
==
0
:
raise
NotAcceptable
()
if
provided
not
in
consumeables
:
if
consumeables
is
not
None
and
provided
not
in
consumeables
:
raise
NotAcceptable
()
return
f
(
*
args
,
**
kwargs
)
return
decorated_function
...
...
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