3

The following code works:

@api.route('/properties')
@api.doc(security='Basic Auth')
class CameraProperties(Resource):
  def get(self):
    return Utils.convertToDictionary(Camera().camera)
  @api.doc(body=camera_fields)
  def put(self):
    reqData = json.loads(request.data)
    for a in reqData:
      exec("Camera().camera." + a + "=reqData['" + a + "']")

@api.route('/stillshot')
@api.doc(security='Basic Auth')
class StillShot(Resource):
  def put(self):
    Camera().TakeStillShot()

However, in order to avoid repeating code, I would like to move the security decorator to the Namespace decorators list like so:

api = Namespace('camera', description='Camera operations', security='Basic Auth', authorizations=authorizations)
api.decorators = [auth.login_required, api.doc(security='Basic Auth')]

But if I do that, when I run the app Swagger doesn't show the padlock by the side of the methods, i.e. the decorator is not working.

The auth.login_required decorator does work, so I'm guessing this is something to do with the decorator parameters, but I can't figure out why it doesn't work.

The full code is here:

https://github.com/codewrite/PiCam1/tree/duplicated-swagger-decorator-does-work/py3/apis https://github.com/codewrite/PiCam1/blob/shared-swagger-decorator-not-working/py3/apis/camera.py

codewrite
  • 111
  • 5
  • 1
    It's not perfect, but I've found a workaround. It would seem that the security attribute can be added to the Api class, but not the Namespace class. This means I can put all the authentication code in __init__.py. Workaround is in this branch: https://github.com/codewrite/PiCam1/tree/swagger-authentication-api-workaround/py3/apis – codewrite Jun 26 '21 at 17:53

0 Answers0