Skip to content

Commit 8bea36d

Browse files
authored
Move example snippets to example directory (#36)
1 parent cae72d1 commit 8bea36d

5 files changed

Lines changed: 418 additions & 102 deletions

File tree

README.rst

Lines changed: 0 additions & 102 deletions
Original file line numberDiff line numberDiff line change
@@ -20,108 +20,6 @@ Prerequisites
2020

2121
- python-oas does not validate OpenAPI 3 document itself at runtime. It should be validated in advance.
2222

23-
Example
24-
-------
25-
26-
.. code-block:: python
27-
28-
import flask
29-
import oas
30-
31-
app = flask.Flask(__name__)
32-
spec = oas.create_spec_from_dict(spec_dict)
33-
34-
35-
@app.route('/example')
36-
def example():
37-
# Create an instance of the subclass of oas.Request.
38-
oas_req = FlaskRequestAdapter(flask.request)
39-
# Find Operation Object for the request.
40-
operation = spec.get_operation(
41-
oas_req.uri_template, oas_req.method, oas_req.media_type
42-
)
43-
# Unmarshal the request and obtain unmarshaled parameters and
44-
# request body.
45-
schema_unmarshaler = oas.SchemaUnmarshaler(spec=spec)
46-
parameters, request_body = oas.unmarshal_request(
47-
schema_unmarshaler, oas_req, operation
48-
)
49-
# ...
50-
return flask.jsonify({...})
51-
52-
``spec.get_operation()`` and ``oas.unmarshal_request()`` may raise ``oas.exceptions.UndocumentedMediaType`` and ``oas.exceptions.UnmarshalError`` respectively. You should register error handlers for them:
53-
54-
.. code-block:: python
55-
56-
@app.errorhandler(oas.exceptions.UndocumentedMediaType)
57-
def handle_undocumented_media_type(e):
58-
return str(e), 400
59-
60-
61-
@app.errorhandler(oas.exceptions.UnmarshalError)
62-
def handle_unmarshal_error(e):
63-
return str(e), 400
64-
65-
``FlaskRequestAdapter`` might be something like the following:
66-
67-
.. code-block:: python
68-
69-
import re
70-
71-
_uri_template_re = re.compile(r'<(?:[^:]+:)?([^>]+)>')
72-
73-
74-
class FlaskRequestAdapter(oas.Request):
75-
76-
def __init__(self, request):
77-
self._request = request
78-
79-
@property
80-
def uri_template(self):
81-
url_rule = self._request.url_rule
82-
if url_rule is not None:
83-
return _uri_template_re.sub(r'{\1}', url_rule.rule)
84-
85-
@property
86-
def method(self):
87-
return self._request.method.lower()
88-
89-
@property
90-
def context(self):
91-
return None
92-
93-
@property
94-
def path(self):
95-
return self._request.view_args
96-
97-
@property
98-
def query(self):
99-
return self._request.args
100-
101-
@property
102-
def header(self):
103-
return self._request.headers
104-
105-
@property
106-
def cookie(self):
107-
return self._request.cookies
108-
109-
@property
110-
def content_length(self):
111-
return self._request.content_length
112-
113-
@property
114-
def media_type(self):
115-
content_type = self._request.content_type
116-
if content_type is not None:
117-
return content_type.split(';', 1)[0]
118-
119-
@property
120-
def media(self):
121-
# TODO: Support media types other than JSON
122-
return self._request.json
123-
124-
12523
Related projects
12624
----------------
12725

example/Pipfile

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[[source]]
2+
name = "pypi"
3+
url = "https://pypi.org/simple"
4+
verify_ssl = true
5+
6+
[dev-packages]
7+
pytest = "*"
8+
9+
[packages]
10+
flask = "~=1.1"
11+
oas = {editable = true, path = "./.."}
12+
pyyaml = "~=5.3"
13+
14+
[requires]
15+
python_version = "3.8"

example/Pipfile.lock

Lines changed: 236 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

example/tests.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import pytest
2+
import wsgi
3+
4+
5+
@pytest.fixture
6+
def client(monkeypatch):
7+
monkeypatch.setattr(
8+
wsgi, 'PETS', [wsgi.Pet(id=1, name='a'), wsgi.Pet(id=2, name='b')]
9+
)
10+
with wsgi.app.test_client() as client:
11+
yield client
12+
13+
14+
def test_collection_post(client):
15+
response = client.post('/api/v1/pets', json={'name': 'c'})
16+
assert response.json == {'id': 3, 'name': 'c'}
17+
18+
19+
def test_collection_get(client):
20+
response = client.get('/api/v1/pets')
21+
assert response.json == [
22+
{'id': 1, 'name': 'a'},
23+
{'id': 2, 'name': 'b'},
24+
]
25+
26+
27+
def test_item_get(client):
28+
response = client.get('/api/v1/pets/1')
29+
assert response.json == {'id': 1, 'name': 'a'}
30+
31+
response = client.get('/api/v1/pets/3')
32+
assert response.status_code == 404

0 commit comments

Comments
 (0)