Rename id parameters, improve doc for some resources.

This commit is contained in:
Alexis Lahouze 2017-05-19 11:49:54 +02:00
parent bbe4fd7f39
commit a27f33aabf
3 changed files with 95 additions and 78 deletions

View File

@ -165,11 +165,11 @@ class AccountListResource(Resource):
return account, 201 return account, 201
@ns.route('/<int:id>') @ns.route('/<int:account_id>')
@ns.doc( @ns.doc(
security='apikey', security='apikey',
params={ params={
'id': 'Id of the account to manage' 'account_id': 'Id of the account to manage'
}, },
responses={ responses={
401: 'Unauthorized', 401: 'Unauthorized',
@ -181,15 +181,15 @@ class AccountResource(Resource):
@ns.response(200, 'OK', account_model) @ns.response(200, 'OK', account_model)
@ns.marshal_with(account_model) @ns.marshal_with(account_model)
@jwt_required @jwt_required
def get(self, id): def get(self, account_id):
"""Get an account.""" """Get an account."""
account = Account.query().get(id) account = Account.query().get(account_id)
if not account: if not account:
ns.abort( ns.abort(
404, 404,
error_message='Account with id %d not found.' % id error_message='Account with id %d not found.' % account_id
) )
# Note: if we don't pass the code, the result is seen as a tuple and # Note: if we don't pass the code, the result is seen as a tuple and
@ -201,25 +201,25 @@ class AccountResource(Resource):
@ns.response(406, 'Invalid account data') @ns.response(406, 'Invalid account data')
@ns.marshal_with(account_model) @ns.marshal_with(account_model)
@jwt_required @jwt_required
def post(self, id): def post(self, account_id):
"""Update an account.""" """Update an account."""
data = self.api.payload data = self.api.payload
# Check ID consistency. # Check ID consistency.
if 'id' in data and data['id'] and data['id'] != id: if 'id' in data and data['id'] and data['id'] != account_id:
ns.abort( ns.abort(
406, 406,
error_message='Id must not be provided or changed on update.' error_message='Id must not be provided or changed on update.'
) )
# Need to get the object to update it. # Need to get the object to update it.
account = Account.query().get(id) account = Account.query().get(account_id)
if not account: if not account:
ns.abort( ns.abort(
404, 404,
error_message='Account with id %d not found.' % id error_message='Account with id %d not found.' % account_id
) )
# SQLAlchemy objects ignore __dict__.update() with merge. # SQLAlchemy objects ignore __dict__.update() with merge.
@ -234,16 +234,16 @@ class AccountResource(Resource):
@ns.response(204, 'Account deleted', account_model) @ns.response(204, 'Account deleted', account_model)
@ns.marshal_with(account_model) @ns.marshal_with(account_model)
@jwt_required @jwt_required
def delete(self, id): def delete(self, account_id):
"""Delete an account.""" """Delete an account."""
# Need to get the object to update it. # Need to get the object to update it.
account = Account.query().get(id) account = Account.query().get(account_id)
if not account: if not account:
ns.abort( ns.abort(
404, 404,
error_message='Account with id %d not found.' % id error_message='Account with id %d not found.' % account_id
) )
db.session.delete(account) db.session.delete(account)
@ -251,28 +251,31 @@ class AccountResource(Resource):
return None, 204 return None, 204
@ns.route('/<int:id>/solds') @ns.route('/<int:account_id>/solds')
@ns.doc(
security='apikey',
params={
'account_id': 'Id of the account to manage'
},
responses={
200: ('OK', solds_model),
401: 'Unauthorized',
404: 'Account not found'
})
class SoldsResource(Resource): class SoldsResource(Resource):
"""Resource to expose solds.""" """Resource to expose solds."""
@ns.doc(
security='apikey',
responses={
200: ('OK', solds_model),
401: 'Unauthorized',
404: 'Account not found'
})
@ns.marshal_with(solds_model) @ns.marshal_with(solds_model)
@jwt_required @jwt_required
def get(self, id): def get(self, account_id):
"""Get solds for a specific account and date range.""" """Get solds for a specific account and date range."""
account = Account.query().get(id) account = Account.query().get(account_id)
if not account: if not account:
ns.abort( ns.abort(
404, 404,
error_message='Account with id %d not found.' % id error_message='Account with id %d not found.' % account_id
) )
# Note: if we don't pass the code, the result is seen as a tuple and # Note: if we don't pass the code, the result is seen as a tuple and
@ -280,29 +283,32 @@ class SoldsResource(Resource):
return account.solds(), 200 return account.solds(), 200
@ns.route('/<int:id>/balance') @ns.route('/<int:account_id>/balance')
@ns.doc(
security='apikey',
params={
'account_id': 'Id of the account to manage'
},
responses={
200: ('OK', balance_model),
401: 'Unauthorized',
404: 'Account not found'
})
class BalanceResource(Resource): class BalanceResource(Resource):
"""Resource to expose balances.""" """Resource to expose balances."""
@ns.doc(
security='apikey',
responses={
200: ('OK', balance_model),
401: 'Unauthorized',
404: 'Account not found'
})
@ns.expect(range_parser) @ns.expect(range_parser)
@ns.marshal_with(balance_model) @ns.marshal_with(balance_model)
@jwt_required @jwt_required
def get(self, id): def get(self, account_id):
"""Get account balance for a specific date range.""" """Get account balance for a specific date range."""
account = Account.query().get(id) account = Account.query().get(account_id)
if not account: if not account:
ns.abort( ns.abort(
404, 404,
error_message='Account with id %d not found.' % id error_message='Account with id %d not found.' % account_id
) )
data = range_parser.parse_args() data = range_parser.parse_args()
@ -312,45 +318,51 @@ class BalanceResource(Resource):
return account.balance(**data), 200 return account.balance(**data), 200
@ns.route("/<int:id>/category") @ns.route("/<int:account_id>/category")
@ns.doc(
security='apikey',
params={
'account_id': 'Id of the account to manage'
},
responses={
200: ('OK', [category_model]),
401: 'Unauthorized',
404: 'Account not found'
})
class CategoryResource(Resource): class CategoryResource(Resource):
"""Resource to expose categories.""" """Resource to expose categories."""
@ns.doc(
security='apikey',
responses={
200: ('OK', [category_model]),
401: 'Unauthorized',
404: 'Account not found'
})
@ns.expect(range_parser) @ns.expect(range_parser)
@ns.marshal_list_with(category_model) @ns.marshal_list_with(category_model)
@jwt_required @jwt_required
def get(self, id): def get(self, account_id):
"""Get account category balances for a specific date range.""" """Get account category balances for a specific date range."""
data = range_parser.parse_args() data = range_parser.parse_args()
return Operation.get_categories_for_range(id, **data).all() return Operation.get_categories_for_range(account_id, **data).all()
@ns.route('/<int:id>/ohlc') @ns.route('/<int:account_id>/ohlc')
@ns.doc(
security='apikey',
params={
'account_id': 'Id of the account to manage'
},
responses={
200: ('OK', [ohlc_model]),
401: 'Unauthorized',
404: 'Account not found'
})
class OHLCResource(Resource): class OHLCResource(Resource):
"""Resource to expose OHLC.""" """Resource to expose OHLC."""
@ns.doc(
security='apikey',
responses={
200: ('OK', [ohlc_model]),
401: 'Unauthorized',
404: 'Account not found'
})
@ns.expect(range_parser) @ns.expect(range_parser)
@ns.marshal_list_with(ohlc_model) @ns.marshal_list_with(ohlc_model)
@jwt_required @jwt_required
def get(self, id): def get(self, account_id):
"""Get OHLC data for a specific date range and account.""" """Get OHLC data for a specific date range and account."""
data = range_parser.parse_args() data = range_parser.parse_args()
return Operation.get_ohlc_per_day_for_range(id, **data).all() return Operation.get_ohlc_per_day_for_range(account_id, **data).all()

View File

@ -146,11 +146,11 @@ class OperationListResource(Resource):
return operation, 201 return operation, 201
@ns.route('/<int:id>') @ns.route('/<int:operation_id>')
@ns.doc( @ns.doc(
security='apikey', security='apikey',
params={ params={
'id': 'Id of the operation to manage' 'operation_id': 'Id of the operation to manage'
}, },
responses={ responses={
401: 'Unauthorized', 401: 'Unauthorized',
@ -162,15 +162,15 @@ class OperationResource(Resource):
@ns.response(200, 'OK', operation_model) @ns.response(200, 'OK', operation_model)
@ns.marshal_with(operation_model) @ns.marshal_with(operation_model)
@jwt_required @jwt_required
def get(self, id): def get(self, operation_id):
"""Get operation.""" """Get operation."""
operation = db.session.query(Operation).get(id) operation = db.session.query(Operation).get(operation_id)
if not operation: if not operation:
ns.abort( ns.abort(
404, 404,
error_message='Operation with id %d not found.' % id error_message='Operation with id %d not found.' % operation_id
) )
return operation, 200 return operation, 200
@ -180,24 +180,24 @@ class OperationResource(Resource):
@ns.response(406, 'Invalid operation data') @ns.response(406, 'Invalid operation data')
@ns.marshal_with(operation_model) @ns.marshal_with(operation_model)
@jwt_required @jwt_required
def post(self, id): def post(self, operation_id):
"""Update an operation.""" """Update an operation."""
data = self.api.payload data = self.api.payload
# Check ID consistency. # Check ID consistency.
if 'id' in data and data['id'] and data['id'] != id: if 'id' in data and data['id'] and data['id'] != operation_id:
ns.abort( ns.abort(
406, 406,
error_message='Id must not be provided or changed on update.' error_message='Id must not be provided or changed on update.'
) )
operation = db.session.query(Operation).get(id) operation = db.session.query(Operation).get(operation_id)
if not operation: if not operation:
ns.abort( ns.abort(
404, 404,
error_message='Operation with id %d not found.' % id error_message='Operation with id %d not found.' % operation_id
) )
# FIXME check account_id consistency. # FIXME check account_id consistency.
@ -213,15 +213,15 @@ class OperationResource(Resource):
@ns.response(204, 'Operation deleted', operation_model) @ns.response(204, 'Operation deleted', operation_model)
@ns.marshal_with(operation_model) @ns.marshal_with(operation_model)
@jwt_required @jwt_required
def delete(self, id): def delete(self, operation_id):
"""Delete an operation.""" """Delete an operation."""
operation = db.session.query(Operation).get(id) operation = db.session.query(Operation).get(operation_id)
if not operation: if not operation:
ns.abort( ns.abort(
404, 404,
error_message='Operation with id %d not found.' % id error_message='Operation with id %d not found.' % operation_id
) )
db.session.delete(operation) db.session.delete(operation)

View File

@ -124,11 +124,11 @@ class ScheduledOperationListResource(Resource):
return scheduled_operation, 201 return scheduled_operation, 201
@ns.route('/<int:id>') @ns.route('/<int:scheduled_operation_id>')
@ns.doc( @ns.doc(
security='apikey', security='apikey',
params={ params={
'id': 'Id of the scheduled operation to manage' 'scheduled_operation_id': 'Id of the scheduled operation to manage'
}, },
responses={ responses={
401: 'Unauthorized', 401: 'Unauthorized',
@ -140,15 +140,17 @@ class ScheduledOperationResource(Resource):
@ns.response(200, 'OK', scheduled_operation_model) @ns.response(200, 'OK', scheduled_operation_model)
@ns.marshal_with(scheduled_operation_model) @ns.marshal_with(scheduled_operation_model)
@jwt_required @jwt_required
def get(self, id): def get(self, scheduled_operation_id):
"""Get scheduled operation.""" """Get scheduled operation."""
scheduled_operation = ScheduledOperation.query().get(id) so_id = scheduled_operation_id
scheduled_operation = ScheduledOperation.query().get(so_id)
if not scheduled_operation: if not scheduled_operation:
ns.abort( ns.abort(
404, 404,
error_message='Scheduled operation with id %d not found.' % id error_message='Scheduled operation with id %d not found.' % so_id
) )
return scheduled_operation, 200 return scheduled_operation, 200
@ -158,24 +160,25 @@ class ScheduledOperationResource(Resource):
@ns.expect(scheduled_operation_model) @ns.expect(scheduled_operation_model)
@ns.marshal_with(scheduled_operation_model) @ns.marshal_with(scheduled_operation_model)
@jwt_required @jwt_required
def post(self, id): def post(self, scheduled_operation_id):
"""Update a scheduled operation.""" """Update a scheduled operation."""
data = self.api.payload data = self.api.payload
so_id = scheduled_operation_id
# Check ID consistency. # Check ID consistency.
if 'id' in data and data['id'] and data['id'] != id: if 'id' in data and data['id'] and data['id'] != so_id:
ns.abort( ns.abort(
406, 406,
error_message='Id must not be provided or changed on update.' error_message='Id must not be provided or changed on update.'
) )
scheduled_operation = ScheduledOperation.query().get(id) scheduled_operation = ScheduledOperation.query().get(so_id)
if not scheduled_operation: if not scheduled_operation:
ns.abort( ns.abort(
404, 404,
error_message='Scheduled operation with id %d not found.' % id error_message='Scheduled operation with id %d not found.' % so_id
) )
# FIXME check account_id consistency. # FIXME check account_id consistency.
@ -196,15 +199,17 @@ class ScheduledOperationResource(Resource):
@ns.response(409, 'Cannot be deleted') @ns.response(409, 'Cannot be deleted')
@ns.marshal_with(scheduled_operation_model) @ns.marshal_with(scheduled_operation_model)
@jwt_required @jwt_required
def delete(self, id): def delete(self, scheduled_operation_id):
"""Delete a scheduled operation.""" """Delete a scheduled operation."""
scheduled_operation = ScheduledOperation.query().get(id) so_id = scheduled_operation_id
scheduled_operation = ScheduledOperation.query().get(so_id)
if not scheduled_operation: if not scheduled_operation:
ns.abort( ns.abort(
404, 404,
error_message='Scheduled operation with id %d not found.' % id error_message='Scheduled operation with id %d not found.' % so_id
) )
operations = scheduled_operation.operations.filter( operations = scheduled_operation.operations.filter(