Esempio n. 1
0
def test_set_pages_big_value(clear_sess_params):
    """fix_pages returns false for large page argument"""
    clear_params()

    assert fix_pages(page=1000)
    assert sess.params["per_page"] == 30
    assert sess.params["page"] == 1000
Esempio n. 2
0
def test_set_valid_page(clear_sess_params):
    """call to fix_pages returns a string"""
    clear_params()

    assert fix_pages(page=1)
    assert sess.params["per_page"] == 30
    assert sess.params["page"] == 1
Esempio n. 3
0
def test_set_per_pages_small_value(clear_sess_params):
    """fix_pages returns false for small per_page argument"""
    clear_params()

    assert not fix_pages(per_page=0)
    assert sess.params["per_page"] == 1
    assert sess.params["page"] == 1
Esempio n. 4
0
def make_request(url: str, kind: str) -> str:
    """ call api server 

        Args:
            url (str): base url to call
            kind (str): get, post, put, or delete
        Returns:
            json encoded response from libraries.io
    """

    try:
        params = {"include_prerelease": "False"} if kind == "post" else {}
        fix_pages()  # Must be called before any request for page validation
        r = getattr(sess, kind)(url, params=params)
        r.raise_for_status()
        return r.json()
    except HTTPError as http_err:
        print(f"HTTP error occurred: {http_err}")
    except Exception as err:
        print(f"Other error occurred: {err}")
    finally:
        clear_params()
Esempio n. 5
0
def make_request(url: str, kind: str) -> Any:
    """ call api server 

        Args:
            url (str): base url to call
            kind (str): get, post, put, or delete
        Returns:
            response from libraries.io
    
    """

    try:
        if kind == "get":
            r = sess.get(url)
            r.raise_for_status()
            r_val = r.json()
        if kind == "post":
            r = sess.post(url, params={"include_prerelease": "False"})
            r.raise_for_status()
            r_val = "successfully subscribed"
            x = r.json()
        if kind == "put":
            r = sess.put(url)
            r.raise_for_status()
            r_val = "include_prerelease is always set to true"
        if kind == "delete":
            r = sess.delete(url)
            r.raise_for_status()
            r_val = "successfully unsubscribed"

        clear_params()
        return r_val

    except HTTPError as http_err:
        clear_params()
        print(f"HTTP error occurred: {http_err}")
    except Exception as err:
        clear_params()
        print(f"Other error occurred: {err}")
Esempio n. 6
0
def clear_sess_params():
    clear_params()
    yield
Esempio n. 7
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