Ejemplo n.º 1
0
def sub_api(action, manager="", package="", *args, **kwargs) -> Union[bool, str]:
    url_end_list = [
        "https://libraries.io/api/subscriptions"
    ]  # start of list to build url
    more_args = []  # for unpacking args
    url_combined = ""  # final string url
    kind = "get"  # get, post, put or delete

    if action == "list_subscribed":
        url_combined = "/".join(url_end_list)
        resp = make_request(url_combined, kind)
        return resp

    assert manager and package, "this operation requires manager and package definition"

    url_end_list += [manager, package]
    url_combined = "/".join(url_end_list)

    if action == "check_subscribed":
        resp = make_request(url_combined, kind)
        return resp is not None
    if action == "subscribe":
        extract("include_prerelease").of(kwargs).then(url_end_list.append)
        kind = "post"
        make_request(url_combined, kind)
        return "Successfully Subscribed"

    if action == "update_subscribe":
        kind = "put"
        # not implemented - seems libraries.io api has bug
        # if implemented in future, adjust modules in readme
        make_request(url_combined, kind)
        return "include_prerelease is always set to true"

    if action == "delete_subscribe":
        kind = "delete"

        # first check if subscribed. Must be done before build url.
        is_subscribed = sub_api(
            "check_subscribed", manager=manager, package=package
        )

        if is_subscribed:
            make_request(url_combined, kind)
            msg = "Successfully Unsubscribed"
        else:
            msg = f"Unsubscribe unnecessary. You are not subscribed to {package}."
        return msg
Ejemplo n.º 2
0
def search_api(action, *args, filters=None, sort=None, **kwargs):
    """
    build and call for search 

    Args:
        action (str): function action name
        filters (dict): filters passed to requests Session
        sort (str): to sort by. Options
        *args (str): positional arguments
        **kwargs (str): keyword arguments
    Returns:
        (list): list of dicts response from libraries.io.
            according to page and per page 
        Many are dicts or list of dicts.
    """

    kind = "get"
    url_end_list = handle_path_params(action, *args, **kwargs)
    handle_query_params(filters, kwargs, sort)
    url_combined = "/".join(url_end_list)
    return make_request(url_combined, kind)
Ejemplo n.º 3
0
def search_api(action, *args, **kwargs):
    """
    build and call for search

    Args:
        action (str): function action name
        *args (str): positional arguments
        **kwargs (str): keyword arguments
    Returns:
        (list): list of dicts response from libraries.io.
            according to page and per page
        Many are dicts or list of dicts.
    """

    kind = "get"

    url_end_list = handle_path_params(action, *args, **kwargs)

    handle_query_params(action, **kwargs)
    url_combined = "/".join(url_end_list)
    return make_request(url_combined, kind)
