Skip to content

Commit 541fd4b

Browse files
committed
Initial commit
0 parents  commit 541fd4b

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# copier-controller
2+
3+
more info soon
4+

USAGE

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
Description:
2+
Creates a new controller and its endpoints. Pass the controller name
3+
under_scored, and a list of endpoints as arguments.
4+
5+
This generates a controller file in app/controllers with unit test
6+
file. Finally, edits config/router.py in order to add controller to
7+
FastAPI router.
8+
9+
Example:
10+
`fastapi-mvc generate controller stock_market ticker buy:post sell:delete`
11+
12+
Creates controller with URLs like /stock_market/ticker.
13+
Controller: app/controllers/stock_market.py
14+
Test: tests/unit/app/controllers/test_stock_market.py
15+

copier.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# TEMPLATE SETTINGS
2+
_subdirectory: template
3+
_templates_suffix: .jinja
4+
_min_copier_version: "6.2.0"
5+
_envops:
6+
block_end_string: "%}"
7+
block_start_string: "{%"
8+
comment_end_string: "#}"
9+
comment_start_string: "{#"
10+
keep_trailing_newline: true
11+
variable_end_string: "}}"
12+
variable_start_string: "{{"
13+
14+
# TEMPLATE QUESTIONS
15+
project_name:
16+
type: str
17+
help: >-
18+
What's your project name?
19+
20+
Do not use dots or spaces in the name; just "A-Za-z0-9-_" please.
21+
22+
controller_name:
23+
type: str
24+
help: What is your new controller name?
25+
26+
controller_endpoints:
27+
type: json
28+
help: What are your new controller endpoints?
29+
multiline: true
30+
31+
skip_routes:
32+
type: bool
33+
help: Skip modifying routes?
34+
default: false
35+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import pytest
2+
from fastapi.testclient import TestClient
3+
from {{package_name}}.app.asgi import get_app
4+
5+
6+
@pytest.fixture
7+
def client():
8+
# This is an example fixture for generated test sake.
9+
# By default there should be a 'app' fixture just like this under:
10+
# tests/unit/app/conftest.py
11+
app = get_app()
12+
with TestClient(app) as client:
13+
yield client
14+
{%- for endpoint, method in controller_endpoints.items() %}
15+
16+
17+
def test_{{endpoint}}(client):
18+
response = client.{{method}}("/{{controller_name}}/{{endpoint}}")
19+
assert response.status_code == 200
20+
assert response.json() == {"hello": "world"}
21+
22+
23+
{%- endfor %}
24+
{%- raw %}
25+
26+
{% endraw %}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
"""{{ controller_name.capitalize().replace('_', ' ') }} controller implementation."""
2+
import logging
3+
4+
from fastapi import APIRouter
5+
6+
7+
router = APIRouter(
8+
prefix="/{{ controller_name }}"
9+
)
10+
log = logging.getLogger(__name__)
11+
{%- for endpoint, method in controller_endpoints.items() %}
12+
13+
14+
@router.{{ method }}(
15+
"/{{ endpoint }}",
16+
status_code=200,
17+
# Decorator options:
18+
# https://fastapi.tiangolo.com/tutorial/path-operation-configuration/
19+
)
20+
async def {{ endpoint }}():
21+
# Implement endpoint logic here.
22+
return {"hello": "world"}
23+
24+
25+
{%- endfor %}
26+
{%- raw %}
27+
28+
{% endraw %}

0 commit comments

Comments
 (0)