Пример #1
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")
Пример #2
0
def tag_(file: Dict,
         tag_value: str,
         rev_to_tag: Optional[int] = None,
         rev: Optional[int] = None) -> Dict:
    """
        Tag a specific revision of the file. Note that a new revision (untagged) will be created

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

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

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

    path = file["_self"] + "/tags"

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

    return http_post(path, body=payload, rev=rev)
Пример #3
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)
Пример #4
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)
Пример #5
0
def tag(resource, tag_value, rev_to_tag=None, rev=None):
    """
    Add a tag to a a specific revision of the resource. Note that a new revision (untagged) will be created

    :param resource: payload of a previously fetched resource
    :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 resource.
    """

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

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

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

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

    return http_post(path, body=data, use_base=False)
Пример #6
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)
Пример #7
0
def tag_(path: str, payload: Dict, rev: int) -> Dict:
    """Tag a revision of a resolver (full path version).

    :param path: Full path of the resolver (i.e. includes its ID), URL encoded.
    :param payload: Payload of the tag.
    :param rev: Last revision of the resolver.
    :return: The Nexus metadata of the tagged resolver.
    """
    p = "{}/tags".format(path)
    return http_post(p, payload, rev=rev)
Пример #8
0
def create_(org_label: str, project_label: str, payload: Dict, storage_id: Optional[str]) -> Dict:
    """Create 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.
    :return: The Nexus metadata of the created storage.
    """
    if storage_id is not None:
        payload["@id"] = storage_id

    return http_post([SEGMENT, org_label, project_label], body=payload)
Пример #9
0
def query_sparql(org_label, project_label, query):
    """
    Perform a SparQL query.

    :param org_label: Label of the organization to perform the query on
    :param project_label: Label of the project to perform the query on
    :param query: Query as a string
    :return: result of the query as a dictionary
    """
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

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

    return http_post(path, body=query, data_type="sparql", use_base=True)
Пример #10
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)
Пример #11
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)
Пример #12
0
def tag(org_label: str, project_label: str, storage_id: str, tag: str, rev_to_tag: str, rev: int) -> Dict:
    """Tag a storage

    :param org_label: Label of the organization the storage belongs to.
    :param project_label: Label of the project the storage belongs to.
    :param storage_id: the storage ID
    :param tag: tag label
    :param rev_to_tag: revision to tag
    :param rev: last known revision of the storage
    :return: The Nexus metadata of the updated storage.
    """
    payload = {
        "tag": tag,
        "rev": rev_to_tag,
    }
    return http_post([SEGMENT, org_label, project_label, url_encode(storage_id), "tags"], payload, rev=rev)
Пример #13
0
def tag(org_label: str, project_label: str, file_id: str, rev_to_tag: int,
        tag_value: str, rev: int) -> Dict:
    """
        Tag a specific revision of the file. Note that a new revision (untagged) will be created

        :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 file_id: The identifier of the file to tag.

        :param rev: The last revision of the file.
        :return: A payload containing only the Nexus metadata for this file.
    """
    path = [SEGMENT, org_label, project_label, url_encode(file_id), "tags"]

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

    return http_post(path, payload, rev=rev)
Пример #14
0
def create_(org_label: str, project_label: str, payload: Dict,
            view_id: Optional[str]):
    """Create a view from a given payload.

    :param org_label: Label of the organization the view belongs to.
    :param project_label: Label of the project the view belongs to.
    :param payload: JSON payload oft he view
    :param view_id: (optional) User-defined ID of the view, given as an IRI
        which is not URL encoded.
    :return: The Nexus metadata of the created view.
    """
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

    path = "/views/" + org_label + "/" + project_label
    if view_id is not None:
        payload["@id"] = view_id

    return http_post(path, body=payload, use_base=True)
Пример #15
0
def query_es(org_label, project_label, query, view_id="documents"):
    """
    Perform a ElasticSearch query.

    :param org_label: Label of the organization to perform the query on
    :param project_label: Label of the project to perform the query on
    :param view_id: id of an ElasticSearch view
    :param query: ElasticSearch query as a JSON string or a dictionary
    :return: the result of the query as a dictionary
    """
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)
    view_id = url_encode(view_id)

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

    if (not isinstance(query, dict)) and isinstance(query, str):
        query = json.loads(query)

    return http_post(path, body=query, use_base=True)
Пример #16
0
def query_sparql(org_label: str,
                 project_label: str,
                 query: str,
                 view_id: str = "nxv:defaultSparqlIndex") -> Dict:
    """
    Perform a SparQL query.

    :param org_label: Label of the organization to perform the query on
    :param project_label: Label of the project to perform the query on
    :param query: Sparql query
    :param view_id: id of a Sparql view
    :return: result of the query as a dictionary
    """
    org_label = url_encode(org_label)
    project_label = url_encode(project_label)

    path = "/views/" + org_label + "/" + project_label + "/" + url_encode(
        view_id) + "/sparql"

    return http_post(path, body=query, data_type="sparql", use_base=True)
Пример #17
0
def tag(org_label: str, project_label: str, id: str, tag: str, rev_to_tag: str,
        rev: int) -> Dict:
    """Tag a revision of 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 tag: Tag to set.
    :param rev_to_tag: Revision number to tag.
    :param rev: Last revision of the resolver.
    :return: The Nexus metadata of the tagged resolver.
    """
    encoded_id = encode_url(id)
    payload = {
        "tag": tag,
        "rev": rev_to_tag,
    }
    return http_post([SEGMENT, org_label, project_label, encoded_id, "tags"],
                     payload,
                     rev=rev)
Пример #18
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)
Пример #19
0
def create(org_label: str,
           project_label: str,
           projects: List[str],
           identities: List[Dict],
           priority: int,
           id: str = None,
           resource_types: List[str] = None) -> Dict:
    """Create a cross-project resolver.

    :param org_label: Label of the organization the resolver belongs to.
    :param project_label: Label of the project the resolver belongs to.
    :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 id: (optional) User-defined ID of the resolver, given as an IRI
        which is not URL encoded.
    :param resource_types: (optional) List of types of the resources to resolve,
        given as IRIs.
    :return: The Nexus metadata of the created resolver.
    """
    payload = _payload(projects, identities, priority, id, resource_types)
    return http_post([SEGMENT, org_label, project_label], payload)