Example #1
0
def update_link(org_label: str,
                project_label: str,
                filename: str,
                filepath: str,
                media_type: str,
                rev: int,
                file_id: str,
                storage_id: Optional[str] = None) -> Dict:
    """
        Update a file (of any kind, not necessarily a link) with a link.

        :param org_label: The label of the organization that the file belongs to
        :param project_label: The label of the project that the file belongs to
        :param filename: The filename that will be exposed in the resource metadata
        :param filepath: The path (relative to its storage root) of the file to link
        :param media_type: The linked file's media type
        :param rev: The previous file revision
        :param file_id: The previous file resource id
        :param storage_id: OPTIONAL The id of the storage backend where the file is located.
                           If not provided, the project's default storage is used.
        :return: A payload containing only the Nexus metadata for this linked file.
    """

    # the elements composing the query URL need to be URL-encoded
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

    payload = {"filename": filename, "path": filepath, "mediaType": media_type}

    request_path = [SEGMENT, org_label, project_label, url_encode(file_id)]

    return http_put(request_path, body=payload, rev=rev, storage=storage_id)
Example #2
0
def update_(file: Dict, filepath: str, rev: Optional[int] = None, storage_id: Optional[str] = None,
            content_type: Optional[str] = None) -> Dict:
    """
        Update a file, from an existing file resource payload, for instance the value returned
        by nexus.file.fetch(), where some fields where modified, added or removed.
        Note that the returned payload only contains the Nexus metadata and not the
        complete file.

        :param file: Payload of a previously fetched file.
        :param rev: OPTIONAL The previous revision you want to update from.
                    If not provided, the rev from the file argument will be used.
        :param storage_id: OPTIONAL The id of the storage backend where the file will be stored.
                           If not provided, the project's default storage is used.
        :param content_type: OPTIONAL Overrides the automatically detected content type
        :return: A payload containing only the Nexus metadata for this updated file.
    """

    if rev is None:
        rev = file["_rev"]

    path = file["_self"]

    file_obj = {"file": (file["_filename"], open(filepath, "rb"), _content_type(filepath, content_type))}

    return http_put(path, body=file_obj, data_type="file", rev=rev, storage=storage_id)
Example #3
0
def create(org_label, project_label, filepath, file_id=None):
    """
    This is the POST method, when the user does not provide a file ID.

    :param org_label: The label of the organization that the file belongs to
    :param project_label: The label of the project that the file belongs to
    :param filepath: path of the file to upload
    :param file_id: OPTIONAL Will use this id to identify the file if provided. If not provided, an ID will be generated
    :return: A payload containing only the Nexus metadata for this updated file.
    """

    # the element composing the query URL need to be URL-encoded
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

    path = "/files/" + org_label + "/" + project_label

    file_obj = {"file": open(filepath, "rb")}

    if file_id is None:
        return http_post(path, body=file_obj, data_type="file", use_base=True)
    else:
        file_id = url_encode(file_id)
        path = path + "/" + file_id
        return http_put(path, use_base=True, body=file_obj, data_type="file")
Example #4
0
def create_es(org_label, project_label, view_data, view_id=None):
    """
    Creates an ElasticSearch view

    :param org_label: Label of the organization the view wil belong to
    :param project_label: label of the project the view will belong too
    :param view_data: Mapping data required for ElasticSearch indexing
    :param view_id: OPTIONAL if provided, the view will be created with the given id.
        Otherwise, an autogenerated one will be given by Nexus
    :return: The payload representing the view. This payload only contains the Nexus metadata
    """

    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

    path = "/views/" + org_label + "/" + project_label

    # we give the possibility to use a JSON string instead of a dict
    if (not isinstance(view_data, dict)) and isinstance(view_data, str):
        view_data = json.loads(view_data)

    if "@type" not in view_data:
        view_data["@type"] = ["View", ELASTIC_TYPE, "Alpha"]

    if view_id is None:
        return http_post(path, body=view_data, use_base=True)
    else:
        view_id = url_encode(view_id)
        path = path + "/" + view_id
        return http_put(path, body=view_data, use_base=True)
