Skip to content

Commit

Permalink
Merge pull request #19 from SAT/autm-76/gravity-api
Browse files Browse the repository at this point in the history
Add Gravity Forms module
  • Loading branch information
Jeremy Gibson authored and GitHub Enterprise committed Jan 31, 2024
2 parents 158bc81 + b09d592 commit e9001b5
Show file tree
Hide file tree
Showing 8 changed files with 147 additions and 6 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/test_publish_and_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 }}
Expand Down Expand Up @@ -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 }}
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -169,3 +169,5 @@ cython_debug/

# repo pulled in for CI/CD
sat-actions/

sat-utils-venv
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,24 @@ logger = SATLogger(__name__)
logger.info("Hello, world!")
```

### Gravity Forms

Three environment variables are required to authenticate with the Gravity Forms API.

- 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


gravity = GravityForms()
cards_requested = gravity.get("/forms/3/entries")
```

## Development

### Setup
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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]
Expand Down
8 changes: 7 additions & 1 deletion requirements/base/base.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --output-file=requirements/base/base.txt pyproject.toml
Expand All @@ -18,6 +18,8 @@ idna==3.4
# via requests
ldap3==2.9.1
# via sat-utils (pyproject.toml)
oauthlib==3.2.2
# via requests-oauthlib
oracledb==1.4.1
# via sat-utils (pyproject.toml)
pyasn1==0.5.0
Expand All @@ -27,6 +29,10 @@ pycparser==2.21
pyodbc==5.0.1
# via sat-utils (pyproject.toml)
requests==2.30.0
# via
# requests-oauthlib
# sat-utils (pyproject.toml)
requests-oauthlib==1.3.1
# via sat-utils (pyproject.toml)
slack-sdk==3.21.3
# via sat-utils (pyproject.toml)
Expand Down
15 changes: 14 additions & 1 deletion requirements/dev/dev.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#
# This file is autogenerated by pip-compile with Python 3.11
# This file is autogenerated by pip-compile with Python 3.10
# by the following command:
#
# pip-compile --extra=dev --output-file=requirements/dev/dev.txt pyproject.toml
#
appnope==0.1.3
# via ipython
asttokens==2.2.1
# via stack-data
attrs==23.1.0
Expand All @@ -30,6 +32,7 @@ colorama==0.4.6
# via mkdocs-material
coverage[toml]==7.2.5
# via
# coverage
# pytest-cov
# sat-utils (pyproject.toml)
cryptography==41.0.3
Expand Down Expand Up @@ -103,6 +106,8 @@ mypy-extensions==1.0.0
# mypy
nodeenv==1.7.0
# via pre-commit
oauthlib==3.2.2
# via requests-oauthlib
oracledb==1.4.1
# via sat-utils (pyproject.toml)
packaging==23.1
Expand Down Expand Up @@ -178,7 +183,10 @@ requests==2.30.0
# via
# flit
# mkdocs-material
# requests-oauthlib
# sat-utils (pyproject.toml)
requests-oauthlib==1.3.1
# via sat-utils (pyproject.toml)
rich==13.3.5
# via bandit
ruff==0.0.263
Expand All @@ -199,6 +207,11 @@ tokenize-rt==4.2.1
# via pyupgrade
toml==0.10.2
# via pytest
tomli==2.0.1
# via
# black
# coverage
# mypy
tomli-w==1.0.0
# via flit
traitlets==5.9.0
Expand Down
76 changes: 76 additions & 0 deletions sat/gravity_forms.py
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 {}
24 changes: 24 additions & 0 deletions tests/test_gravity_forms.py
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

0 comments on commit e9001b5

Please sign in to comment.