Skip to content

Commit bf01486

Browse files
committed
MethodView mandates naming of methods
We don't like that and prefer to retain flexibility.
1 parent c9c94f2 commit bf01486

File tree

1 file changed

+101
-94
lines changed
  • mrmat_python_api_flask/apis/resource/v1

1 file changed

+101
-94
lines changed

mrmat_python_api_flask/apis/resource/v1/api.py

Lines changed: 101 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
from werkzeug.local import LocalProxy
2929
from flask import request, g, current_app
30-
from flask.views import MethodView
30+
#from flask import g, request
3131
from flask_smorest import Blueprint
3232
from marshmallow import ValidationError
3333

@@ -40,97 +40,104 @@
4040
logger = LocalProxy(lambda: current_app.logger)
4141

4242

43-
class ResourceV1(MethodView):
44-
45-
def _extract_identity(self) -> Tuple:
46-
return g.oidc_token_info['client_id'], \
47-
g.oidc_token_info['preferred_username']
48-
49-
@bp.route('/', methods=['GET'])
50-
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-read'])
51-
def get_all(self):
52-
identity = self._extract_identity()
53-
logger.info(f'Called by {identity[1]} ({identity[0]}')
54-
a = Resource.query.all()
55-
return {'resources': resources_schema.dump(a)}, 200
56-
57-
@bp.route('/<i>', methods=['GET'])
58-
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-read'])
59-
def get_one(self, i: int):
60-
identity = self._extract_identity()
61-
logger.info(f'Called by {identity[1]} ({identity[0]}')
62-
resource = Resource.query.filter(Resource.id == i).first_or_404()
63-
if resource is None:
64-
return {'status': 404, 'message': f'Unable to find entry with identifier {i} in database'}, 404
65-
return resource_schema.dump(resource), 200
66-
67-
68-
@bp.route('/', methods=['POST'])
69-
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-write'])
70-
def create(self):
71-
(client_id, name) = self._extract_identity()
72-
logger.info(f'Called by {name} ({client_id}')
73-
try:
74-
json_body = request.get_json()
75-
if not json_body:
76-
return {'message': 'No input data provided'}, 400
77-
body = resource_schema.load(request.get_json())
78-
except ValidationError as ve:
79-
return ve.messages, 422
80-
81-
#
82-
# Check if we have a resource with the same name and owner already
83-
84-
resource = Resource.query\
85-
.filter(Resource.name == body['name'] and Resource.owner.client_id == client_id)\
86-
.one_or_none()
87-
if resource is not None:
88-
return {'status': 409,
89-
'message': f'A resource with the same name and owner already exists with id {resource.id}'}, 409
90-
91-
#
92-
# Look up the owner and create one if necessary
93-
94-
owner = Owner.query.filter(Owner.client_id == client_id).one_or_none()
95-
if owner is None:
96-
owner = Owner(client_id=client_id, name=name)
97-
db.session.add(owner)
98-
99-
resource = Resource(owner=owner, name=body['name'])
100-
db.session.add(resource)
101-
db.session.commit()
102-
return resource_schema.dump(resource), 201
103-
104-
@bp.route('/<i>', methods=['PUT'])
105-
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-write'])
106-
def modify(self, i: int):
107-
(client_id, name) = self._extract_identity()
108-
logger.info(f'Called by {name} ({client_id}')
43+
def _extract_identity() -> Tuple:
44+
return g.oidc_token_info['client_id'], \
45+
g.oidc_token_info['preferred_username']
46+
47+
48+
@bp.get('/')
49+
@bp.doc(security=[{'mrmat_keycloak': ['mrmat-python-api-flask-resource-read']}])
50+
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-read'])
51+
def get_all():
52+
identity = _extract_identity()
53+
logger.info(f'Called by {identity[1]} ({identity[0]}')
54+
a = Resource.query.all()
55+
return {'resources': resources_schema.dump(a)}, 200
56+
57+
58+
@bp.get('/<i>')
59+
@bp.doc(security=[{'mrmat_keycloak': ['mrmat-python-api-flask-resource-read']}])
60+
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-read'])
61+
def get_one(i: int):
62+
identity = _extract_identity()
63+
#logger.info(f'Called by {identity[1]} ({identity[0]}')
64+
resource = Resource.query.filter(Resource.id == i).first_or_404()
65+
if resource is None:
66+
return {'status': 404, 'message': f'Unable to find entry with identifier {i} in database'}, 404
67+
return resource_schema.dump(resource), 200
68+
69+
70+
@bp.post('/')
71+
@bp.doc(security=[{'mrmat_keycloak': ['mrmat-python-api-flask-resource-write']}])
72+
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-write'])
73+
def create():
74+
(client_id, name) = _extract_identity()
75+
#logger.info(f'Called by {name} ({client_id}')
76+
try:
77+
json_body = request.get_json()
78+
if not json_body:
79+
return {'message': 'No input data provided'}, 400
10980
body = resource_schema.load(request.get_json())
110-
111-
resource = Resource.query.filter(Resource.id == i).one_or_none()
112-
if resource is None:
113-
return {'status': 404, 'message': 'Unable to find requested resource'}, 404
114-
if resource.owner.client_id != client_id:
115-
return {'status': 401, 'message': 'You do not own this resource'}, 401
116-
resource.name = body['name']
117-
118-
db.session.add(resource)
119-
db.session.commit()
120-
return resource_schema.dump(resource), 200
121-
122-
@bp.route('/<i>', methods=['DELETE'])
123-
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-write'])
124-
def remove(self, i: int):
125-
(client_id, name) = self._extract_identity()
126-
logger.info(f'Called by {name} ({client_id}')
127-
128-
resource = Resource.query.filter(Resource.id == i).one_or_none()
129-
if resource is None:
130-
return {'status': 410, 'message': 'Unable to find requested resource'}, 410
131-
if resource.owner.client_id != client_id:
132-
return {'status': 401, 'message': 'You do not own this resource'}, 401
133-
134-
db.session.delete(resource)
135-
db.session.commit()
136-
return {}, 204
81+
except ValidationError as ve:
82+
return ve.messages, 422
83+
84+
#
85+
# Check if we have a resource with the same name and owner already
86+
87+
resource = Resource.query\
88+
.filter(Resource.name == body['name'] and Resource.owner.client_id == client_id)\
89+
.one_or_none()
90+
if resource is not None:
91+
return {'status': 409,
92+
'message': f'A resource with the same name and owner already exists with id {resource.id}'}, 409
93+
94+
#
95+
# Look up the owner and create one if necessary
96+
97+
owner = Owner.query.filter(Owner.client_id == client_id).one_or_none()
98+
if owner is None:
99+
owner = Owner(client_id=client_id, name=name)
100+
db.session.add(owner)
101+
102+
resource = Resource(owner=owner, name=body['name'])
103+
db.session.add(resource)
104+
db.session.commit()
105+
return resource_schema.dump(resource), 201
106+
107+
108+
@bp.put('/<i>')
109+
@bp.doc(security=[{'mrmat_keycloak': ['mrmat-python-api-flask-resource-write']}])
110+
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-write'])
111+
def modify(i: int):
112+
(client_id, name) = _extract_identity()
113+
#logger.info(f'Called by {name} ({client_id}')
114+
body = resource_schema.load(request.get_json())
115+
116+
resource = Resource.query.filter(Resource.id == i).one_or_none()
117+
if resource is None:
118+
return {'status': 404, 'message': 'Unable to find requested resource'}, 404
119+
if resource.owner.client_id != client_id:
120+
return {'status': 401, 'message': 'You do not own this resource'}, 401
121+
resource.name = body['name']
122+
123+
db.session.add(resource)
124+
db.session.commit()
125+
return resource_schema.dump(resource), 200
126+
127+
128+
@bp.delete('/<i>')
129+
@bp.doc(security=[{'mrmat_keycloak': ['mrmat-python-api-flask-resource-write']}])
130+
@oidc.accept_token(require_token=True, scopes_required=['mrmat-python-api-flask-resource-write'])
131+
def remove(self, i: int):
132+
(client_id, name) = _extract_identity()
133+
#logger.info(f'Called by {name} ({client_id}')
134+
135+
resource = Resource.query.filter(Resource.id == i).one_or_none()
136+
if resource is None:
137+
return {'status': 410, 'message': 'Unable to find requested resource'}, 410
138+
if resource.owner.client_id != client_id:
139+
return {'status': 401, 'message': 'You do not own this resource'}, 401
140+
141+
db.session.delete(resource)
142+
db.session.commit()
143+
return {}, 204

0 commit comments

Comments
 (0)