Example #5
0
def update(org_label: str, project_label: str, filepath: str, file_id: str, rev: int, storage_id: Optional[str] = None,
           filename: Optional[str] = None, content_type: Optional[str] = None) -> Dict:
    """
        Updates an existing file resource with a new binary attachment.

        :param org_label: The label of the organization that the file belongs to
        :param project_label: The label of the project that the file belongs to
        :param filepath: The path of the file to upload
        :param file_id: The id of the file resource to update.
        :param rev: The revision to update from.
        :param storage_id: OPTIONAL The id of the storage backend where the file will be stored.
                           If not provided, the project's default storage is used.
        :param filename: OPTIONAL Overrides the automatically detected filename
        :param content_type: OPTIONAL Overrides the automatically detected content type
        :return: A payload containing only the Nexus metadata for this updated file.
    """

    # the elements composing the query URL need to be URL-encoded
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

    path = [SEGMENT, org_label, project_label, url_encode(file_id)]

    if filename is None:
        filename = filepath.split("/")[-1]

    file_obj = {
        "file": (filename, open(filepath, "rb"), _content_type(filepath, content_type))
    }

    return http_put(path, body=file_obj, data_type="file", rev=rev, storage=storage_id)
Example #6
0
def create(org_label, project_label, schema_obj, schema_id=None):
    """
    Create a new schema

    :param org_label: Label of the organization in which to create the schema
    :param project_label: label of the project in which to create a schema
    :param schema_obj: Schema, can be a dictionary or a JSON string
    :param schema_id: OPTIONAL The view will be created with this specific internal id, if provided.
        Otherwise, an id will be generated by Nexus.
    :return: payload of the schema as a Python dictionary. This payload is partial and contains only Nexus metadata.
        To get the full schema payload, use the fetch() method.
    """

    # we give the possibility to use a JSON string instead of a dict
    if (not isinstance(schema_obj, dict)) and isinstance(schema_obj, str):
        schema_obj = json.loads(schema_obj)

    org_label = url_encode(org_label)
    project_label = url_encode(project_label)
    path = "/schemas/" + org_label + "/" + project_label

    if schema_id is None:
        return http_post(path, schema_obj, use_base=True)
    else:
        schema_id = url_encode(schema_id)
        path = path + "/" + schema_id
        return http_put(path, schema_obj, use_base=True)
Example #7
0
def create(org_label, project_label, data, schema_id="_", resource_id=None):
    """
        Create a resource. If resource_id is provided, this given ID will be used. If resource_id not provided,
        an ID will be automatically generated for this new resource.

        :param org_label: The label of the organization that the resource belongs to
        :param project_label: The label of the project that the resource belongs to
        :param schema_id: OPTIONAL The schema to constrain the data. Can be None for non constrained data (default: "resource)
        :param data: dictionary containing the data to store in this new resource
        :param resource_id: OPTIONAL force the use of a specific id when creating the new resource
        :return: A payload containing only the Nexus metadata for this updated resource.

        If the data does not have a "@context" value, a default one is automatically added.
    """

    # if no schema is provided, we can create a resource with a non-constraining
    # default schema called "resource"
    if schema_id is None:
        schema_id = "resource"

    # the element composing the query URL need to be URL-encoded
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)
    schema_id = url_encode(schema_id)

    path = "/resources/" + org_label + "/" + project_label + "/" + schema_id

    if resource_id is None:
        return http_post(path, data, use_base=True)
    else:
        resource_id = url_encode(resource_id)
        path = path + "/" + resource_id
        return http_put(path, data, use_base=True)
