From 92a5620abf67051828d38060841a49d6d3c5ef60 Mon Sep 17 00:00:00 2001 From: Tyrel Souza <923113+tyrelsouza@users.noreply.github.com> Date: Wed, 3 Nov 2021 22:26:39 -0400 Subject: [PATCH] initial commit --- .gitignore | 2 ++ cli.py | 48 ++++++++++++++++++++++++++++ requirements.in | 6 ++++ requirements.txt | 83 ++++++++++++++++++++++++++++++++++++++++++++++++ tasks.py | 12 +++++++ tests.py | 29 +++++++++++++++++ tests/repo.json | 1 + tests/user.json | 1 + 8 files changed, 182 insertions(+) create mode 100644 .gitignore create mode 100644 cli.py create mode 100644 requirements.in create mode 100644 requirements.txt create mode 100644 tasks.py create mode 100644 tests.py create mode 100644 tests/repo.json create mode 100644 tests/user.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..06a47c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +env/ +__pycache__/ diff --git a/cli.py b/cli.py new file mode 100644 index 0000000..8437002 --- /dev/null +++ b/cli.py @@ -0,0 +1,48 @@ +import httpx +from pprint import pprint + + +class GHub: + headers = {"Accept": "application/vnd.github.v3+json"} + + def __init__(self, user_name: str, repo_name: str): + self.user_name = user_name + self.repo_name = repo_name + + def get(self, url: str) -> dict: + """Uses httpx.Client().get to send the appropriate github headers, and return the url""" + with httpx.Client(headers=self.headers) as client: + return client.get(url).json() + + def schema(self) -> dict: + """Show all the schema that are available""" + return self.get("https://api.github.com") + + def set_user(self): + self.user = self.get_user() + + def get_user(self) -> dict: + """Get the user object""" + return self.get(f"https://api.github.com/users/{self.user_name}") + + def set_repo(self): + self.repo = self.get_repo() + + def get_repo(self) -> dict: + """Get the repo object""" + return self.get(f"https://api.github.com/repos/{self.user_name}/{self.repo_name}") + + +def main(): + gh = GHub(user_name="mkb79", repo_name="audible-cli") + # pprint(gh.schema()) + pprint( + ( + gh.get_user(), + gh.get_repo(), + ) + ) + + +if __name__ == "__main__": + main() diff --git a/requirements.in b/requirements.in new file mode 100644 index 0000000..af96c94 --- /dev/null +++ b/requirements.in @@ -0,0 +1,6 @@ +httpx +pip-tools +black +invoke +pytest +pytest-httpx diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..000a6d8 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,83 @@ +# +# This file is autogenerated by pip-compile with python 3.9 +# To update, run: +# +# pip-compile +# +anyio==3.3.4 + # via httpcore +attrs==21.2.0 + # via pytest +black==21.10b0 + # via -r requirements.in +certifi==2021.10.8 + # via httpx +charset-normalizer==2.0.7 + # via httpx +click==8.0.3 + # via + # black + # pip-tools +h11==0.12.0 + # via httpcore +httpcore==0.13.7 + # via httpx +httpx==0.20.0 + # via + # -r requirements.in + # pytest-httpx +idna==3.3 + # via + # anyio + # rfc3986 +iniconfig==1.1.1 + # via pytest +invoke==1.6.0 + # via -r requirements.in +mypy-extensions==0.4.3 + # via black +packaging==21.2 + # via pytest +pathspec==0.9.0 + # via black +pep517==0.12.0 + # via pip-tools +pip-tools==6.4.0 + # via -r requirements.in +platformdirs==2.4.0 + # via black +pluggy==1.0.0 + # via pytest +py==1.10.0 + # via pytest +pyparsing==2.4.7 + # via packaging +pytest==6.2.5 + # via + # -r requirements.in + # pytest-httpx +pytest-httpx==0.14.0 + # via -r requirements.in +regex==2021.11.2 + # via black +rfc3986[idna2008]==1.5.0 + # via httpx +sniffio==1.2.0 + # via + # anyio + # httpcore + # httpx +toml==0.10.2 + # via pytest +tomli==1.2.2 + # via + # black + # pep517 +typing-extensions==3.10.0.2 + # via black +wheel==0.37.0 + # via pip-tools + +# The following packages are considered to be unsafe in a requirements file: +# pip +# setuptools diff --git a/tasks.py b/tasks.py new file mode 100644 index 0000000..f4c5661 --- /dev/null +++ b/tasks.py @@ -0,0 +1,12 @@ +from invoke import task + +@task +def test(c): + c.run("pytest tests.py --pdb") + +@task +def black(c): + """ + Runs The uncompromising Python code formatter on specific python files. + """ + c.run("black *.py") diff --git a/tests.py b/tests.py new file mode 100644 index 0000000..4988292 --- /dev/null +++ b/tests.py @@ -0,0 +1,29 @@ +import json +import pytest +from cli import GHub +from pytest_httpx import HTTPXMock + +def _load_user(): + with open("tests/user.json", "r") as f: + return json.loads(f.read()) + +def _load_repo(): + with open("tests/repo.json", "r") as f: + return json.loads(f.read()) + +def test_user(httpx_mock: HTTPXMock): + httpx_mock.add_response(method="GET", json=_load_user()) + + gh = GHub(user_name="tyrelsouza", repo_name="work_sample") + gh.set_user() + assert(gh.user["login"] == "tyrelsouza") + +def test_repo(httpx_mock: HTTPXMock): + httpx_mock.add_response(method="GET", json=_load_repo()) + + gh = GHub(user_name="tyrelsouza", repo_name="work_sample") + gh.set_repo() + breakpoint() + assert(gh.repo["login"] == "tyrelsouza2") + + diff --git a/tests/repo.json b/tests/repo.json new file mode 100644 index 0000000..6aa2456 --- /dev/null +++ b/tests/repo.json @@ -0,0 +1 @@ +{"id": 228900091, "node_id": "MDEwOlJlcG9zaXRvcnkyMjg5MDAwOTE=", "name": "work_sample", "full_name": "tyrelsouza/work_sample", "private": false, "owner": {"login": "tyrelsouza", "id": 923113, "node_id": "MDQ6VXNlcjkyMzExMw==", "avatar_url": "https://avatars.githubusercontent.com/u/923113?v=4", "gravatar_id": "", "url": "https://api.github.com/users/tyrelsouza", "html_url": "https://github.com/tyrelsouza", "followers_url": "https://api.github.com/users/tyrelsouza/followers", "following_url": "https://api.github.com/users/tyrelsouza/following{/other_user}", "gists_url": "https://api.github.com/users/tyrelsouza/gists{/gist_id}", "starred_url": "https://api.github.com/users/tyrelsouza/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/tyrelsouza/subscriptions", "organizations_url": "https://api.github.com/users/tyrelsouza/orgs", "repos_url": "https://api.github.com/users/tyrelsouza/repos", "events_url": "https://api.github.com/users/tyrelsouza/events{/privacy}", "received_events_url": "https://api.github.com/users/tyrelsouza/received_events", "type": "User", "site_admin": false}, "html_url": "https://github.com/tyrelsouza/work_sample", "description": "i need a public repo for work.", "fork": false, "url": "https://api.github.com/repos/tyrelsouza/work_sample", "forks_url": "https://api.github.com/repos/tyrelsouza/work_sample/forks", "keys_url": "https://api.github.com/repos/tyrelsouza/work_sample/keys{/key_id}", "collaborators_url": "https://api.github.com/repos/tyrelsouza/work_sample/collaborators{/collaborator}", "teams_url": "https://api.github.com/repos/tyrelsouza/work_sample/teams", "hooks_url": "https://api.github.com/repos/tyrelsouza/work_sample/hooks", "issue_events_url": "https://api.github.com/repos/tyrelsouza/work_sample/issues/events{/number}", "events_url": "https://api.github.com/repos/tyrelsouza/work_sample/events", "assignees_url": "https://api.github.com/repos/tyrelsouza/work_sample/assignees{/user}", "branches_url": "https://api.github.com/repos/tyrelsouza/work_sample/branches{/branch}", "tags_url": "https://api.github.com/repos/tyrelsouza/work_sample/tags", "blobs_url": "https://api.github.com/repos/tyrelsouza/work_sample/git/blobs{/sha}", "git_tags_url": "https://api.github.com/repos/tyrelsouza/work_sample/git/tags{/sha}", "git_refs_url": "https://api.github.com/repos/tyrelsouza/work_sample/git/refs{/sha}", "trees_url": "https://api.github.com/repos/tyrelsouza/work_sample/git/trees{/sha}", "statuses_url": "https://api.github.com/repos/tyrelsouza/work_sample/statuses/{sha}", "languages_url": "https://api.github.com/repos/tyrelsouza/work_sample/languages", "stargazers_url": "https://api.github.com/repos/tyrelsouza/work_sample/stargazers", "contributors_url": "https://api.github.com/repos/tyrelsouza/work_sample/contributors", "subscribers_url": "https://api.github.com/repos/tyrelsouza/work_sample/subscribers", "subscription_url": "https://api.github.com/repos/tyrelsouza/work_sample/subscription", "commits_url": "https://api.github.com/repos/tyrelsouza/work_sample/commits{/sha}", "git_commits_url": "https://api.github.com/repos/tyrelsouza/work_sample/git/commits{/sha}", "comments_url": "https://api.github.com/repos/tyrelsouza/work_sample/comments{/number}", "issue_comment_url": "https://api.github.com/repos/tyrelsouza/work_sample/issues/comments{/number}", "contents_url": "https://api.github.com/repos/tyrelsouza/work_sample/contents/{+path}", "compare_url": "https://api.github.com/repos/tyrelsouza/work_sample/compare/{base}...{head}", "merges_url": "https://api.github.com/repos/tyrelsouza/work_sample/merges", "archive_url": "https://api.github.com/repos/tyrelsouza/work_sample/{archive_format}{/ref}", "downloads_url": "https://api.github.com/repos/tyrelsouza/work_sample/downloads", "issues_url": "https://api.github.com/repos/tyrelsouza/work_sample/issues{/number}", "pulls_url": "https://api.github.com/repos/tyrelsouza/work_sample/pulls{/number}", "milestones_url": "https://api.github.com/repos/tyrelsouza/work_sample/milestones{/number}", "notifications_url": "https://api.github.com/repos/tyrelsouza/work_sample/notifications{?since,all,participating}", "labels_url": "https://api.github.com/repos/tyrelsouza/work_sample/labels{/name}", "releases_url": "https://api.github.com/repos/tyrelsouza/work_sample/releases{/id}", "deployments_url": "https://api.github.com/repos/tyrelsouza/work_sample/deployments", "created_at": "2019-12-18T18:36:33Z", "updated_at": "2019-12-18T18:37:38Z", "pushed_at": "2019-12-18T18:37:36Z", "git_url": "git://github.com/tyrelsouza/work_sample.git", "ssh_url": "git@github.com:tyrelsouza/work_sample.git", "clone_url": "https://github.com/tyrelsouza/work_sample.git", "svn_url": "https://github.com/tyrelsouza/work_sample", "homepage": null, "size": 3, "stargazers_count": 0, "watchers_count": 0, "language": "Python", "has_issues": true, "has_projects": true, "has_downloads": true, "has_wiki": true, "has_pages": false, "forks_count": 0, "mirror_url": null, "archived": false, "disabled": false, "open_issues_count": 1, "license": {"key": "mit", "name": "MIT License", "spdx_id": "MIT", "url": "https://api.github.com/licenses/mit", "node_id": "MDc6TGljZW5zZTEz"}, "allow_forking": true, "is_template": false, "topics": [], "visibility": "public", "forks": 0, "open_issues": 1, "watchers": 0, "default_branch": "master", "temp_clone_token": null, "network_count": 0, "subscribers_count": 1} diff --git a/tests/user.json b/tests/user.json new file mode 100644 index 0000000..228ad6a --- /dev/null +++ b/tests/user.json @@ -0,0 +1 @@ +{"login": "tyrelsouza", "id": 923113, "node_id": "MDQ6VXNlcjkyMzExMw==", "avatar_url": "https://avatars.githubusercontent.com/u/923113?v=4", "gravatar_id": "", "url": "https://api.github.com/users/tyrelsouza", "html_url": "https://github.com/tyrelsouza", "followers_url": "https://api.github.com/users/tyrelsouza/followers", "following_url": "https://api.github.com/users/tyrelsouza/following{/other_user}", "gists_url": "https://api.github.com/users/tyrelsouza/gists{/gist_id}", "starred_url": "https://api.github.com/users/tyrelsouza/starred{/owner}{/repo}", "subscriptions_url": "https://api.github.com/users/tyrelsouza/subscriptions", "organizations_url": "https://api.github.com/users/tyrelsouza/orgs", "repos_url": "https://api.github.com/users/tyrelsouza/repos", "events_url": "https://api.github.com/users/tyrelsouza/events{/privacy}", "received_events_url": "https://api.github.com/users/tyrelsouza/received_events", "type": "User", "site_admin": false, "name": "Tyrel Souza", "company": "@tidelift @librariesio", "blog": "https://tidelift.com", "location": "Chapel Hill, NC", "email": null, "hireable": null, "bio": "I have a job. I write code for it. Any sponsorships are my own, and not endorsed by my employer. I disagree with Badges and Achievements.", "twitter_username": "tyrelsouza", "public_repos": 10, "public_gists": 34, "followers": 43, "following": 30, "created_at": "2011-07-18T15:04:40Z", "updated_at": "2021-10-12T05:33:35Z"}