Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor: remove dependency on Benefits models, rename modules, improve Client parameters #27

Merged
merged 10 commits into from
May 20, 2022
58 changes: 33 additions & 25 deletions eligibility_api/api.py → eligibility_api/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RequestToken:
def __init__(
self,
types: Iterable[str],
agency_id: str,
agency_identifier: str,
thekaveman marked this conversation as resolved.
Show resolved Hide resolved
jws_signing_alg: str,
client_private_jwk: jwk.JWK,
jwe_encryption_alg: str,
Expand All @@ -50,7 +50,7 @@ def __init__(
.replace(tzinfo=datetime.timezone.utc)
.timestamp()
),
agency=agency_id,
agency=agency_identifier,
eligibility=types,
sub=sub,
name=name,
Expand Down Expand Up @@ -139,28 +139,36 @@ def __init__(
class Client:
"""Eligibility Verification API HTTP client."""

def __init__(self, agency, verifier, issuer):
logger.debug(f"Initialize client for agency: {agency.short_name}")
self.issuer = issuer

# get the eligibility type names
self.types = list(map(lambda t: t.name, agency.types_to_verify()))
self.agency_id = agency.agency_id
self.jws_signing_alg = agency.jws_signing_alg
self.client_private_jwk = agency.private_jwk
self.jwe_encryption_alg = verifier.jwe_encryption_alg
self.jwe_cek_enc = verifier.jwe_cek_enc
self.server_public_jwk = verifier.public_jwk

self.api_url = verifier.api_url
self.api_auth_header = verifier.api_auth_header
self.api_auth_key = verifier.api_auth_key
def __init__(
self,
api_url,
thekaveman marked this conversation as resolved.
Show resolved Hide resolved
api_auth_header,
api_auth_key,
thekaveman marked this conversation as resolved.
Show resolved Hide resolved
issuer,
agency_identifier,
thekaveman marked this conversation as resolved.
Show resolved Hide resolved
jws_signing_alg,
client_private_jwk,
jwe_encryption_alg,
jwe_cek_enc,
server_public_jwk,
):
self.api_url = api_url
self.api_auth_header = api_auth_header
self.api_auth_key = api_auth_key

def _tokenize_request(self, sub, name):
self.issuer = issuer
self.agency_identifier = agency_identifier
self.jws_signing_alg = jws_signing_alg
self.client_private_jwk = client_private_jwk
self.jwe_encryption_alg = jwe_encryption_alg
self.jwe_cek_enc = jwe_cek_enc
self.server_public_jwk = server_public_jwk

def _tokenize_request(self, sub, name, types):
"""Create a request token."""
return RequestToken(
self.types,
self.agency_id,
types,
self.agency_identifier,
self.jws_signing_alg,
self.client_private_jwk,
self.jwe_encryption_alg,
Expand Down Expand Up @@ -188,12 +196,12 @@ def _auth_headers(self, token):
headers[self.api_auth_header] = self.api_auth_key
return headers

def _request(self, sub, name):
def _request(self, sub, name, types):
"""Make an API request for eligibility verification."""
logger.debug("Start new eligibility verification request")

try:
token = self._tokenize_request(sub, name)
token = self._tokenize_request(sub, name, types)
except jwcrypto.JWException:
raise TokenError("Failed to tokenize form values")

Expand All @@ -219,6 +227,6 @@ def _request(self, sub, name):
)
raise ApiError("Unexpected eligibility verification response")

def verify(self, sub, name):
def verify(self, sub, name, types):
"""Check eligibility for the subject and name."""
return self._request(sub, name)
return self._request(sub, name, types)
File renamed without changes.