Example #8
0
def update_link_(file: Dict,
                 filename: str,
                 filepath: str,
                 media_type: str,
                 rev: Optional[int] = None,
                 storage_id: Optional[str] = None) -> Dict:
    """
        Update a file (of any kind, not necessarily a link) with a link
        from an existing file resource payload, for instance the value returned
        by nexus.file.fetch(), where some fields where modified, added or removed.

        :param file: Payload of a previously fetched file.
        :param filename: The filename that will be exposed in the resource metadata
        :param filepath: The path (relative to its storage root) of the file to link
        :param media_type: The linked file's media type
        :param rev: The previous file revision
        :param file_id: The previous file resource id
        :param storage_id: OPTIONAL The id of the storage backend where the file is located.
                           If not provided, the project's default storage is used.
        :return: A payload containing only the Nexus metadata for this linked file.
    """

    if rev is None:
        rev = file["_rev"]

    path = file["_self"]

    payload = {"filename": filename, "path": filepath, "mediaType": media_type}

    return http_put(path, body=payload, rev=rev, storage=storage_id)
Example #9
0
def update(org_label: str,
           project_label: str,
           id: str,
           projects: List[str],
           identities: List[Dict],
           priority: int,
           rev: int,
           resource_types: List[str] = None) -> Dict:
    """Update a resolver.

    :param org_label: Label of the organization the resolver belongs to.
    :param project_label: Label of the project the resolver belongs to.
    :param id: ID of the resolver, given as an IRI which is not URL encoded.
    :param projects: List of target projects, given with format ``organization/project``.
    :param identities: List of identities the creator of the resolver has and
        which have the permission ``resources/read`` on the target projects.
    :param priority: Resolution priority.
    :param rev: Last revision of the resolver.
    :param resource_types: (optional) List of types of the resources to resolve,
        given as IRIs.
    :return: The Nexus metadata of the updated resolver.
    """
    encoded_id = encode_url(id)
    payload = _payload(projects, identities, priority, id, resource_types)
    return http_put([SEGMENT, org_label, project_label, encoded_id],
                    payload,
                    rev=rev)
Example #10
0
def create_(path: str, payload: Dict) -> Dict:
    """Create a realm (full path version).

    :param path: Full path of the realm.
    :param payload: Payload of the realm.
    :return: The Nexus metadata of the created realm.
    """
    return http_put(path, payload)
Example #11
0
def replace_(endpoint: str, payload: Dict, rev: int) -> Dict:
    """Replace the user-defined permissions (full path version).

    :param endpoint: Endpoint for permissions.
    :param payload: Payload of user-defined permissions.
    :param rev: Last revision of the permissions.
    :return: The Nexus metadata of the permissions.
    """
    return http_put(endpoint, payload, rev=rev)
Example #12
0
def update_(path: str, payload: Dict, rev: int) -> Dict:
    """Update a resolver (full path version).

    :param path: Full path of the resolver (i.e. includes its ID), URL encoded.
    :param payload: Payload of the resolver.
    :param rev: Last revision of the resolver.
    :return: The Nexus metadata of the updated resolver.
    """
    return http_put(path, payload, rev=rev)
Example #13
0
def replace_(path: str, payload: Dict, rev: int) -> Dict:
    """Replace ACLs on a full path.

    :param path: Full path on which replacing the ACLs.
    :param payload: Payload of the ACLs.
    :param rev: Last revision of the ACLs.
    :return: The Nexus metadata of the ACLs.
    """
    return http_put(path, payload, rev=rev)
Example #14
0
def replace_(path: str, payload: Dict, rev: int) -> Dict:
    """Replace a realm (full path version).

    :param path: Full path of the realm.
    :param payload: Updated payload of the realm.
    :param rev: Last revision of the realm.
    :return: The Nexus metadata of the realm.
    """
    return http_put(path, payload, rev=rev)
Example #15
0
def replace(permissions: List[str], rev: int) -> Dict:
    """Replace the user-defined permissions.

    :param permissions: List of user-defined permissions.
    :param rev: Last revision of the permissions.
    :return: The Nexus metadata of the permissions.
    """
    payload = _payload(permissions)
    return http_put([SEGMENT], payload, rev=rev)
