diff --git a/canvasapi/file.py b/canvasapi/file.py index ea68a52d..81439f6a 100644 --- a/canvasapi/file.py +++ b/canvasapi/file.py @@ -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 diff --git a/canvasapi/requester.py b/canvasapi/requester.py index fbdfee35..2d236ce2 100644 --- a/canvasapi/requester.py +++ b/canvasapi/requester.py @@ -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) @@ -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: diff --git a/tests/test_file.py b/tests/test_file.py index 97048a5f..ed7a5693 100644 --- a/tests/test_file.py +++ b/tests/test_file.py @@ -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"') diff --git a/tests/test_requester.py b/tests/test_requester.py index cf85bc8a..df59eb99 100644 --- a/tests/test_requester.py +++ b/tests/test_requester.py @@ -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()