Skip to content

Commit 100ec8a

Browse files
committed
Address the comments
1 parent 87044ac commit 100ec8a

File tree

4 files changed

+33
-16
lines changed

4 files changed

+33
-16
lines changed
Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,31 @@
1-
import datetime
21
import sys
2+
from datetime import datetime
33

44
from uid2_client import IdentityMapClient
55

66

77
# this sample client takes date time as input and generates an IdentityBucketsResponse object which contains
8-
# a list of buckets
8+
# a list of buckets, the timestamp strings in the format YYYY-MM-DD[*HH[:MM[:SS[.fff[fff]]]][+HH:MM[:SS[.ffffff]]]],
9+
# for example: UTC: 2024-07-02, 2024-07-02T14:30:15.123456+00:00 and EST: 2024-07-02T14:30:15.123456-05:00
910

1011
def _usage():
11-
print('Usage: python3 sample_get_identity_buckets.py <base_url> <api_key> <client_secret> <year> <month> <day> <hour> <minute> <second>'
12+
print('Usage: python3 sample_get_identity_buckets.py <base_url> <api_key> <client_secret> <timestamp>'
1213
, file=sys.stderr)
1314
sys.exit(1)
1415

1516

16-
if len(sys.argv) <= 9:
17+
if len(sys.argv) <= 4:
1718
_usage()
1819

1920
base_url = sys.argv[1]
2021
api_key = sys.argv[2]
2122
client_secret = sys.argv[3]
22-
year = int(sys.argv[4])
23-
month = int(sys.argv[5])
24-
day = int(sys.argv[6])
25-
hour = int(sys.argv[7])
26-
minute = int(sys.argv[8])
27-
second = int(sys.argv[9])
23+
timestamp = sys.argv[4]
2824

2925
client = IdentityMapClient(base_url, api_key, client_secret)
3026

31-
identity_buckets_response = client.get_identity_buckets(datetime.datetime(year, month, day, hour, minute, second))
27+
identity_buckets_response = client.get_identity_buckets(datetime.fromisoformat(timestamp))
3228

3329
for bucket in identity_buckets_response.buckets:
34-
print("The bucket id of the first bucket: ", bucket.get_bucket_id())
35-
print("The last updated timestamp of the first bucket: ", bucket.get_last_updated())
30+
print("The bucket id of the bucket: ", bucket.get_bucket_id())
31+
print("The last updated timestamp of the bucket: ", bucket.get_last_updated())

tests/test_identity_map_client.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import datetime
22
import os
33
import unittest
4+
from datetime import datetime
45

56
import requests
67

7-
from uid2_client import IdentityMapClient, IdentityMapInput, normalize_and_hash_email, normalize_and_hash_phone
8+
from uid2_client import IdentityMapClient, IdentityMapInput, normalize_and_hash_email, normalize_and_hash_phone, \
9+
get_datetime_utc_iso_format
810

911

1012
class IdentityMapIntegrationTests(unittest.TestCase):
@@ -195,6 +197,17 @@ def test_identity_buckets_invalid_timestamp(self):
195197
self.assertRaises(AttributeError, self.identity_map_client.get_identity_buckets,
196198
timestamp)
197199

200+
def test_get_datetime_utc_iso_format_timestamp(self):
201+
expected_timestamp = "2024-07-02T14:30:15.123456"
202+
test_cases = ["2024-07-02T14:30:15.123456+00:00", "2024-07-02 09:30:15.123456-05:00",
203+
"2024-07-02T08:30:15.123456-06:00", "2024-07-02T10:30:15.123456-04:00",
204+
"2024-07-02T06:30:15.123456-08:00", "2024-07-02T23:30:15.123456+09:00",
205+
"2024-07-03T00:30:15.123456+10:00", "2024-07-02T20:00:15.123456+05:30"]
206+
for timestamp_str in test_cases:
207+
timestamp = datetime.fromisoformat(timestamp_str)
208+
iso_format_timestamp = get_datetime_utc_iso_format(timestamp)
209+
self.assertEqual(expected_timestamp, iso_format_timestamp)
210+
198211

199212
if __name__ == '__main__':
200213
unittest.main()

uid2_client/identity_map_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from .identity_buckets_response import IdentityBucketsResponse
77
from .identity_map_response import IdentityMapResponse
88

9-
from uid2_client import auth_headers, make_v2_request, post, parse_v2_response
9+
from uid2_client import auth_headers, make_v2_request, post, parse_v2_response, get_datetime_utc_iso_format
1010

1111

1212
class IdentityMapClient:
@@ -44,7 +44,7 @@ def generate_identity_map(self, identity_map_input):
4444

4545
def get_identity_buckets(self, since_timestamp):
4646
req, nonce = make_v2_request(self._client_secret, dt.datetime.now(tz=timezone.utc),
47-
json.dumps({"since_timestamp": since_timestamp.isoformat()}).encode())
47+
json.dumps({"since_timestamp": get_datetime_utc_iso_format(since_timestamp)}).encode())
4848
resp = post(self._base_url, '/v2/identity/buckets', headers=auth_headers(self._api_key), data=req)
4949
resp.raise_for_status()
5050
resp_body = parse_v2_response(self._client_secret, resp.text, nonce)

uid2_client/input_util.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import hashlib
22
import base64
33

4+
import pytz
5+
46

57
def is_phone_number_normalized(phone_number):
68
min_phone_number_digits = 10
@@ -119,3 +121,9 @@ def normalize_and_hash_phone(phone):
119121
if not is_phone_number_normalized(phone):
120122
raise ValueError("phone number is not normalized: " + phone)
121123
return get_base64_encoded_hash(phone)
124+
125+
126+
def get_datetime_utc_iso_format(timestamp):
127+
dt_utc = timestamp.astimezone(pytz.utc)
128+
dt_utc_without_tz = dt_utc.replace(tzinfo=None)
129+
return dt_utc_without_tz.isoformat()

0 commit comments

Comments
 (0)