Skip to content

Commit

Permalink
[CAT-103] FIX: Support gzip deserialization (#116)
Browse files Browse the repository at this point in the history
* ADD: gzip as a valid MIME type

* REMOVE: gzip decompression check by extension
  • Loading branch information
thearchitector authored Jul 27, 2021
1 parent 9e8083e commit 09df0e7
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 23 deletions.
21 changes: 9 additions & 12 deletions indico/http/client.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import logging
import requests
import http.cookiejar
from pathlib import Path
from contextlib import contextmanager
import logging
from collections import defaultdict
from contextlib import contextmanager
from copy import deepcopy
from pathlib import Path
from typing import Union

import requests
from indico.client.request import HTTPRequest
from indico.config import IndicoConfig
from indico.errors import IndicoAuthenticationFailed, IndicoRequestError
from indico.http.serialization import deserialize
from indico.errors import IndicoRequestError, IndicoAuthenticationFailed
from indico.client.request import HTTPRequest
from requests import Response
from copy import deepcopy

logger = logging.getLogger(__file__)

Expand Down Expand Up @@ -128,14 +129,10 @@ def _make_request(
# code, api_response =
url_parts = path.split(".")
json = False
gzip = False
if len(url_parts) > 1 and (url_parts[-1] == "json" or url_parts[-2] == "json"):
json = True

if url_parts[-1] == "gz":
gzip = True

content = deserialize(response, force_json=json, gzip=gzip)
content = deserialize(response, force_json=json)

# If auth expired refresh
if response.status_code == 401 and not _refreshed:
Expand Down
15 changes: 8 additions & 7 deletions indico/http/serialization.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""
Handles deserialization / decoding of responses
"""
from collections import defaultdict
import cgi
import json
import io
import gzip
import io
import json
import logging
import msgpack
import traceback
from collections import defaultdict

import msgpack
from indico.errors import IndicoDecodingError

logger = logging.getLogger(__name__)
Expand All @@ -25,11 +25,10 @@ def raw_bytes(content, *args, **kwargs):
return content


def deserialize(response, gzip=False, force_json=False):
def deserialize(response, force_json=False):
content_type, params = cgi.parse_header(response.headers.get("Content-Type"))

content = None
if gzip:
if content_type in ["application/x-gzip", "application/gzip"]:
content = decompress(response)
else:
content = response.content
Expand Down Expand Up @@ -85,5 +84,7 @@ def zip_serialization(content, charset=None):
"image/png": image_serialization,
"image/jpg": image_serialization,
"application/zip": zip_serialization,
"application/x-gzip": raw_bytes,
"application/gzip": raw_bytes,
},
)
Binary file added tests/unit/http/mocked_data/application_gzip_
Binary file not shown.
15 changes: 11 additions & 4 deletions tests/unit/http/test_unit_serialization.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
from unittest.mock import MagicMock
from pathlib import Path
import logging
from pathlib import Path
from unittest.mock import MagicMock

import pytest

from indico.http.serialization import deserialize
from indico.errors import IndicoDecodingError
from indico.http.serialization import deserialize


@pytest.fixture(scope="function")
Expand All @@ -26,6 +25,7 @@ def _mock_loader(mime, charset):
with open(path, "rb") as f:
content = f.read()
response_mock.content = content
response_mock.raw.data = content
return response_mock

return _mock_loader
Expand Down Expand Up @@ -66,6 +66,13 @@ def test_deserialize_pdf(mock_loader):
assert isinstance(content, bytes)


def test_deserialize_gzip(mock_loader):
response = mock_loader("application/gzip", "")
content = deserialize(response)

assert isinstance(content, bytes)


def test_deserialize_unknown(mock_loader):
response = mock_loader("unknown", "")
try:
Expand Down

0 comments on commit 09df0e7

Please sign in to comment.