Example #16
0
def update_(org_label: str, project_label: str, payload: Dict, storage_id: str, rev: int) -> Dict:
    """Update storage.

    :param org_label: Label of the organization the storage belongs to.
    :param project_label: Label of the project the storage belongs to.
    :param payload: Payload of the storage
    :param storage_id: (optional) User-defined ID of the storage, given as an IRI which is not URL encoded.
    :param rev: last known revision of the storage
    :return: The Nexus metadata of the updated storage.
    """
    return http_put([SEGMENT, org_label, project_label, url_encode(storage_id)], body=payload, rev=rev)
Example #17
0
def create(subpath: str,
           name: str,
           openid_config: str,
           logo: str = None) -> Dict:
    """Create a realm.

    :param subpath: Subpath of the realm.
    :param name: Name of the realm.
    :param openid_config: URL of the OpenID configuration.
    :param logo: (optional) URL of a logo.
    :return: The Nexus metadata of the created realm.
    """
    payload = _payload(name, openid_config, logo)
    return http_put([SEGMENT, subpath], payload)
Example #18
0
def replace(subpath: str, permissions: List[List[str]], identities: List[Dict],
            rev: int) -> Dict:
    """Replace ACLs on a subpath.

    ``permissions`` and ``identities`` have the same order.

    :param subpath: Subpath on which replacing the ACLs.
    :param permissions: List of list of permissions.
    :param identities: List of identities for which to replace permissions.
    :param rev: Last revision of the ACLs.
    :return: The Nexus metadata of the ACLs.
    """
    payload = _payload(permissions, identities)
    return http_put([SEGMENT, subpath], payload, rev=rev)
Example #19
0
def create_(path: str, payload: Dict, id: str = None) -> Dict:
    """Create a resolver (path version).

    :param path: Full path of the project the resolver belongs to, URL encoded.
    :param payload: Payload of the resolver.
    :param id: (optional) User-defined ID of the resolver, given as an IRI
        which is not URL encoded.
    :return: The Nexus metadata of the created resolver.
    """
    if id is None:
        return http_post(path, payload)
    else:
        encoded_id = encode_url(id)
        p = "{}/{}".format(path, encoded_id)
        return http_put(p, payload)
Example #20
0
def create(org_label: str,
           project_label: str,
           filepath: str,
           storage_id: Optional[str] = None,
           file_id: Optional[str] = None,
           filename: Optional[str] = None,
           content_type: Optional[str] = None) -> Dict:
    """
        Creates a file resource from a binary attachment using the POST method when the user does not provide
        a file ID, PUT otherwise.

        :param org_label: The label of the organization that the file belongs to
        :param project_label: The label of the project that the file belongs to
        :param filepath: path of the file to upload
        :param storage_id: OPTIONAL The id of the storage backend where the file will be stored.
                           If not provided, the project's default storage is used.
        :param file_id: OPTIONAL Will use this id to identify the file if provided.
                        If not provided, an ID will be generated.
        :param filename: OPTIONAL Overrides the automatically detected filename
        :param content_type: OPTIONAL Override the automatically detected content type
        :return: A payload containing only the Nexus metadata for this updated file.
    """

    # the elements composing the query URL need to be URL-encoded
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

    path = [SEGMENT, org_label, project_label]

    if filename is None:
        filename = filepath.split("/")[-1]

    file_obj = {
        "file": (filename, open(filepath,
                                "rb"), _content_type(filepath, content_type))
    }

    if file_id is None:
        return http_post(path,
                         body=file_obj,
                         data_type="file",
                         storage=storage_id)
    else:
        path.append(url_encode(file_id))
        return http_put(path,
                        body=file_obj,
                        data_type="file",
                        storage=storage_id)
Example #21
0
def replace(subpath: str,
            name: str,
            openid_config: str,
            rev: int,
            logo: str = None) -> Dict:
    """Replace a realm.

    :param subpath: Subpath of the realm.
    :param name: Name of the realm.
    :param openid_config: Updated URL of the OpenID configuration.
    :param rev: Last revision of the realm.
    :param logo: (optional) Updated URL of a logo.
    :return: The Nexus metadata of the realm.
    """
    payload = _payload(name, openid_config, logo)
    return http_put([SEGMENT, subpath], payload, rev=rev)
