def get(self): """ Get all imagery licenses --- tags: - licenses produces: - application/json responses: 200: description: Licenses found 404: description: Licenses not found 500: description: Internal Server Error """ try: licenses_dto = LicenseService.get_all_licenses() return licenses_dto.to_primitive(), 200 except NotFound: return {"Error": "License Not Found"}, 404 except Exception as e: error_msg = f'License PUT - unhandled error: {str(e)}' current_app.logger.critical(error_msg) return {"error": error_msg}, 500
def get(self, license_id): """ Get specified mapping license --- tags: - licenses produces: - application/json parameters: - name: license_id in: path description: The unique license ID required: true type: integer default: 1 responses: 200: description: License found 404: description: License not found 500: description: Internal Server Error """ try: license_dto = LicenseService.get_license_as_dto(license_id) return license_dto.to_primitive(), 200 except NotFound: return {"Error": "License Not Found"}, 404 except Exception as e: error_msg = f'License PUT - unhandled error: {str(e)}' current_app.logger.critical(error_msg) return {"error": error_msg}, 500
def put(self): """ Creates a new mapping license --- tags: - licenses produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - in: body name: body required: true description: JSON object for creating a new mapping license schema: properties: name: type: string default: Public Domain description: type: string default: This imagery is in the public domain. plainText: type: string default: This imagery is in the public domain. responses: 200: description: New license created 400: description: Invalid Request 401: description: Unauthorized - Invalid credentials 500: description: Internal Server Error """ try: license_dto = LicenseDTO(request.get_json()) license_dto.validate() except DataError as e: current_app.logger.error(f'Error validating request: {str(e)}') return str(e), 400 try: new_license_id = LicenseService.create_licence(license_dto) return {"licenseId": new_license_id}, 200 except Exception as e: error_msg = f'License PUT - unhandled error: {str(e)}' current_app.logger.critical(error_msg) return {"error": error_msg}, 500
def delete(self, license_id): """ Delete the specified mapping license --- tags: - licenses produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: license_id in: path description: The unique license ID required: true type: integer default: 1 responses: 200: description: License deleted 401: description: Unauthorized - Invalid credentials 404: description: License not found 500: description: Internal Server Error """ try: LicenseService.delete_license(license_id) return {"Success": "License deleted"}, 200 except NotFound: return {"Error": "License Not Found"}, 404 except Exception as e: error_msg = f'License DELETE - unhandled error: {str(e)}' current_app.logger.critical(error_msg) return {"error": error_msg}, 500
def post(self, license_id): """ Update a new mapping license --- tags: - licenses produces: - application/json parameters: - in: header name: Authorization description: Base64 encoded session token required: true type: string default: Token sessionTokenHere== - name: license_id in: path description: The unique license ID required: true type: integer default: 1 - in: body name: body required: true description: JSON object for creating a new mapping license schema: properties: name: type: string default: Public Domain description: type: string default: This imagery is in the public domain. plainText: type: string default: This imagery is in the public domain. responses: 200: description: New license created 400: description: Invalid Request 401: description: Unauthorized - Invalid credentials 500: description: Internal Server Error """ try: license_dto = LicenseDTO(request.get_json()) license_dto.license_id = license_id license_dto.validate() except DataError as e: current_app.logger.error(f'Error validating request: {str(e)}') return str(e), 400 try: updated_license = LicenseService.update_licence(license_dto) return updated_license.to_primitive(), 200 except NotFound: return {"Error": "License Not Found"}, 404 except Exception as e: error_msg = f'License POST - unhandled error: {str(e)}' current_app.logger.critical(error_msg) return {"error": error_msg}, 500
def _validate_imagery_licence(license_id: int): """ Ensures that the suppliced license Id actually exists """ try: LicenseService.get_license_as_dto(license_id) except NotFound: raise ProjectAdminServiceError(f'LicenseId {license_id} not found')
def test_license_crud_works_as_expected(self): if self.skip_tests: return # Arrange license_dto = LicenseDTO() license_dto.name = "thinkWhere" license_dto.description = 'thinkWhere test' license_dto.plain_text = 'thinkWhere test' # Act license_id = LicenseService.create_licence(license_dto) try: actual_license = LicenseService.get_license_as_dto(license_id) # Assert PUT and GET self.assertEqual(actual_license.name, license_dto.name) license_dto.license_id = license_id license_dto.name = 'Updated name' LicenseService.update_licence(license_dto) updated_license = LicenseService.get_license_as_dto(license_id) # Assert POST self.assertEqual(updated_license.name, license_dto.name) all_licenses = LicenseService.get_all_licenses() # Assert Get all self.assertGreaterEqual(1, len(all_licenses)) # Assert Delete LicenseService.delete_license(license_id) with self.assertRaises(NotFound): LicenseService.get_license_as_dto(license_id) except Exception: # If any problem occurs try and tidy up LicenseService.delete_license(license_id)