-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from SAT/autm-76/gravity-api
Add Gravity Forms module
- Loading branch information
Showing
8 changed files
with
147 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,3 +169,5 @@ cython_debug/ | |
|
||
# repo pulled in for CI/CD | ||
sat-actions/ | ||
|
||
sat-utils-venv |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,11 +4,12 @@ build-backend = "flit_core.buildapi" | |
|
||
[project] | ||
name = "sat-utils" | ||
version = "1.1.15-pre.1" | ||
version = "1.2.0" | ||
authors = [ | ||
{ name="Ryan Semmler", email="[email protected]" }, | ||
{ name="Shawn Taylor", email="[email protected]" }, | ||
{ name="Jeremy Gibson", email="[email protected]"}, | ||
{ name="John Champion", email="[email protected]"} | ||
] | ||
description = "Contains a collection of shared utility functions" | ||
readme = "README.md" | ||
|
@@ -21,6 +22,7 @@ dependencies = [ | |
"cx_Oracle==8.3.0", | ||
"oracledb==1.4.1", | ||
"pyodbc>=5.0.1, <6.0.0", | ||
"requests_oauthlib==1.3.1" | ||
] | ||
|
||
[project.optional-dependencies] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
""" | ||
Access to the Gravity Forms API. | ||
Two required environment variables: | ||
- CONSUMER_KEY | ||
- CONSUMER_SECRET | ||
""" | ||
|
||
import os | ||
|
||
import oauthlib | ||
from requests.exceptions import RequestException | ||
from requests_oauthlib import OAuth1Session | ||
|
||
from sat.logs import SATLogger | ||
|
||
logger = SATLogger(name=__name__) | ||
|
||
|
||
class GravityForms: | ||
""" | ||
A helper class for connecting to and calling the Gravity Forms API. | ||
""" | ||
|
||
session = None | ||
base_url = None | ||
|
||
def __init__(self, **settings) -> None: | ||
""" | ||
Configure the connection to Gravity Forms. | ||
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. | ||
""" | ||
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 ValueError( | ||
"A consumer_key is required as either an environment variable or parameter." | ||
) | ||
|
||
if not consumer_secret: | ||
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, | ||
signature_type=oauthlib.oauth1.SIGNATURE_TYPE_QUERY, | ||
) | ||
|
||
def get(self, endpoint: str): | ||
""" | ||
Submits a GET request to a specified endpoint. | ||
Parameters: | ||
- endpoint: The string representing the endpoint URL. (ex. "/forms") | ||
""" | ||
try: | ||
response = self.session.get(self.base_url + endpoint) | ||
return response.json() | ||
except RequestException as e: | ||
logger.error(str(e)) | ||
return {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from unittest.mock import patch | ||
|
||
from requests.exceptions import RequestException | ||
from sat.gravity_forms import GravityForms | ||
|
||
|
||
def test_environment_variable_error(): | ||
try: | ||
gravity_forms = GravityForms() | ||
gravity_forms.get("/forms/2/entries") | ||
assert False | ||
except ValueError: | ||
assert True | ||
|
||
|
||
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" | ||
) | ||
response = gravity_forms.get("/forms/2/entries") | ||
assert response == {} | ||
assert "Simulated request exception." in caplog.text |