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

Handle seeing uncompressed sendgrid contact data #35343

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,13 @@ def download_data(self, url: str, chunk_size: int = 1024) -> tuple[str, str]:
tmp_file, "wb"
) as data_file:
for chunk in response.iter_content(chunk_size=chunk_size):
data_file.write(decompressor.decompress(chunk))
try:
# see if it's compressed. we are seeing some that are not all of a sudden.
# but let's also guard against the case where sendgrid changes it back.
data_file.write(decompressor.decompress(chunk))
except zlib.error as e:
# it's not actually compressed!
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we log a warning here to inform that an error has occurred?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For better or worse, this is the expected case I've seen at the moment. I would be curious how often it work and it doesn't work. What's the right way to log so I can see (datadog?) but not have it fill up the user-visible logs. Is that a thing?

data_file.write(chunk)
# check the file exists
if os.path.isfile(tmp_file):
return tmp_file, self.encoding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def test_should_retry_on_permission_error(requests_mock, stream_class, status, e

def test_compressed_contact_response(requests_mock):
stream = Contacts()
with open(os.path.dirname(__file__) + "/compressed_response", "rb") as compressed_response:
with open(os.path.dirname(__file__) + "/compressed_response", "rb") as file_response:
url = "https://api.sendgrid.com/v3/marketing/contacts/exports"
requests_mock.register_uri("POST", url, [{"json": {"id": "random_id"}, "status_code": 202}])
url = "https://api.sendgrid.com/v3/marketing/contacts/exports/random_id"
Expand All @@ -162,7 +162,28 @@ def test_compressed_contact_response(requests_mock):
{"json": {"status": "ready", "urls": ["https://sample_url/sample_csv.csv.gzip"]}, "status_code": 202},
]
requests_mock.register_uri("GET", url, resp_bodies)
requests_mock.register_uri("GET", "https://sample_url/sample_csv.csv.gzip", [{"body": compressed_response, "status_code": 202}])
requests_mock.register_uri("GET", "https://sample_url/sample_csv.csv.gzip", [{"body": file_response, "status_code": 202}])
recs = list(stream.read_records(sync_mode=SyncMode.full_refresh))
decompressed_response = pd.read_csv(os.path.dirname(__file__) + "/decompressed_response.csv", dtype=str)
expected_records = [
{k.lower(): v for k, v in x.items()} for x in decompressed_response.replace({nan: None}).to_dict(orient="records")
]

assert recs == expected_records


def test_uncompressed_contact_response(requests_mock):
stream = Contacts()
with open(os.path.dirname(__file__) + "/decompressed_response.csv", "rb") as file_response:
url = "https://api.sendgrid.com/v3/marketing/contacts/exports"
requests_mock.register_uri("POST", url, [{"json": {"id": "random_id"}, "status_code": 202}])
url = "https://api.sendgrid.com/v3/marketing/contacts/exports/random_id"
resp_bodies = [
{"json": {"status": "pending", "id": "random_id", "urls": []}, "status_code": 202},
{"json": {"status": "ready", "urls": ["https://sample_url/sample_csv.csv.gzip"]}, "status_code": 202},
]
requests_mock.register_uri("GET", url, resp_bodies)
requests_mock.register_uri("GET", "https://sample_url/sample_csv.csv.gzip", [{"body": file_response, "status_code": 202}])
recs = list(stream.read_records(sync_mode=SyncMode.full_refresh))
decompressed_response = pd.read_csv(os.path.dirname(__file__) + "/decompressed_response.csv", dtype=str)
expected_records = [
Expand Down
Loading