Ejemplo n.º 4
0
def search_api(action, *args, **kwargs):
    """
    build and call for search 

    Args:
        action (str): function action name
        filters (list): list of strings 
        sort (str): to sort by. Options
        *args (str): positional arguments
        **kwargs (str): keyword arguments
    Returns:
        (list): list of dicts response from libraries.io.
            according to page and per page 
        Many are dicts or list of dicts.
    """

    url_end_list = ["https://libraries.io/api"]  # start of list to build url
    more_args = []  # for unpacking args
    url_combined = ""  # final string url
    kind = "get"  # type of request

    if action == "special_project_search":
        url_end_list.append("search?")

        # package seems to be ignored by the libraries.io API
        # - bug in docs or their api
        # if "package" in kwargs:
        #    url_end_list.append(kwargs["package"])

        if kwargs:
            if "filters" in kwargs:
                filts = dict(kwargs["filters"])
                if "manager" in filts:
                    filts["platforms"] = filts.pop("manager")
                sess.params = {**sess.params, **filts}
            if "sort" in kwargs:
                sess.params["sort"] = kwargs["sort"]

        url_combined = "/".join(url_end_list)
        response = make_request(url_combined, kind)

        clear_params()
        return response

    if action == "platforms":
        url_end_list.append("platforms")

    if "pproject" in action:
        if kwargs:
            if "manager" in kwargs:
                url_end_list.append(kwargs["manager"])
            if "package" in kwargs:
                url_end_list.append(kwargs["package"])
        if args:
            more_args = [arg for arg in args]
            url_end_list = url_end_list + more_args

        if action == "pproject_dependencies":
            url_end_list.append(
                "latest")  # could make option to subscribe to other versions
            url_end_list.append("dependencies")

        if action == "pproject_dependents":
            url_end_list.append("dependents")

        if action == "pproject_dependent_repositories":
            url_end_list.append("dependent_repositories")

        if action == "pproject_contributors":
            url_end_list.append("contributors")

        if action == "pproject_sourcerank":
            url_end_list.append("sourcerank")

        if action == "pproject_usage":
            url_end_list.append("usage")

    if "repository" in action:
        if kwargs:
            if "host" in kwargs:
                url_end_list.append(kwargs["host"])
            if "owner" in kwargs:
                url_end_list.append(kwargs["owner"])
            if "repo" in kwargs:
                url_end_list.append(kwargs["repo"])
        if args:
            more_args = [arg for arg in args]
            url_end_list = url_end_list + more_args

        if action == "repository_dependencies":
            url_end_list.append("dependencies")

        if action == "repository_projects":
            url_end_list.append("projects")

    if "user" in action:
        if kwargs:
            if "host" in kwargs:
                url_end_list.append(kwargs["host"])
            if "user" in kwargs:
                url_end_list.append(kwargs["user"])
        if args:
            more_args = [arg for arg in args]
            url_end_list = url_end_list + more_args
            print(url_end_list)

        if action == "user_repositories":
            url_end_list.append("repositories")

        if action == "user_packages":
            url_end_list.append("projects")

        if action == "user_packages_contributions":
            url_end_list.append("project-contributions")

        if action == "user_repositories_contributions":
            url_end_list.append("repository-contributions")

        if action == "user_dependencies":
            url_end_list.append("dependencies")

    url_combined = "/".join(url_end_list)
    url_end_list = []
    response = make_request(url_combined, kind)
    return response
Ejemplo n.º 5
0
def sub_api(action, *args, **kwargs):
    url_end_list = ["https://libraries.io/api/subscriptions"
                    ]  # start of list to build url
    more_args = []  # for unpacking args
    url_combined = ""  # final string url
    call_type = ""  # post, put or delete
    manager = ""
    package = ""
    kind = "get"

    if kwargs:
        if "manager" in kwargs:
            manager = kwargs["manager"]
        if "package" in kwargs:
            package = kwargs["package"]

    if args:
        if args[0]:
            manager = args[0]
        if args[1]:
            package = args[1]

    if action == "list_subscribed":
        url_combined = "/".join(url_end_list)
        resp = make_request(url_combined, kind)
        return resp

    if action == "check_subscribed":
        # do we need to check for args?
        url_end_list.append(manager)
        url_end_list.append(package)
        url_combined = "/".join(url_end_list)

        check = make_request(url_combined, kind)

        if check is not None:
            r_bool = True
        else:
            r_bool = False

        return r_bool

    if action == "subscribe":
        kind = "post"
        url_end_list.append(manager)
        url_end_list.append(package)

        url_combined = "/".join(url_end_list)
        return make_request(url_combined, kind)

    if action == "update_subscribe":
        kind = "put"
        # not implemented - seems libraries.io api has bug
        # if implemented in future, adjust modules in readme
        url_end_list.append(manager)
        url_end_list.append(package)

        url_combined = "/".join(url_end_list)
        return make_request(url_combined, kind)

    if action == "delete_subscribe":
        kind = "delete"
        url_end_list.append(manager)
        url_end_list.append(package)
        url_combined = "/".join(url_end_list)

        # first check if subscribed. Must be done before build url.
        check_pkg_subscribed = sub_api("check_subscribed",
                                       manager=manager,
                                       package=package)

        if check_pkg_subscribed is False:
            msg = f"Unsubscribe unnecessary. You are not subscribed to {package}."
            return msg
        else:
            return make_request(url_combined, kind)

        # prerelease option ccurrently not changeable for subscribe or update
        # (perhaps due to libraries.io api glitch)
        def __check_prerelease(*args, **kwargs):
            prerelease = kwargs.pop("include_prerelease", "")
            return prerelease