From 890557a2e845f22e0757c352390f1a1c2159fd72 Mon Sep 17 00:00:00 2001 From: Caroline6312 Date: Tue, 2 Jul 2024 16:16:24 -0700 Subject: [PATCH] Address the comments --- examples/sample_get_identity_buckets.py | 5 +---- tests/test_identity_map_client.py | 20 ++++++++++++++----- ...sponse.py => identity_buckets_response.py} | 0 uid2_client/identity_map_client.py | 4 ++-- uid2_client/input_util.py | 10 +--------- 5 files changed, 19 insertions(+), 20 deletions(-) rename uid2_client/{Identity_buckets_response.py => identity_buckets_response.py} (100%) diff --git a/examples/sample_get_identity_buckets.py b/examples/sample_get_identity_buckets.py index 49cde43..129e99a 100644 --- a/examples/sample_get_identity_buckets.py +++ b/examples/sample_get_identity_buckets.py @@ -30,9 +30,6 @@ def _usage(): identity_buckets_response = client.get_identity_buckets(datetime.datetime(year, month, day, hour, minute, second)) -if identity_buckets_response.buckets: - bucket = identity_buckets_response.buckets[0] +for bucket in identity_buckets_response.buckets: print("The bucket id of the first bucket: ", bucket.get_bucket_id()) print("The last updated timestamp of the first bucket: ", bucket.get_last_updated()) -else: - print("No buckets were returned for this datetime") diff --git a/tests/test_identity_map_client.py b/tests/test_identity_map_client.py index 0d93fd5..609dd8b 100644 --- a/tests/test_identity_map_client.py +++ b/tests/test_identity_map_client.py @@ -135,7 +135,8 @@ def test_identity_map_hashed_phones(self): def test_identity_map_client_bad_url(self): identity_map_input = IdentityMapInput.from_emails( ["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"]) - client = IdentityMapClient("https://operator-bad-url.uidapi.com", os.getenv("UID2_API_KEY"), os.getenv("UID2_SECRET_KEY")) + client = IdentityMapClient("https://operator-bad-url.uidapi.com", os.getenv("UID2_API_KEY"), + os.getenv("UID2_SECRET_KEY")) self.assertRaises(requests.exceptions.ConnectionError, client.generate_identity_map, identity_map_input) self.assertRaises(requests.exceptions.ConnectionError, client.get_identity_buckets, datetime.datetime.now()) @@ -143,13 +144,14 @@ def test_identity_map_client_bad_api_key(self): identity_map_input = IdentityMapInput.from_emails( ["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"]) client = IdentityMapClient(os.getenv("UID2_BASE_URL"), "bad-api-key", os.getenv("UID2_SECRET_KEY")) - self.assertRaises(requests.exceptions.HTTPError, client.generate_identity_map,identity_map_input) + self.assertRaises(requests.exceptions.HTTPError, client.generate_identity_map, identity_map_input) self.assertRaises(requests.exceptions.HTTPError, client.get_identity_buckets, datetime.datetime.now()) def test_identity_map_client_bad_secret(self): identity_map_input = IdentityMapInput.from_emails( ["hopefully-not-opted-out@example.com", "somethingelse@example.com", "optout@example.com"]) - client = IdentityMapClient(os.getenv("UID2_BASE_URL"), os.getenv("UID2_API_KEY"), "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=") + client = IdentityMapClient(os.getenv("UID2_BASE_URL"), os.getenv("UID2_API_KEY"), + "wJ0hP19QU4hmpB64Y3fV2dAed8t/mupw3sjN5jNRFzg=") self.assertRaises(requests.exceptions.HTTPError, client.generate_identity_map, identity_map_input) self.assertRaises(requests.exceptions.HTTPError, client.get_identity_buckets, @@ -182,8 +184,16 @@ def test_identity_buckets_empty_response(self): self.assertTrue(response.is_success) def test_identity_buckets_invalid_timestamp(self): - self.assertRaises(TypeError, self.identity_map_client.get_identity_buckets, - "1234567890") + test_cases = ["1234567890", + 1234567890, + 2024.7, + "2024-7-1", + "2024-07-01T12:00:00", + [2024, 7, 1, 12, 0, 0], + None] + for timestamp in test_cases: + self.assertRaises(AttributeError, self.identity_map_client.get_identity_buckets, + timestamp) if __name__ == '__main__': diff --git a/uid2_client/Identity_buckets_response.py b/uid2_client/identity_buckets_response.py similarity index 100% rename from uid2_client/Identity_buckets_response.py rename to uid2_client/identity_buckets_response.py diff --git a/uid2_client/identity_map_client.py b/uid2_client/identity_map_client.py index 4911c2d..8a37d8a 100644 --- a/uid2_client/identity_map_client.py +++ b/uid2_client/identity_map_client.py @@ -3,7 +3,7 @@ import json from datetime import timezone -from .Identity_buckets_response import IdentityBucketsResponse +from .identity_buckets_response import IdentityBucketsResponse from .identity_map_response import IdentityMapResponse from uid2_client import auth_headers, make_v2_request, post, parse_v2_response, get_datetime_iso_format @@ -44,7 +44,7 @@ def generate_identity_map(self, identity_map_input): def get_identity_buckets(self, since_timestamp): req, nonce = make_v2_request(self._client_secret, dt.datetime.now(tz=timezone.utc), - json.dumps({"since_timestamp": get_datetime_iso_format(since_timestamp)}).encode()) + json.dumps({"since_timestamp": since_timestamp.isoformat()}).encode()) resp = post(self._base_url, '/v2/identity/buckets', headers=auth_headers(self._api_key), data=req) resp.raise_for_status() resp_body = parse_v2_response(self._client_secret, resp.text, nonce) diff --git a/uid2_client/input_util.py b/uid2_client/input_util.py index 0b1e0cc..b9d1c5e 100644 --- a/uid2_client/input_util.py +++ b/uid2_client/input_util.py @@ -119,12 +119,4 @@ def normalize_and_hash_email(email): def normalize_and_hash_phone(phone): if not is_phone_number_normalized(phone): raise ValueError("phone number is not normalized: " + phone) - return get_base64_encoded_hash(phone) - - -def get_datetime_iso_format(timestamp): - if isinstance(timestamp, datetime.datetime): - return timestamp.isoformat() - else: - raise TypeError("timestamp is not in datetime format") - + return get_base64_encoded_hash(phone) \ No newline at end of file