Skip to content

Commit 1c1a21e

Browse files
committed
Fix canvas API issue when downloading file contents
see ucfopen/canvasapi#687
1 parent 02d6ae5 commit 1c1a21e

File tree

3 files changed

+36
-3
lines changed

3 files changed

+36
-3
lines changed

canvas_langchain/canvas.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -560,6 +560,39 @@ def load(self) -> List[Document]:
560560
try:
561561
# Import the Canvas class
562562
from canvasapi import Canvas
563+
564+
# see: https://github.com/ucfopen/canvasapi/issues/687
565+
from canvasapi.file import File
566+
567+
def patched_download(self, location):
568+
"""
569+
Download the file to specified location.
570+
571+
:param location: The path to download to.
572+
:type location: str
573+
"""
574+
response = self._requester.request("GET", _url=self.url, use_auth=False)
575+
576+
with open(location, "wb") as file_out:
577+
file_out.write(response.content)
578+
579+
580+
def patched_get_contents(self, binary=False):
581+
"""
582+
Download the contents of this file.
583+
Pass binary=True to return a bytes object instead of a str.
584+
585+
:rtype: str or bytes
586+
"""
587+
response = self._requester.request("GET", _url=self.url, use_auth=False)
588+
if binary:
589+
return response.content
590+
else:
591+
return response.text
592+
593+
File.get_contents = patched_get_contents
594+
File.download = patched_download
595+
563596
from canvasapi.exceptions import CanvasException
564597
except ImportError as exc:
565598
raise ImportError(

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
langchain
22
langchain-community
33
unstructured[docx,pptx,xlsx]
4-
canvasapi
4+
canvasapi=3.3.0
55
beautifulsoup4
66
lxml
77
PyPDF2[crypto]

setup.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
setup(
44
name="canvas_langchain",
5-
version="0.9.17",
5+
version="0.9.18",
66
description="A canvas langchain integration",
77
long_description=open("README.md").read(),
88
long_description_content_type="text/markdown",
@@ -18,7 +18,7 @@
1818
"langchain",
1919
"langchain-community",
2020
"unstructured[docx,pptx]",
21-
"canvasapi",
21+
"canvasapi=3.3.0",
2222
"beautifulsoup4",
2323
"lxml",
2424
"PyPDF2[crypto]",

0 commit comments

Comments
 (0)