From 60857c7bc97154b25ca67f108a609b1803f7d0ea Mon Sep 17 00:00:00 2001 From: John Champion Date: Fri, 19 Jan 2024 09:30:20 -0500 Subject: [PATCH 01/20] add module --- README.md | 10 ++++++++ pyproject.toml | 6 +++-- requirements/base/base.txt | 14 ++++++++++- requirements/dev/dev.txt | 24 ++++++++++++------- sat/gravity_forms.py | 48 +++++++++++++++++++++++++++++++++++++ tests/test_gravity_forms.py | 11 +++++++++ 6 files changed, 102 insertions(+), 11 deletions(-) create mode 100644 sat/gravity_forms.py create mode 100644 tests/test_gravity_forms.py diff --git a/README.md b/README.md index 263e42c..d00054d 100644 --- a/README.md +++ b/README.md @@ -36,6 +36,16 @@ logger = SATLogger(__name__) logger.info("Hello, world!") ``` +### Gravity Forms + +Two environment variables are required to use Gravity Forms. + +```python +from sat.gravity_forms import GravityForms + +requests = GravityForms.get_cards_requested() +``` + ## Development ### Setup diff --git a/pyproject.toml b/pyproject.toml index 0f99210..439892f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,11 +4,12 @@ build-backend = "flit_core.buildapi" [project] name = "sat-utils" -version = "1.1.13" +version = "1.2.0" authors = [ { name="Ryan Semmler", email="rsemmle@ncsu.edu" }, { name="Shawn Taylor", email="staylor8@ncsu.edu" }, { name="Jeremy Gibson", email="jmgibso3@ncsu.edu"}, + { name="John Champion", email="jtchampi@ncsu.edu"} ] description = "Contains a collection of shared utility functions" readme = "README.md" @@ -20,7 +21,8 @@ dependencies = [ "ldap3>=2.9.1, <3.0.0", "cx-Oracle==8.3.0", "oracledb==1.4.1", - "pyodbc==4.0.39" + "pyodbc==4.0.39", + "requests_oauthlib==1.3.1" ] [project.optional-dependencies] diff --git a/requirements/base/base.txt b/requirements/base/base.txt index f64d5b5..7310a15 100644 --- a/requirements/base/base.txt +++ b/requirements/base/base.txt @@ -1,9 +1,11 @@ # -# This file is autogenerated by pip-compile with Python 3.10 +# This file is autogenerated by pip-compile with Python 3.11 # by the following command: # # pip-compile --generate-hashes --output-file=requirements/base/base.txt pyproject.toml # +--extra-index-url https://pypi.ehps.ncsu.edu/ + certifi==2022.12.7 \ --hash=sha256:35824b4c3a97115964b408844d64aa14db1cc518f6562e8d7261699d1350a9e3 \ --hash=sha256:4ad3232f5e926d6718ec31cfc1fcadfde020920e278684144551c91769c7bc18 @@ -202,6 +204,10 @@ ldap3==2.9.1 \ --hash=sha256:5869596fc4948797020d3f03b7939da938778a0f9e2009f7a072ccf92b8e8d70 \ --hash=sha256:f3e7fc4718e3f09dda568b57100095e0ce58633bcabbed8667ce3f8fbaa4229f # via sat-utils (pyproject.toml) +oauthlib==3.2.2 \ + --hash=sha256:8139f29aac13e25d502680e9e19963e83f16838d48a0d71c287fe40e7067fbca \ + --hash=sha256:9859c40929662bec5d64f34d01c99e093149682a3f38915dc0655d5a633dd918 + # via requests-oauthlib oracledb==1.4.1 \ --hash=sha256:077a6975fb65bc79dee95f25afa927735daa8999d7369b8bfb237e540002aa1e \ --hash=sha256:0d8c0be76ac8a3b3c4c68dfc2d1b801fb82fee96aaeb5fd33637d151524423bd \ @@ -280,6 +286,12 @@ pyodbc==4.0.39 \ requests==2.30.0 \ --hash=sha256:10e94cc4f3121ee6da529d358cdaeaff2f1c409cd377dbc72b825852f2f7e294 \ --hash=sha256:239d7d4458afcb28a692cdd298d87542235f4ca8d36d03a15bfc128a6559a2f4 + # via + # requests-oauthlib + # sat-utils (pyproject.toml) +requests-oauthlib==1.3.1 \ + --hash=sha256:2577c501a2fb8d05a304c09d090d6e47c306fef15809d102b327cf8364bddab5 \ + --hash=sha256:75beac4a47881eeb94d5ea5d6ad31ef88856affe2332b9aafb52c6452ccf0d7a # via sat-utils (pyproject.toml) slack-sdk==3.21.3 \ --hash=sha256:20829bdc1a423ec93dac903470975ebf3bc76fd3fd91a4dadc0eeffc940ecb0c \ diff --git a/requirements/dev/dev.txt b/requirements/dev/dev.txt index cf9c3bf..920fb1c 100644 --- a/requirements/dev/dev.txt +++ b/requirements/dev/dev.txt @@ -1,9 +1,15 @@ # -# This file is autogenerated by pip-compile with Python 3.10 +# This file is autogenerated by pip-compile with Python 3.11 # by the following command: # # pip-compile --extra=dev --generate-hashes --output-file=requirements/dev/dev.txt pyproject.toml # +--extra-index-url https://pypi.ehps.ncsu.edu/ + +appnope==0.1.3 \ + --hash=sha256:02bd91c4de869fbb1e1c50aafc4098827a7a54ab2f39d9dcba6c9547ed920e24 \ + --hash=sha256:265a455292d0bd8a72453494fa24df5a11eb18373a60c7c0430889f22548605e + # via ipython asttokens==2.2.1 \ --hash=sha256:4622110b2a6f30b77e1473affaa97e711bc2f07d3f10848420ff1898edbe94f3 \ --hash=sha256:6b0ac9e93fb0335014d382b8fa9b3afa7df546984258005da0b9e7095b3deb1c @@ -505,6 +511,10 @@ nodeenv==1.7.0 \ --hash=sha256:27083a7b96a25f2f5e1d8cb4b6317ee8aeda3bdd121394e5ac54e498028a042e \ --hash=sha256:e0e7f7dfb85fc5394c6fe1e8fa98131a2473e04311a45afb6508f7cf1836fa2b # via pre-commit +oauthlib==3.2.2 \ + --hash=sha256:8139f29aac13e25d502680e9e19963e83f16838d48a0d71c287fe40e7067fbca \ + --hash=sha256:9859c40929662bec5d64f34d01c99e093149682a3f38915dc0655d5a633dd918 + # via requests-oauthlib oracledb==1.4.1 \ --hash=sha256:077a6975fb65bc79dee95f25afa927735daa8999d7369b8bfb237e540002aa1e \ --hash=sha256:0d8c0be76ac8a3b3c4c68dfc2d1b801fb82fee96aaeb5fd33637d151524423bd \ @@ -819,7 +829,12 @@ requests==2.30.0 \ # via # flit # mkdocs-material + # requests-oauthlib # sat-utils (pyproject.toml) +requests-oauthlib==1.3.1 \ + --hash=sha256:2577c501a2fb8d05a304c09d090d6e47c306fef15809d102b327cf8364bddab5 \ + --hash=sha256:75beac4a47881eeb94d5ea5d6ad31ef88856affe2332b9aafb52c6452ccf0d7a + # via sat-utils (pyproject.toml) rich==13.3.5 \ --hash=sha256:2d11b9b8dd03868f09b4fffadc84a6a8cda574e40dc90821bd845720ebb8e89c \ --hash=sha256:69cdf53799e63f38b95b9bf9c875f8c90e78dd62b2f00c13a911c7a3b9fa4704 @@ -873,13 +888,6 @@ toml==0.10.2 \ --hash=sha256:806143ae5bfb6a3c6e736a764057db0e6a0e05e338b5630894a5f779cabb4f9b \ --hash=sha256:b3bda1d108d5dd99f4a20d24d9c348e91c4db7ab1b749200bded2f839ccbe68f # via pytest -tomli==2.0.1 \ - --hash=sha256:939de3e7a6161af0c887ef91b7d41a53e7c5a1ca976325f429cb46ea9bc30ecc \ - --hash=sha256:de526c12914f0c550d15924c62d72abc48d6fe7364aa87328337a31007fe8a4f - # via - # black - # coverage - # mypy tomli-w==1.0.0 \ --hash=sha256:9f2a07e8be30a0729e533ec968016807069991ae2fd921a78d42f429ae5f4463 \ --hash=sha256:f463434305e0336248cac9c2dc8076b707d8a12d019dd349f5c1e382dd1ae1b9 diff --git a/sat/gravity_forms.py b/sat/gravity_forms.py new file mode 100644 index 0000000..bca845f --- /dev/null +++ b/sat/gravity_forms.py @@ -0,0 +1,48 @@ +""" +Access to the Gravity Forms API. + +Two required environment variables: +- CONSUMER_KEY +- CONSUMER_SECRET +""" + +import os + +import oauthlib +from requests_oauthlib import OAuth1Session + + +class GravityForms: + """ + A helper class for connecting to and calling the Gravity Forms API. + """ + + session = None + + @classmethod + def get_session(cls) -> OAuth1Session: + """Gets the existing authenticated session.""" + if cls.session is None: + session = OAuth1Session( + os.getenv("CONSUMER_KEY"), + client_secret=os.getenv("CONSUMER_SECRET"), + signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY, + ) + + return session + + @classmethod + def get_sponsors(cls): + """Gets the sponsor.""" + session = cls.get_session() + response = session.get("https://onecard.ncsu.edu/wp-json/gf/v2/forms/2/entries") + + return response.json() + + @classmethod + def get_cards_requested(cls): + """Gets requested cards.""" + session = cls.get_session() + response = session.get("https://onecard.ncsu.edu/wp-json/gf/v2/forms/3/entries") + + return response.json() diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py new file mode 100644 index 0000000..9b101fc --- /dev/null +++ b/tests/test_gravity_forms.py @@ -0,0 +1,11 @@ +from sat.gravity_forms import GravityForms + + +def test_get_sponsors(): + response = GravityForms.get_sponsors() + assert len(response) > 0 + + +def test_get_card_requests(): + response = GravityForms.get_cards_requested() + assert len(response) > 0 From 5ba934336b40a67a188f0ede0bae01089c282a41 Mon Sep 17 00:00:00 2001 From: John Champion Date: Fri, 19 Jan 2024 10:20:06 -0500 Subject: [PATCH 02/20] update readme --- README.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index d00054d..f33e2f5 100644 --- a/README.md +++ b/README.md @@ -38,7 +38,10 @@ logger.info("Hello, world!") ### Gravity Forms -Two environment variables are required to use Gravity Forms. +Two environment variables are required to authenticate with the Gravity Forms API. + +- CONSUMER_KEY +- CONSUMER_SECRET ```python from sat.gravity_forms import GravityForms From af51baf8866cf25aaead3e614a8a53f311985937 Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 23 Jan 2024 09:27:01 -0500 Subject: [PATCH 03/20] update package --- sat/gravity_forms.py | 57 ++++++++++++++++++++++++++++---------------- 1 file changed, 37 insertions(+), 20 deletions(-) diff --git a/sat/gravity_forms.py b/sat/gravity_forms.py index bca845f..d31a260 100644 --- a/sat/gravity_forms.py +++ b/sat/gravity_forms.py @@ -18,31 +18,48 @@ class GravityForms: """ session = None + base_url = "https://onecard.ncsu.edu/wp-json/gf/v2" - @classmethod - def get_session(cls) -> OAuth1Session: - """Gets the existing authenticated session.""" - if cls.session is None: - session = OAuth1Session( - os.getenv("CONSUMER_KEY"), - client_secret=os.getenv("CONSUMER_SECRET"), - signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY, - ) + def __init__(self, **settings) -> None: + """ + Configure the connection to Gravity Forms. - return session + Optional Parameters: + - consumer_key: Key for accessing the Gravity Forms API. + - consumer_secret: Secret for authenticating with the Gravity Forms API. + - base_url: An alternate base URL, if different than the default. + """ + self.session = OAuth1Session( + settings.get("consumer_key", os.getenv("CONSUMER_KEY")), + client_secret=settings.get("consumer_secret", os.getenv("CONSUMER_SECRET")), + signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY, + ) + self.base_url = settings.get("base_url", self.base_url) - @classmethod - def get_sponsors(cls): + def get_sponsors(self): """Gets the sponsor.""" - session = cls.get_session() - response = session.get("https://onecard.ncsu.edu/wp-json/gf/v2/forms/2/entries") + try: + response = self.session.get(self.base_url + "/forms/2/entries") + return response.json() + except Exception as e: + print(e) + raise e + def get_cards_requested(self): + """Gets requested cards.""" + response = self.session.get(self.base_url + "/forms/3/entries") return response.json() - @classmethod - def get_cards_requested(cls): - """Gets requested cards.""" - session = cls.get_session() - response = session.get("https://onecard.ncsu.edu/wp-json/gf/v2/forms/3/entries") + def get(self, endpoint: str): + """ + Submits a GET request to a specified endpoint. - return response.json() + Parameters: + - endpoint: The string representing the endpoint URL. (ex. "/forms") + """ + try: + response = self.session.get(self.base_url + endpoint) + return response.json() + except Exception as e: + print(e) + raise e From 9f4157adf600943b71649ee9e5fc3dc46a441dc6 Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 23 Jan 2024 15:20:58 -0500 Subject: [PATCH 04/20] update tests --- sat/gravity_forms.py | 22 ++++++++++++++++++---- tests/test_gravity_forms.py | 33 +++++++++++++++++++++++++++------ 2 files changed, 45 insertions(+), 10 deletions(-) diff --git a/sat/gravity_forms.py b/sat/gravity_forms.py index d31a260..6a7ad04 100644 --- a/sat/gravity_forms.py +++ b/sat/gravity_forms.py @@ -9,6 +9,7 @@ import os import oauthlib +from requests.exceptions import RequestException from requests_oauthlib import OAuth1Session @@ -29,9 +30,22 @@ def __init__(self, **settings) -> None: - consumer_secret: Secret for authenticating with the Gravity Forms API. - base_url: An alternate base URL, if different than the default. """ + consumer_key = settings.get("consumer_key", os.getenv("CONSUMER_KEY")) + consumer_secret = settings.get("consumer_secret", os.getenv("CONSUMER_SECRET")) + + if not consumer_key: + raise RuntimeError( + "A consumer_key is required as either an environment variable or parameter." + ) + + if not consumer_secret: + raise RuntimeError( + "A consumer_secret is required as either an environment variable or parameter." + ) + self.session = OAuth1Session( - settings.get("consumer_key", os.getenv("CONSUMER_KEY")), - client_secret=settings.get("consumer_secret", os.getenv("CONSUMER_SECRET")), + consumer_key, + client_secret=consumer_secret, signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY, ) self.base_url = settings.get("base_url", self.base_url) @@ -41,7 +55,7 @@ def get_sponsors(self): try: response = self.session.get(self.base_url + "/forms/2/entries") return response.json() - except Exception as e: + except RequestException as e: print(e) raise e @@ -60,6 +74,6 @@ def get(self, endpoint: str): try: response = self.session.get(self.base_url + endpoint) return response.json() - except Exception as e: + except RequestException as e: print(e) raise e diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 9b101fc..79db10f 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -1,11 +1,32 @@ +from unittest.mock import patch + +import pytest +from oauthlib.oauth1 import OAuth1Error +from requests.exceptions import RequestException from sat.gravity_forms import GravityForms -def test_get_sponsors(): - response = GravityForms.get_sponsors() - assert len(response) > 0 +def test_environment_variable_error(): + try: + GravityForms.get_sponsors() + assert False + except RuntimeError: + assert True + + +def test_request_exception(): + with patch("your_module.OAuth1Session.get") as mock_get: + gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") + mock_get.side_effect = RequestException("Simulated request exception.") + with pytest.raises(RequestException) as ex: + gravity_forms.get_sponsors() + assert "Simulated request exception." in str(ex.value) -def test_get_card_requests(): - response = GravityForms.get_cards_requested() - assert len(response) > 0 +def test_oauth_error(): + with patch("your_module.OAuth1Session") as mock_oauth_session: + gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") + mock_oauth_session.side_effect = OAuth1Error("Simulated OAuth error.") + with pytest.raises(OAuth1Error) as ex: + gravity_forms.get_sponsors() + assert "Simulated OAuth error." in str(ex.value) From d8b9ba008ba17254c87007fd4dece0fdef4d0429 Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 23 Jan 2024 15:26:20 -0500 Subject: [PATCH 05/20] update tests --- tests/test_gravity_forms.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 79db10f..1181262 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -15,7 +15,7 @@ def test_environment_variable_error(): def test_request_exception(): - with patch("your_module.OAuth1Session.get") as mock_get: + with patch("requests_oauthlib.OAuth1Session.get") as mock_get: gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") mock_get.side_effect = RequestException("Simulated request exception.") with pytest.raises(RequestException) as ex: @@ -24,7 +24,7 @@ def test_request_exception(): def test_oauth_error(): - with patch("your_module.OAuth1Session") as mock_oauth_session: + with patch("requests_oauthlib.OAuth1Session") as mock_oauth_session: gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") mock_oauth_session.side_effect = OAuth1Error("Simulated OAuth error.") with pytest.raises(OAuth1Error) as ex: From fd856b249916d72fb77c7868d3ec1a3acd328cf9 Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 23 Jan 2024 15:32:10 -0500 Subject: [PATCH 06/20] update tests --- tests/test_gravity_forms.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 1181262..8915c72 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -8,7 +8,8 @@ def test_environment_variable_error(): try: - GravityForms.get_sponsors() + gravity_forms = GravityForms() + gravity_forms.get_sponsors() assert False except RuntimeError: assert True From 16e01a84712c4d629d973a49a67d3fd49450cf4d Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 23 Jan 2024 16:37:05 -0500 Subject: [PATCH 07/20] update tests --- tests/test_gravity_forms.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 8915c72..bda6992 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -28,6 +28,8 @@ def test_oauth_error(): with patch("requests_oauthlib.OAuth1Session") as mock_oauth_session: gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") mock_oauth_session.side_effect = OAuth1Error("Simulated OAuth error.") - with pytest.raises(OAuth1Error) as ex: + with pytest.raises(Exception) as ex: gravity_forms.get_sponsors() - assert "Simulated OAuth error." in str(ex.value) + + assert isinstance(ex.value, OAuth1Error) + assert str(ex.value) == "Simulated OAuth error." From 6a1d96a157a8775b459f0bcc39b9a5f3ea5492d2 Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 23 Jan 2024 16:38:50 -0500 Subject: [PATCH 08/20] remove test --- tests/test_gravity_forms.py | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index bda6992..cf0a9c0 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -1,7 +1,6 @@ from unittest.mock import patch import pytest -from oauthlib.oauth1 import OAuth1Error from requests.exceptions import RequestException from sat.gravity_forms import GravityForms @@ -22,14 +21,3 @@ def test_request_exception(): with pytest.raises(RequestException) as ex: gravity_forms.get_sponsors() assert "Simulated request exception." in str(ex.value) - - -def test_oauth_error(): - with patch("requests_oauthlib.OAuth1Session") as mock_oauth_session: - gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") - mock_oauth_session.side_effect = OAuth1Error("Simulated OAuth error.") - with pytest.raises(Exception) as ex: - gravity_forms.get_sponsors() - - assert isinstance(ex.value, OAuth1Error) - assert str(ex.value) == "Simulated OAuth error." From bedc353add3d0c5837d45c4a25cec71629323546 Mon Sep 17 00:00:00 2001 From: John Champion Date: Wed, 24 Jan 2024 09:15:31 -0500 Subject: [PATCH 09/20] add test --- tests/test_gravity_forms.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index cf0a9c0..6d9f939 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -14,10 +14,19 @@ def test_environment_variable_error(): assert True -def test_request_exception(): +def test_request_exception_get_sponsors(): with patch("requests_oauthlib.OAuth1Session.get") as mock_get: gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") mock_get.side_effect = RequestException("Simulated request exception.") with pytest.raises(RequestException) as ex: gravity_forms.get_sponsors() assert "Simulated request exception." in str(ex.value) + + +def test_request_exception_get_cards_requested(): + with patch("requests_oauthlib.OAuth1Session.get") as mock_get: + gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") + mock_get.side_effect = RequestException("Simulated request exception.") + with pytest.raises(RequestException) as ex: + gravity_forms.get_cards_requested() + assert "Simulated request exception." in str(ex.value) From 05d1fac634bc21661305ee86916a67893871ebb8 Mon Sep 17 00:00:00 2001 From: John Champion Date: Wed, 24 Jan 2024 09:18:44 -0500 Subject: [PATCH 10/20] add test --- tests/test_gravity_forms.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 6d9f939..ae55e3b 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -30,3 +30,12 @@ def test_request_exception_get_cards_requested(): with pytest.raises(RequestException) as ex: gravity_forms.get_cards_requested() assert "Simulated request exception." in str(ex.value) + + +def test_request_exception_get(): + with patch("requests_oauthlib.OAuth1Session.get") as mock_get: + gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") + mock_get.side_effect = RequestException("Simulated request exception.") + with pytest.raises(RequestException) as ex: + gravity_forms.get("/forms") + assert "Simulated request exception." in str(ex.value) From a6cc47544675fd60fd54a94f5187ab9ecfef41fb Mon Sep 17 00:00:00 2001 From: John Champion Date: Thu, 25 Jan 2024 11:03:48 -0500 Subject: [PATCH 11/20] update gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 8fc3122..adc77c0 100644 --- a/.gitignore +++ b/.gitignore @@ -170,4 +170,4 @@ cython_debug/ # repo pulled in for CI/CD sat-actions/ -sat-utils +sat-utils-venv From 025c3864a43d8bf4c28e9bcbaa23ceab393e8c34 Mon Sep 17 00:00:00 2001 From: John Champion Date: Mon, 29 Jan 2024 10:22:17 -0500 Subject: [PATCH 12/20] make changes --- README.md | 13 ++++++++---- sat/gravity_forms.py | 40 ++++++++++++++++++------------------- tests/test_gravity_forms.py | 22 ++------------------ 3 files changed, 30 insertions(+), 45 deletions(-) diff --git a/README.md b/README.md index f33e2f5..21f821a 100644 --- a/README.md +++ b/README.md @@ -38,15 +38,20 @@ logger.info("Hello, world!") ### Gravity Forms -Two environment variables are required to authenticate with the Gravity Forms API. +Three environment variables are required to authenticate with the Gravity Forms API. -- CONSUMER_KEY -- CONSUMER_SECRET +- GRAVITY_FORMS_CONSUMER_KEY +- GRAVITY_FORMS_CONSUMER_SECRET +- GRAVITY_FORMS_BASE_URL + +Alternatively, these values can be passed into the GravityForms initialization as parameters. ```python from sat.gravity_forms import GravityForms -requests = GravityForms.get_cards_requested() + +gravity = GravityForms() +cards_requested = gravity.get("/forms/3/entries") ``` ## Development diff --git a/sat/gravity_forms.py b/sat/gravity_forms.py index 6a7ad04..c304360 100644 --- a/sat/gravity_forms.py +++ b/sat/gravity_forms.py @@ -12,6 +12,10 @@ from requests.exceptions import RequestException from requests_oauthlib import OAuth1Session +from sat.logs import SATLogger + +logger = SATLogger(name=__name__) + class GravityForms: """ @@ -19,7 +23,7 @@ class GravityForms: """ session = None - base_url = "https://onecard.ncsu.edu/wp-json/gf/v2" + base_url = None def __init__(self, **settings) -> None: """ @@ -30,19 +34,27 @@ def __init__(self, **settings) -> None: - consumer_secret: Secret for authenticating with the Gravity Forms API. - base_url: An alternate base URL, if different than the default. """ - consumer_key = settings.get("consumer_key", os.getenv("CONSUMER_KEY")) - consumer_secret = settings.get("consumer_secret", os.getenv("CONSUMER_SECRET")) + consumer_key = settings.get("consumer_key", os.getenv("GRAVITY_FORMS_CONSUMER_KEY")) + consumer_secret = settings.get( + "consumer_secret", os.getenv("GRAVITY_FORMS_CONSUMER_SECRET") + ) + self.base_url = settings.get("base_url", os.getenv("GRAVITY_FORMS_BASE_URL")) if not consumer_key: - raise RuntimeError( + raise ValueError( "A consumer_key is required as either an environment variable or parameter." ) if not consumer_secret: - raise RuntimeError( + raise ValueError( "A consumer_secret is required as either an environment variable or parameter." ) + if not self.base_url: + raise ValueError( + "A base_url is required as either an environment variable or parameter." + ) + self.session = OAuth1Session( consumer_key, client_secret=consumer_secret, @@ -50,20 +62,6 @@ def __init__(self, **settings) -> None: ) self.base_url = settings.get("base_url", self.base_url) - def get_sponsors(self): - """Gets the sponsor.""" - try: - response = self.session.get(self.base_url + "/forms/2/entries") - return response.json() - except RequestException as e: - print(e) - raise e - - def get_cards_requested(self): - """Gets requested cards.""" - response = self.session.get(self.base_url + "/forms/3/entries") - return response.json() - def get(self, endpoint: str): """ Submits a GET request to a specified endpoint. @@ -75,5 +73,5 @@ def get(self, endpoint: str): response = self.session.get(self.base_url + endpoint) return response.json() except RequestException as e: - print(e) - raise e + logger.error(str(e)) + return {} diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index ae55e3b..0531b03 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -8,30 +8,12 @@ def test_environment_variable_error(): try: gravity_forms = GravityForms() - gravity_forms.get_sponsors() + gravity_forms.get("/forms/2/entries") assert False - except RuntimeError: + except ValueError: assert True -def test_request_exception_get_sponsors(): - with patch("requests_oauthlib.OAuth1Session.get") as mock_get: - gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") - mock_get.side_effect = RequestException("Simulated request exception.") - with pytest.raises(RequestException) as ex: - gravity_forms.get_sponsors() - assert "Simulated request exception." in str(ex.value) - - -def test_request_exception_get_cards_requested(): - with patch("requests_oauthlib.OAuth1Session.get") as mock_get: - gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") - mock_get.side_effect = RequestException("Simulated request exception.") - with pytest.raises(RequestException) as ex: - gravity_forms.get_cards_requested() - assert "Simulated request exception." in str(ex.value) - - def test_request_exception_get(): with patch("requests_oauthlib.OAuth1Session.get") as mock_get: gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") From ec281ec4c13b8b677e74d8643cbfc9f0b573d331 Mon Sep 17 00:00:00 2001 From: John Champion Date: Mon, 29 Jan 2024 10:27:08 -0500 Subject: [PATCH 13/20] update runner; update test --- .github/workflows/test_publish_and_release.yml | 6 +++--- tests/test_gravity_forms.py | 4 +++- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test_publish_and_release.yml b/.github/workflows/test_publish_and_release.yml index 234a399..8867b33 100644 --- a/.github/workflows/test_publish_and_release.yml +++ b/.github/workflows/test_publish_and_release.yml @@ -16,7 +16,7 @@ env: jobs: run_tests: name: Run Tests - runs-on: sat-hosted + runs-on: runner-1 steps: - name: Get Code uses: actions/checkout@v3 @@ -83,7 +83,7 @@ jobs: # only proceed if test job ran successfully needs: run_tests if: ${{ github.event_name == 'push' }} - runs-on: sat-hosted + runs-on: runner-1 env: FLIT_USERNAME: ${{ secrets.PYPI_USERNAME }} FLIT_PASSWORD: ${{ secrets.PYPI_PASSWORD }} @@ -162,7 +162,7 @@ jobs: # only proceed if publish workflow ran successfully needs: build_and_publish if: ${{ github.event_name == 'push' }} - runs-on: sat-hosted + runs-on: runner-1 env: PR_BODY: ${{ github.event.pull_request.body }} LATEST_COMMIT_MESSAGE: ${{ github.event.head_commit.message }} diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 0531b03..31aaa96 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -16,7 +16,9 @@ def test_environment_variable_error(): def test_request_exception_get(): with patch("requests_oauthlib.OAuth1Session.get") as mock_get: - gravity_forms = GravityForms(consumer_key="your_key", consumer_secret="your_secret") + gravity_forms = GravityForms( + consumer_key="your_key", consumer_secret="your_secret", base_url="https://baseurl.edu" + ) mock_get.side_effect = RequestException("Simulated request exception.") with pytest.raises(RequestException) as ex: gravity_forms.get("/forms") From 969779897843c73ae30623d1d8b936d4c7a65fa0 Mon Sep 17 00:00:00 2001 From: John Champion Date: Mon, 29 Jan 2024 10:53:34 -0500 Subject: [PATCH 14/20] remove redundant line --- sat/gravity_forms.py | 1 - 1 file changed, 1 deletion(-) diff --git a/sat/gravity_forms.py b/sat/gravity_forms.py index c304360..33404c0 100644 --- a/sat/gravity_forms.py +++ b/sat/gravity_forms.py @@ -60,7 +60,6 @@ def __init__(self, **settings) -> None: client_secret=consumer_secret, signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY, ) - self.base_url = settings.get("base_url", self.base_url) def get(self, endpoint: str): """ From 11f64600990909e4ba038d5cc8518c126fea3eca Mon Sep 17 00:00:00 2001 From: John Champion Date: Mon, 29 Jan 2024 15:55:42 -0500 Subject: [PATCH 15/20] modify test --- tests/test_gravity_forms.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 31aaa96..e1c4b8e 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -20,6 +20,14 @@ def test_request_exception_get(): consumer_key="your_key", consumer_secret="your_secret", base_url="https://baseurl.edu" ) mock_get.side_effect = RequestException("Simulated request exception.") - with pytest.raises(RequestException) as ex: + # with pytest.raises(RequestException) as ex: + # gravity_forms.get("/forms") + try: gravity_forms.get("/forms") - assert "Simulated request exception." in str(ex.value) + except RequestException as ex: + # Asserting that the error message in the caught exception matches the simulated one + assert str(ex) == "Simulated request exception." + else: + # If no exception is raised, fail the test + pytest.fail("Expected RequestException but no exception was raised.") + # assert "Simulated request exception." in str(ex.value) From 470da78c6642fa6d2c023c456a166b20eab3be07 Mon Sep 17 00:00:00 2001 From: John Champion Date: Mon, 29 Jan 2024 16:27:21 -0500 Subject: [PATCH 16/20] modify test --- tests/test_gravity_forms.py | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index e1c4b8e..5b89da1 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -20,14 +20,8 @@ def test_request_exception_get(): consumer_key="your_key", consumer_secret="your_secret", base_url="https://baseurl.edu" ) mock_get.side_effect = RequestException("Simulated request exception.") - # with pytest.raises(RequestException) as ex: - # gravity_forms.get("/forms") - try: + + with pytest.raises(RequestException) as ex: gravity_forms.get("/forms") - except RequestException as ex: - # Asserting that the error message in the caught exception matches the simulated one - assert str(ex) == "Simulated request exception." - else: - # If no exception is raised, fail the test - pytest.fail("Expected RequestException but no exception was raised.") - # assert "Simulated request exception." in str(ex.value) + + assert str(ex.value) == "Simulated request exception." From 0dcc0eed0050c44e156d40bdfc0eb5fe2205e73d Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 30 Jan 2024 09:31:06 -0500 Subject: [PATCH 17/20] modify test --- tests/test_gravity_forms.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 5b89da1..d0bd81e 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -14,14 +14,14 @@ def test_environment_variable_error(): assert True -def test_request_exception_get(): +def test_request_exception_get(caplog): with patch("requests_oauthlib.OAuth1Session.get") as mock_get: gravity_forms = GravityForms( consumer_key="your_key", consumer_secret="your_secret", base_url="https://baseurl.edu" ) mock_get.side_effect = RequestException("Simulated request exception.") - with pytest.raises(RequestException) as ex: + with pytest.raises(RequestException): gravity_forms.get("/forms") - assert str(ex.value) == "Simulated request exception." + assert "Simulated request exception." in caplog.text From bee2713394594d455901447423a459195e01e121 Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 30 Jan 2024 09:33:40 -0500 Subject: [PATCH 18/20] modify test --- tests/test_gravity_forms.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index d0bd81e..e42d15a 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -21,7 +21,9 @@ def test_request_exception_get(caplog): ) mock_get.side_effect = RequestException("Simulated request exception.") + print(f"1 - {caplog.text}") with pytest.raises(RequestException): gravity_forms.get("/forms") + print(f"2 - {caplog.text}") assert "Simulated request exception." in caplog.text From 3659b4a18513af2e37dfd9b6cab9578111e9710d Mon Sep 17 00:00:00 2001 From: John Champion Date: Tue, 30 Jan 2024 09:42:49 -0500 Subject: [PATCH 19/20] modify test --- tests/test_gravity_forms.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index e42d15a..08fed2e 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -16,14 +16,12 @@ def test_environment_variable_error(): def test_request_exception_get(caplog): with patch("requests_oauthlib.OAuth1Session.get") as mock_get: + mock_get.side_effect = RequestException("Simulated request exception.") gravity_forms = GravityForms( consumer_key="your_key", consumer_secret="your_secret", base_url="https://baseurl.edu" ) - mock_get.side_effect = RequestException("Simulated request exception.") - print(f"1 - {caplog.text}") with pytest.raises(RequestException): gravity_forms.get("/forms") - print(f"2 - {caplog.text}") assert "Simulated request exception." in caplog.text From b09d592084af7580c1e356b8d7c68b93fa7ca228 Mon Sep 17 00:00:00 2001 From: Jeremy Gibson Date: Tue, 30 Jan 2024 14:59:06 -0500 Subject: [PATCH 20/20] AUTM-76 Fix test --- tests/test_gravity_forms.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/tests/test_gravity_forms.py b/tests/test_gravity_forms.py index 08fed2e..e2df0b7 100644 --- a/tests/test_gravity_forms.py +++ b/tests/test_gravity_forms.py @@ -1,6 +1,5 @@ from unittest.mock import patch -import pytest from requests.exceptions import RequestException from sat.gravity_forms import GravityForms @@ -20,8 +19,6 @@ def test_request_exception_get(caplog): gravity_forms = GravityForms( consumer_key="your_key", consumer_secret="your_secret", base_url="https://baseurl.edu" ) - - with pytest.raises(RequestException): - gravity_forms.get("/forms") - + response = gravity_forms.get("/forms/2/entries") + assert response == {} assert "Simulated request exception." in caplog.text