Example #22
0
def update(org, rev=None):
    """
    Update an organization. Only the field "name" can be updated (and "description" in the future).

    :param org: Organization payload as a dictionary. This is most likely the returned value of `organisation.get(...)`
    :param rev: OPTIONAL The last revision number, to make sure the developer is aware of the latest status of
        this organization. If not provided, the `_rev` number from the `org` argument will be used.
    :return: The payload from the Nexus API as a dictionary. This contains the Nexus metadata of the organization
    """
    org_label = url_encode(org["_label"])

    if rev is None:
        rev = org["_rev"]

    path = "/orgs/" + org_label + "?rev=" + str(rev)

    return http_put(path, org, use_base=True)
Example #23
0
def create(org_label, description=None):
    """
    Create a new organization.

    :param org_label: The label of the organization. Does not allow spaces or special characters
    :param description: OPTIONAL The description of the organization
    :return: The payload from the Nexus API as a dictionary. This contains the Nexus metadata of the organization
    """
    org_label = url_encode(org_label)
    path = "/orgs/" + org_label

    data = {}

    if "description" is not None:
        data["description"] = description
    else:
        data["description"] = ""

    return http_put(path, use_base=True, body=data)
Example #24
0
def update(resource, rev=None):
    """
    Update a resource. The resource object is most likely the returned value of a
    nexus.resource.fetch(), where some fields where modified, added or removed.
    Note that the returned payload only contains the Nexus metadata and not the
    complete resource.

    :param resource: payload of a previously fetched resource, with the modification to be updated
    :param rev: OPTIONAL The previous revision you want to update from.
        If not provided, the rev from the resource argument will be used.
    :return: A payload containing only the Nexus metadata for this updated resource.
    """

    if rev is None:
        rev = resource["_rev"]

    path = resource["_self"] + "?rev=" + str(rev)

    return http_put(path, resource, use_base=False)
Example #25
0
def update(project, rev=None):
    """
    Update a project. The data to update on a project are mostly related to the api mapping. To do so, you must
    get the project information as a payload, most likely using `project.fetch(...)`, then, modify this payload
    according to the update to perform, and finally, use this modified payload as the `project` argument of this method.

    :param project: Project payload as a dictionary. This is most likely the returned value of `project.fetch(...)`
    :param rev: OPTIONAL The last revision number, to make sure the developer is aware of the latest status of
        this project. If not provided, the `_rev` number from the `project` argument will be used.
    :return: The payload from the Nexus API as a dictionary. This contains the Nexus metadata of the project
    """

    if rev is None:
        rev = project["_rev"]

    org_label = url_encode(project["_organizationLabel"])
    project_label = url_encode(project["_label"])

    path = "/projects/" + org_label + "/" + project_label + "?rev=" + str(rev)

    return http_put(path, project, use_base=True)
Example #26
0
def update(file, filepath, rev=None):
    """
    Update a file. The file object is most likely the returned value of a
    nexus.file.fetch(), where some fields where modified, added or removed.
    Note that the returned payload only contains the Nexus metadata and not the
    complete file.

    :param file: payload of a previously fetched file, with the modification to be updated
    :param rev: OPTIONAL The previous revision you want to update from.
        If not provided, the rev from the file argument will be used.
    :return: A payload containing only the Nexus metadata for this updated file.
    """

    if rev is None:
        rev = file["_rev"]

    path = file["_self"] + "?rev=" + str(rev)

    file_obj = {"file": open(filepath, "rb")}

    return http_put(path, body=file_obj, use_base=False, data_type="file")
