From 7ab78478e9d0b52c2f848baa720a8b2d27adf298 Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 08:52:31 -0300 Subject: [PATCH 01/15] fixes #3944 authentication error --- web_programming/fetch_github_info.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index 227598bb20ab..cd3a47fe3a34 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -8,19 +8,24 @@ for more information around suggested workarounds and removal dates. """ +""" +Created by lawric1 on 24/11/20 + +To generate your personal access token visit https://github.com/settings/tokens +""" import requests _GITHUB_API = "https://api.github.com/user" -def fetch_github_info(auth_user: str, auth_pass: str) -> dict: +def fetch_github_info(auth_user: str, auth_token: str) -> dict: """ Fetch GitHub info of a user using the requests module """ - return requests.get(_GITHUB_API, auth=(auth_user, auth_pass)).json() + return requests.get(_GITHUB_API, auth=(auth_user, auth_token)).json() if __name__ == "__main__": - for key, value in fetch_github_info("", "").items(): + for key, value in fetch_github_info("", "").items(): print(f"{key}: {value}") From 6b11a5adb9e68c9cdd4bd8520ac878ffface777e Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 09:55:51 -0300 Subject: [PATCH 02/15] Fixes: #3944 authentication error --- web_programming/fetch_github_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index cd3a47fe3a34..86f9d45c0ff0 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -1,4 +1,5 @@ #!/usr/bin/env python3 +import requests """ Created by sarathkaul on 14/11/19 @@ -14,7 +15,6 @@ To generate your personal access token visit https://github.com/settings/tokens """ -import requests _GITHUB_API = "https://api.github.com/user" From 7b1433474ae969d9359bee7cf05cb06767639c29 Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 13:21:35 -0300 Subject: [PATCH 03/15] Fixed docstring failure in pre-commit, Fixed request.get params to GitHub REST API standards --- web_programming/fetch_github_info.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index 86f9d45c0ff0..b0a56896e626 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -1,31 +1,31 @@ #!/usr/bin/env python3 -import requests - """ Created by sarathkaul on 14/11/19 Basic authentication using an API password is deprecated and will soon no longer work. Visit https://developer.github.com/changes/2020-02-14-deprecating-password-auth for more information around suggested workarounds and removal dates. -""" -""" -Created by lawric1 on 24/11/20 -To generate your personal access token visit https://github.com/settings/tokens +Updated by lawric1 on 24/11/20 + +Authentication will be made via access token. +To generate your personal access token visit https://github.com/settings/tokens. """ +import requests _GITHUB_API = "https://api.github.com/user" -def fetch_github_info(auth_user: str, auth_token: str) -> dict: +def fetch_github_info(auth_token: str) -> dict: """ Fetch GitHub info of a user using the requests module """ - return requests.get(_GITHUB_API, auth=(auth_user, auth_token)).json() + token = {'Authorization': 'token {}'.format(auth_token)} + return requests.get(_GITHUB_API, headers=token).json() if __name__ == "__main__": - for key, value in fetch_github_info("", "").items(): + for key, value in fetch_github_info("").items(): print(f"{key}: {value}") From 993ef9ce1a3255002fd72179b96f9812ba787d39 Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 13:28:21 -0300 Subject: [PATCH 04/15] run black formatter --- web_programming/fetch_github_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index b0a56896e626..aa09ec539095 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -22,7 +22,7 @@ def fetch_github_info(auth_token: str) -> dict: """ Fetch GitHub info of a user using the requests module """ - token = {'Authorization': 'token {}'.format(auth_token)} + token = {"Authorization": "token {}".format(auth_token)} return requests.get(_GITHUB_API, headers=token).json() From d640d406ed9344f33504698b15bf2feda60dd4cb Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 16:05:04 -0300 Subject: [PATCH 05/15] Add USER_TOKEN constant and checks if empty, removes deprecated docstring --- web_programming/fetch_github_info.py | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index aa09ec539095..84394a3d7cf7 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -1,12 +1,5 @@ #!/usr/bin/env python3 """ -Created by sarathkaul on 14/11/19 - -Basic authentication using an API password is deprecated and will soon no longer work. -Visit https://developer.github.com/changes/2020-02-14-deprecating-password-auth -for more information around suggested workarounds and removal dates. - - Updated by lawric1 on 24/11/20 Authentication will be made via access token. @@ -16,7 +9,7 @@ import requests _GITHUB_API = "https://api.github.com/user" - +USER_TOKEN = "" # provide your access token def fetch_github_info(auth_token: str) -> dict: """ @@ -27,5 +20,8 @@ def fetch_github_info(auth_token: str) -> dict: if __name__ == "__main__": - for key, value in fetch_github_info("").items(): - print(f"{key}: {value}") + if USER_TOKEN: + for key, value in fetch_github_info(USER_TOKEN).items(): + print(f"{key}: {value}") + else: + print("token should not be empty") \ No newline at end of file From cfb87dc31cf2c234bb5c2c181931ec528eec5785 Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 16:41:02 -0300 Subject: [PATCH 06/15] Add descriptive dict type hint, change headers format to f-string --- web_programming/fetch_github_info.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index 84394a3d7cf7..e6c134a453e0 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -6,16 +6,18 @@ To generate your personal access token visit https://github.com/settings/tokens. """ + import requests +from typing import Dict _GITHUB_API = "https://api.github.com/user" USER_TOKEN = "" # provide your access token -def fetch_github_info(auth_token: str) -> dict: +def fetch_github_info(auth_token: str) -> Dict[any, any]: """ Fetch GitHub info of a user using the requests module """ - token = {"Authorization": "token {}".format(auth_token)} + token = {"Authorization": f"token {auth_token}"} return requests.get(_GITHUB_API, headers=token).json() @@ -24,4 +26,4 @@ def fetch_github_info(auth_token: str) -> dict: for key, value in fetch_github_info(USER_TOKEN).items(): print(f"{key}: {value}") else: - print("token should not be empty") \ No newline at end of file + print("token should not be empty.") \ No newline at end of file From ea5164e9660d96bb311f3f622db3debb9d1a531a Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 16:49:18 -0300 Subject: [PATCH 07/15] Add Accept header --- web_programming/fetch_github_info.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index e6c134a453e0..146c84f5c2b3 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -17,8 +17,9 @@ def fetch_github_info(auth_token: str) -> Dict[any, any]: """ Fetch GitHub info of a user using the requests module """ - token = {"Authorization": f"token {auth_token}"} - return requests.get(_GITHUB_API, headers=token).json() + headers = {"Authorization": f"token {auth_token}", + "Accept": "application/vnd.github.v3+json"} + return requests.get(_GITHUB_API, headers=headers).json() if __name__ == "__main__": From fa46152a50b0055a43a7efe2d7e265c11ca2a46b Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 17:16:50 -0300 Subject: [PATCH 08/15] Fix pre-commit error --- web_programming/fetch_github_info.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index 146c84f5c2b3..bffa755ce7b3 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -6,19 +6,21 @@ To generate your personal access token visit https://github.com/settings/tokens. """ - -import requests from typing import Dict +import requests _GITHUB_API = "https://api.github.com/user" -USER_TOKEN = "" # provide your access token +USER_TOKEN = "" # provide your access token + def fetch_github_info(auth_token: str) -> Dict[any, any]: """ Fetch GitHub info of a user using the requests module """ - headers = {"Authorization": f"token {auth_token}", - "Accept": "application/vnd.github.v3+json"} + headers = { + "Authorization": f"token {auth_token}", + "Accept": "application/vnd.github.v3+json", + } return requests.get(_GITHUB_API, headers=headers).json() @@ -27,4 +29,4 @@ def fetch_github_info(auth_token: str) -> Dict[any, any]: for key, value in fetch_github_info(USER_TOKEN).items(): print(f"{key}: {value}") else: - print("token should not be empty.") \ No newline at end of file + print("token should not be empty.") From 76a6e56928fa11d9ac3f9b7faa7f799ee4db9664 Mon Sep 17 00:00:00 2001 From: lawric1 <67882089+lawric1@users.noreply.github.com> Date: Tue, 24 Nov 2020 17:23:16 -0300 Subject: [PATCH 09/15] Fix pre-commit error --- web_programming/fetch_github_info.py | 1 + 1 file changed, 1 insertion(+) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index bffa755ce7b3..01b6e10e3332 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -7,6 +7,7 @@ """ from typing import Dict + import requests _GITHUB_API = "https://api.github.com/user" From 7d09accfa0b4dccdfe82f983fde1aa49fe34fb9b Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 25 Nov 2020 10:09:51 +0530 Subject: [PATCH 10/15] Add test for fetch_github_info --- web_programming/fetch_github_info.py | 55 ++++++++++++++++++++++++---- 1 file changed, 48 insertions(+), 7 deletions(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index 01b6e10e3332..ce0b8b9a9c35 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -1,20 +1,37 @@ #!/usr/bin/env python3 """ +Created by sarathkaul on 14/11/19 Updated by lawric1 on 24/11/20 Authentication will be made via access token. To generate your personal access token visit https://github.com/settings/tokens. -""" -from typing import Dict +NOTE: +Never hardcode any credential information in the code. Always use an environment +file to store the private information and use the `os` module to get the information +during runtime. + +Create a ".env" file in the root directory and write these two lines in that file +with your token:: + +#!/usr/bin/env bash +export USER_TOKEN="" +""" +import os +from typing import Any, Dict import requests -_GITHUB_API = "https://api.github.com/user" -USER_TOKEN = "" # provide your access token +BASE_URL = "https://api.github.com" + +# https://docs.github.com/en/free-pro-team@latest/rest/reference/users#get-the-authenticated-user +AUTHENTICATED_USER_ENDPOINT = BASE_URL + "/user" +# https://github.com/settings/tokens +USER_TOKEN = os.environ.get("USER_TOKEN", "") -def fetch_github_info(auth_token: str) -> Dict[any, any]: + +def fetch_github_info(auth_token: str) -> Dict[Any, Any]: """ Fetch GitHub info of a user using the requests module """ @@ -22,7 +39,31 @@ def fetch_github_info(auth_token: str) -> Dict[any, any]: "Authorization": f"token {auth_token}", "Accept": "application/vnd.github.v3+json", } - return requests.get(_GITHUB_API, headers=headers).json() + return requests.get(AUTHENTICATED_USER_ENDPOINT, headers=headers).json() + + +def test_fetch_github_info(monkeypatch): + class FakeResponse: + def __init__(self, content) -> None: + assert isinstance(content, (bytes, str)) + self.content = content + + def json(self): + import json + + return json.loads(self.content) + + def mock_response(*args, **kwargs): + assert args[0] == AUTHENTICATED_USER_ENDPOINT + assert "Authorization" in kwargs["headers"] + assert kwargs["headers"]["Authorization"].startswith("token ") + assert "Accept" in kwargs["headers"] + return FakeResponse(b'{"login":"test","id":1}') + + monkeypatch.setattr(requests, "get", mock_response) + result = fetch_github_info("token") + assert result["login"] == "test" + assert result["id"] == 1 if __name__ == "__main__": @@ -30,4 +71,4 @@ def fetch_github_info(auth_token: str) -> Dict[any, any]: for key, value in fetch_github_info(USER_TOKEN).items(): print(f"{key}: {value}") else: - print("token should not be empty.") + raise ValueError("'USER_TOKEN' field cannot be empty.") From 8454ae13c03e4146ac1f7ed81955ec2050d09eb6 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 25 Nov 2020 10:18:47 +0530 Subject: [PATCH 11/15] Remove test function from main file --- web_programming/fetch_github_info.py | 24 ------------------------ 1 file changed, 24 deletions(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index ce0b8b9a9c35..4ace9aad8be2 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -42,30 +42,6 @@ def fetch_github_info(auth_token: str) -> Dict[Any, Any]: return requests.get(AUTHENTICATED_USER_ENDPOINT, headers=headers).json() -def test_fetch_github_info(monkeypatch): - class FakeResponse: - def __init__(self, content) -> None: - assert isinstance(content, (bytes, str)) - self.content = content - - def json(self): - import json - - return json.loads(self.content) - - def mock_response(*args, **kwargs): - assert args[0] == AUTHENTICATED_USER_ENDPOINT - assert "Authorization" in kwargs["headers"] - assert kwargs["headers"]["Authorization"].startswith("token ") - assert "Accept" in kwargs["headers"] - return FakeResponse(b'{"login":"test","id":1}') - - monkeypatch.setattr(requests, "get", mock_response) - result = fetch_github_info("token") - assert result["login"] == "test" - assert result["id"] == 1 - - if __name__ == "__main__": if USER_TOKEN: for key, value in fetch_github_info(USER_TOKEN).items(): From dc08346af645298d07bd82149813b1200c627c2f Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 25 Nov 2020 10:20:57 +0530 Subject: [PATCH 12/15] Create test_fetch_github_info.py --- web_programming/test_fetch_github_info.py | 24 +++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 web_programming/test_fetch_github_info.py diff --git a/web_programming/test_fetch_github_info.py b/web_programming/test_fetch_github_info.py new file mode 100644 index 000000000000..e951312c4479 --- /dev/null +++ b/web_programming/test_fetch_github_info.py @@ -0,0 +1,24 @@ +import json + +from .fetch_github_info import fetch_github_info + +def test_fetch_github_info(monkeypatch): + class FakeResponse: + def __init__(self, content) -> None: + assert isinstance(content, (bytes, str)) + self.content = content + + def json(self): + return json.loads(self.content) + + def mock_response(*args, **kwargs): + assert args[0] == AUTHENTICATED_USER_ENDPOINT + assert "Authorization" in kwargs["headers"] + assert kwargs["headers"]["Authorization"].startswith("token ") + assert "Accept" in kwargs["headers"] + return FakeResponse(b'{"login":"test","id":1}') + + monkeypatch.setattr(requests, "get", mock_response) + result = fetch_github_info("token") + assert result["login"] == "test" + assert result["id"] == 1 From de7b50b5f3ea00101235a3d4879e3e6661f0f945 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 25 Nov 2020 10:27:12 +0530 Subject: [PATCH 13/15] Update test_fetch_github_info.py --- web_programming/test_fetch_github_info.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/web_programming/test_fetch_github_info.py b/web_programming/test_fetch_github_info.py index e951312c4479..c4f34ac6a154 100644 --- a/web_programming/test_fetch_github_info.py +++ b/web_programming/test_fetch_github_info.py @@ -1,7 +1,10 @@ import json +import requests + from .fetch_github_info import fetch_github_info + def test_fetch_github_info(monkeypatch): class FakeResponse: def __init__(self, content) -> None: From 5b7664ca3ed69ed9d0749e342f40349eb9fa1b19 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 25 Nov 2020 10:29:00 +0530 Subject: [PATCH 14/15] Update test_fetch_github_info.py --- web_programming/test_fetch_github_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/test_fetch_github_info.py b/web_programming/test_fetch_github_info.py index c4f34ac6a154..2da97c782df7 100644 --- a/web_programming/test_fetch_github_info.py +++ b/web_programming/test_fetch_github_info.py @@ -2,7 +2,7 @@ import requests -from .fetch_github_info import fetch_github_info +from .fetch_github_info import AUTHENTICATED_USER_ENDPOINT, fetch_github_info def test_fetch_github_info(monkeypatch): From fe1a2ddf1cdbb2900b92edd7e822b845eadb6311 Mon Sep 17 00:00:00 2001 From: Dhruv Manilawala Date: Wed, 25 Nov 2020 10:37:50 +0530 Subject: [PATCH 15/15] No need to cover __name__ == __main__ block --- web_programming/fetch_github_info.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/web_programming/fetch_github_info.py b/web_programming/fetch_github_info.py index 4ace9aad8be2..c9198460f211 100644 --- a/web_programming/fetch_github_info.py +++ b/web_programming/fetch_github_info.py @@ -42,7 +42,7 @@ def fetch_github_info(auth_token: str) -> Dict[Any, Any]: return requests.get(AUTHENTICATED_USER_ENDPOINT, headers=headers).json() -if __name__ == "__main__": +if __name__ == "__main__": # pragma: no cover if USER_TOKEN: for key, value in fetch_github_info(USER_TOKEN).items(): print(f"{key}: {value}")