Skip to content

Commit 0d62c5f

Browse files
pieterjan-oip1c2u
authored andcommitted
Add django wrapper
1 parent 9be404e commit 0d62c5f

File tree

1 file changed

+104
-0
lines changed

1 file changed

+104
-0
lines changed

openapi_core/wrappers/django.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
"""OpenAPI core wrappers module"""
2+
import re
3+
4+
from openapi_core.wrappers.base import BaseOpenAPIRequest, BaseOpenAPIResponse
5+
6+
# https://docs.djangoproject.com/en/2.2/topics/http/urls/
7+
#
8+
# Currently unsupported are :
9+
# - nested arguments, e.g.: ^comments/(?:page-(?P<page_number>\d+)/)?$
10+
# - unnamed regex groups, e.g.: ^articles/([0-9]{4})/$
11+
# - multiple named parameters between a single pair of slashes e.g.: <page_slug>-<page_id>/edit/
12+
#
13+
# The regex matches everything, except a "/" until "<". Than only the name is exported, after which it matches ">" and
14+
# everything until a "/".
15+
PATH_PARAMETER_PATTERN = r'(?:[^\/]*?)<(?:(?:.*?:))*?(\w+)>(?:[^\/]*)'
16+
17+
18+
class DjangoOpenAPIRequest(BaseOpenAPIRequest):
19+
path_regex = re.compile(PATH_PARAMETER_PATTERN)
20+
21+
def __init__(self, request):
22+
self.request = request
23+
24+
@property
25+
def host_url(self):
26+
"""
27+
:return: The host with scheme as IRI.
28+
"""
29+
return self.request._current_scheme_host
30+
31+
@property
32+
def path(self):
33+
"""
34+
:return: Requested path as unicode.
35+
"""
36+
return self.request.path
37+
38+
@property
39+
def method(self):
40+
"""
41+
:return: The request method, in lowercase.
42+
"""
43+
return self.request.method.lower()
44+
45+
@property
46+
def path_pattern(self):
47+
"""
48+
:return: The matched url pattern.
49+
"""
50+
return self.path_regex.sub(r'{\1}', self.request.resolver_match.route)
51+
52+
@property
53+
def parameters(self):
54+
"""
55+
:return: A dictionary of all parameters.
56+
"""
57+
return {
58+
'path': self.request.resolver_match.kwargs,
59+
'query': self.request.GET,
60+
'header': self.request.headers,
61+
'cookie': self.request.COOKIES,
62+
}
63+
64+
@property
65+
def body(self):
66+
"""
67+
:return: The request body, as string.
68+
"""
69+
return self.request.body
70+
71+
@property
72+
def mimetype(self):
73+
"""
74+
:return: Like content type, but without parameters (eg, without charset, type etc.) and always lowercase.
75+
For example if the content type is "text/HTML; charset=utf-8" the mimetype would be "text/html".
76+
"""
77+
return self.request.content_type
78+
79+
80+
class DjangoOpenAPIResponse(BaseOpenAPIResponse):
81+
82+
def __init__(self, response):
83+
self.response = response
84+
85+
@property
86+
def data(self):
87+
"""
88+
:return: The response body, as string.
89+
"""
90+
return self.response.content
91+
92+
@property
93+
def status_code(self):
94+
"""
95+
:return: The status code as integer.
96+
"""
97+
return self.response.status_code
98+
99+
@property
100+
def mimetype(self):
101+
"""
102+
:return: Lowercase content type without charset.
103+
"""
104+
return self.response["Content-Type"]

0 commit comments

Comments
 (0)