Skip to content

Commit

Permalink
Add provisions for security headers (internal use) and cleanup redund…
Browse files Browse the repository at this point in the history
…ant and incorrectly named constructors
  • Loading branch information
joe-sheldon committed Sep 15, 2023
1 parent 958ec20 commit 6189c31
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 13 deletions.
14 changes: 10 additions & 4 deletions src/blueink/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ def __init__(
private_api_key: str = None,
base_url: str = None,
raise_exceptions: bool = True,
security_headers: dict = None,
):
"""Initialize a Client instance to access the Blueink eSignature API
Expand All @@ -29,6 +30,7 @@ def __init__(
base_url: override the API base URL. If not supplied, we check the
environment variable BLUEINK_API_URL. If that is empty, the default
value of "https://api.blueink.com/api/v2" is used.
security_headers: Place for additional security headers, likely unnecessary
raise_exceptions (Default True): raise HTTPError if code != 200. Otherwise
return as NormalizedResponse objects.
Expand All @@ -44,9 +46,9 @@ def __init__(

if not private_api_key:
raise ValueError(
"A Blueink Private API Key must be provided on Client initialization"
" or specified via the environment variable"
" {ENV_BLUEINK_PRIVATE_API_KEY}"
f"A Blueink Private API Key must be provided on Client initialization"
f" or specified via the environment variable"
f" {ENV_BLUEINK_PRIVATE_API_KEY}"
)

if not base_url:
Expand All @@ -57,7 +59,11 @@ def __init__(

self._base_url = base_url

self._request_helper = RequestHelper(private_api_key, raise_exceptions)
self._request_helper = RequestHelper(
private_api_key,
raise_exceptions,
security_headers=security_headers,
)

self.bundles = BundleSubClient(self._base_url, self._request_helper)
self.persons = PersonSubClient(self._base_url, self._request_helper)
Expand Down
12 changes: 9 additions & 3 deletions src/blueink/request_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import requests
from munch import munchify
from requests import Request, Session

from blueink.constants import BLUEINK_PAGINATION_HEADER

Expand Down Expand Up @@ -55,9 +54,13 @@ def __init__(self, response: requests.Response):


class RequestHelper:
def __init__(self, private_api_key, raise_exceptions=False):
def __init__(
self, private_api_key, raise_exceptions=False, security_headers: dict = None
):

self._private_api_key = private_api_key
self._raise_exceptions = raise_exceptions
self._security_headers = security_headers

def delete(self, url, **kwargs):
return self._make_request("delete", url, **kwargs)
Expand Down Expand Up @@ -88,6 +91,9 @@ def _build_headers(self, content_type=None, more_headers=None):
if more_headers:
hdrs.update(more_headers)

if self._security_headers:
hdrs.update(self._security_headers)

hdrs["Authorization"] = f"Token {self._private_api_key}"

if content_type is not None:
Expand Down Expand Up @@ -120,6 +126,6 @@ def _make_request(
)

if self._raise_exceptions:
# print(response.content)
response.raise_for_status()

return NormalizedResponse(response)
3 changes: 0 additions & 3 deletions src/blueink/subclients/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@


class TemplateSubClient(SubClient):
def __init__(self, base_url, private_api_key):
super().__init__(base_url, private_api_key)

def paged_list(
self, page: int = 1, per_page: int = 50, **query_params
) -> PaginatedIterator:
Expand Down
3 changes: 0 additions & 3 deletions src/blueink/subclients/webhook.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@


class WebhookSubClient(SubClient):
def __init__(self, base_url, private_api_key):
super().__init__(base_url, private_api_key)

# ----------
# Webhooks
# ----------
Expand Down

0 comments on commit 6189c31

Please sign in to comment.