Example #1
0
 def test_save(self):
     with self.app.app_context():
         s3 = boto3.client('s3')
         bucket_name = self.app.config['S3_BUCKET_NAME']
         s3.create_bucket(Bucket=bucket_name)
         metadata = BitStore(publisher="pub_test",
                             package="test_package",
                             body='hi')
         key = metadata.build_s3_key('datapackage.json')
         metadata.save()
         obs_list = list(s3.list_objects(Bucket=bucket_name, Prefix=key).\
                         get('Contents'))
         self.assertEqual(1, len(obs_list))
         self.assertEqual(key, obs_list[0]['Key'])
Example #2
0
def save_metadata(publisher, package):
    """
    DPR metadata put operation.
    This API is responsible for pushing  datapackage.json to S3.
    ---
    tags:
        - package
    parameters:
        - in: path
          name: publisher
          type: string
          required: true
          description: publisher name
        - in: path
          name: package
          type: string
          required: true
          description: package name
    responses:
        400:
            description: JWT is invalid or req body is not valid
        401:
            description: Invalid Header for JWT
        403:
            description: User name and publisher not matched
        404:
            description: User not found
        500:
            description: Internal Server Error
        200:
            description: Success Message
            schema:
                id: put_package_success
                properties:
                    status:
                        type: string
                        description: Status of the operation
                        default: OK
    """
    try:
        user = _request_ctx_stack.top.current_user
        user_id = user['user']
        user = User.query.filter_by(id=user_id).first()
        if user is not None:
            if user.name == publisher:
                metadata = BitStore(publisher=publisher,
                                    package=package,
                                    body=request.data)
                is_valid = metadata.validate()
                if not is_valid:
                    return handle_error('INVALID_DATA',
                                        'Missing required field in metadata',
                                        400)
                metadata.save()
                return jsonify({"status": "OK"}), 200
            return handle_error('NOT_PERMITTED',
                                'user name and publisher not matched', 403)
        return handle_error('USER_NOT_FOUND', 'user not found', 404)
    except Exception as e:
        app.logger.error(e)
        return handle_error('GENERIC_ERROR', e.message, 500)