Example #27
0
def create(org_label,
           project_label,
           description=None,
           api_mappings=None,
           vocab=None,
           base=None):
    """
    Create a new project under an organization.

    :param org_label: The label of the organization to create the project in
    :param project_label: The label of the project to add
    :param description: OPTIONAL a description for this project
    :param api_mappings: OPTIONAL apiMappings
        see https://bluebrain.github.io/nexus/docs/api/admin/admin-projects-api.html#api-mappings
    :param vocab: OPTIONAL vocab as a string
    :param base: OPTIONAL base for the project
    :return: The payload from the Nexus API as a dictionary. This contains the Nexus metadata of the project
    """

    org_label = url_encode(org_label)
    project_label = url_encode(project_label)
    path = "/projects/" + org_label + "/" + project_label

    config = {}

    if description is not None:
        config["description"] = description

    if api_mappings is not None:
        config["apiMappings"] = api_mappings

    if vocab is not None:
        config["vocab"] = vocab

    if base is not None:
        config["base"] = base

    return http_put(path, use_base=True, body=config)
Example #28
0
def create_link(org_label: str,
                project_label: str,
                filename: str,
                filepath: str,
                media_type: str,
                storage_id: Optional[str] = None,
                file_id: Optional[str] = None) -> Dict:
    """
        Creates a file resource from a link to an existing binary using the POST method when the user does not provide
        a file ID, PUT otherwise.

        :param org_label: The label of the organization that the file belongs to
        :param project_label: The label of the project that the file belongs to
        :param filename: The filename that will be exposed in the resource metadata
        :param filepath: The path (relative to its storage root) of the file to link
        :param media_type: The media type of the linked file
        :param storage_id: OPTIONAL The id of the storage backend where the file is located.
                           If not provided, the project's default storage is used.
        :param file_id: OPTIONAL The id of the created resource if provided.
                        If not, an ID will be generated
        :return: A payload containing only the Nexus metadata for this linked file.
    """

    # the elements composing the query URL need to be URL-encoded
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

    payload = {"filename": filename, "path": filepath, "mediaType": media_type}

    request_path = [SEGMENT, org_label, project_label]

    if file_id is not None:
        request_path.append(url_encode(file_id))

    if file_id is None:
        return http_post(request_path, body=payload, storage=storage_id)
    else:
        return http_put(request_path, body=payload, storage=storage_id)
Example #29
0
def tag_es(esview, tag_value, rev_to_tag=None, rev=None):
    """
    Add a tag to a a specific revision of an ElasticSearch view. Note that a new revision (untagged) will be created.

    :param esview: payload of a previously fetched view (ElasticSearch)
    :param tag_value: The value (or name) of a tag
    :param rev_to_tag: OPTIONAL Number of the revision to tag. If not provided, this will take the revision number
        from the provided resource payload.
    :param rev: OPTIONAL The previous revision you want to update from.
        If not provided, the rev from the resource argument will be used.
    :return: A payload containing only the Nexus metadata for this view.
    """

    if rev is None:
        rev = esview["_rev"]

    if rev_to_tag is None:
        rev_to_tag = esview["_rev"]

    path = esview["_self"] + "/tags?rev=" + str(rev)

    data = {"tag": tag_value, "rev": rev_to_tag}

    return http_put(path, body=data, use_base=False)
Example #30
0
def aggregate_es(org_label, project_label, esviews, id):
    """
    Creates an aggregated view for ElasticSearch.

    :param org_label: Label of the organization the view wil belong to
    :param project_label: label of the project the view will belong too
    :param esviews: list of ElasticSearch view payloads, most likely got with .fetch()
    :param id: id to give to this aggregation id ElasticSearch views
    :return: A payload containing only the Nexus metadata for this aggregated view.
    """

    org_label = url_encode(org_label)
    project_label = url_encode(project_label)
    id = url_encode(id)

    path = "/views/" + org_label + "/" + project_label + "/" + id

    views_data = []

    for v in esviews:
        v_data = {
            "project": "/".join(v["_project"].split("/")[-2:]),
            "viewId": v["@id"]
        }

        views_data.append(v_data)

    data = {
        "@context": {
            "nxv": "https://bluebrain.github.io/nexus/vocabulary/"
        },
        "@type": ["View", "AggregateElasticView", "Alpha"],
        "views": views_data
    }

    return http_put(path, body=data, use_base=True)