Skip to content

Commit

Permalink
Issue/386 encoding fixes (#443)
Browse files Browse the repository at this point in the history
* Fix rtype in request method

* Should fix #386 by avoiding call to response.text while logging as response may contain binary data

* Add option for File.get_contents to return bytes instead of a string

* Add test for a response that returns invalid unicode + run formatter

Co-authored-by: Matthew Emond <me@ucf.edu>
  • Loading branch information
blepabyte and Thetwam committed Dec 2, 2020
1 parent e74ca80 commit 315069c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 7 deletions.
10 changes: 7 additions & 3 deletions canvasapi/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ def download(self, location):
with open(location, "wb") as file_out:
file_out.write(response.content)

def get_contents(self):
def get_contents(self, binary=False):
"""
Download the contents of this file.
Pass binary=True to return a bytes object instead of a str.
:rtype: str
:rtype: str or bytes
"""
response = self._requester.request("GET", _url=self.url)
return response.text
if binary:
return response.content
else:
return response.text
13 changes: 9 additions & 4 deletions canvasapi/requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def request(
currently only the POST request of GraphQL is using this parameter.
For all other methods it's just passed and ignored.
:type json: `bool`
:rtype: str
:rtype: :class:`requests.Response`
"""
full_url = _url if _url else "{}{}".format(self.base_url, endpoint)

Expand Down Expand Up @@ -217,9 +217,14 @@ def request(
)

try:
logger.debug("Data: {data}".format(data=pformat(response.json())))
except ValueError:
logger.debug("Data: {data}".format(data=pformat(response.text)))
logger.debug(
"Data: {data}".format(data=pformat(response.content.decode("utf-8")))
)
except UnicodeDecodeError:
logger.debug("Data: {data}".format(data=pformat(response.content)))
except AttributeError:
# response.content is None
logger.debug("No data")

# Add response to internal cache
if len(self._cache) > 4:
Expand Down
2 changes: 2 additions & 0 deletions tests/test_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ def test_contents_file(self, m):
register_uris({"file": ["file_contents"]}, m)
contents = self.file.get_contents()
self.assertEqual(contents, '"Hello there"')
contents_binary = self.file.get_contents(binary=True)
self.assertEqual(contents_binary, b'"Hello there"')
12 changes: 12 additions & 0 deletions tests/test_requester.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,18 @@ def test_request_get(self, m):
response = self.requester.request("GET", "fake_get_request")
self.assertEqual(response.status_code, 200)

def test_request_get_binary(self, m):
m.register_uri(
"GET",
settings.BASE_URL_WITH_VERSION + "get_binary_data",
content=b"\xff\xff\xff",
status_code=200,
headers={},
)

response = self.requester.request("GET", "get_binary_data")
self.assertEqual(response.content, b"\xff\xff\xff")

def test_request_get_datetime(self, m):
date = datetime.today()

Expand Down

0 comments on commit 315069c

Please sign in to comment.