Skip to content

Commit 6bcc26a

Browse files
committed
Add binary download mode
1 parent 1e258ce commit 6bcc26a

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

duetwebapi/api/dsf_api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,24 @@ def send_code(self, code: str) -> Dict:
2828
r = requests.post(url, data=code)
2929
return {'response': r.text}
3030

31-
def get_file(self, filename: str, directory: str = 'gcodes') -> str:
31+
def get_file(self, filename: str, directory: str = 'gcodes', binary: bool = False) -> str:
3232
"""
3333
filename: name of the file you want to download including extension
3434
directory: the folder that the file is in, options are ['gcodes', 'macros', 'sys']
35+
binary: return binary data instead of a string
3536
36-
returns the file as a string
37+
returns the file as a string or binary data
3738
"""
3839
url = f'{self.base_url}/machine/file/{directory}/{filename}'
3940
r = requests.get(url)
4041
if not r.ok:
4142
raise ValueError
42-
return r.text
43+
if binary:
44+
return r.content
45+
else:
46+
return r.text
4347

44-
def upload_file(self, file: Union[StringIO, TextIOWrapper], filename: str, directory: str = 'gcodes') -> Dict:
48+
def upload_file(self, file: Union[StringIO, TextIOWrapper, bytes], filename: str, directory: str = 'gcodes') -> Dict:
4549
"""
4650
file: the path to the file you want to upload from your PC
4751
directory: the folder that the file is in, options are ['gcodes', 'macros', 'sys']

duetwebapi/api/dwc_api.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,24 @@ def send_code(self, code: str) -> Dict:
5656
reply = self._get_reply()
5757
return {'response': reply}
5858

59-
def get_file(self, filename: str, directory: str = 'gcodes') -> str:
59+
def get_file(self, filename: str, directory: str = 'gcodes', binary: bool = False) -> str:
6060
"""
6161
filename: name of the file you want to download including extension
6262
directory: the folder that the file is in, options are ['gcodes', 'macros', 'sys']
63+
binary: return binary data instead of a string
6364
64-
returns the file as a string
65+
returns the file as a string or binary data
6566
"""
6667
url = f'{self.base_url}/rr_download'
6768
r = requests.get(url, {'name': f'/{directory}/{filename}'})
6869
if not r.ok:
6970
raise ValueError
70-
return r.text
71+
if binary:
72+
return r.content
73+
else:
74+
return r.text
7175

72-
def upload_file(self, file: Union[StringIO, TextIOWrapper], filename: str, directory: str = 'gcodes') -> Dict:
76+
def upload_file(self, file: Union[StringIO, TextIOWrapper, bytes], filename: str, directory: str = 'gcodes') -> Dict:
7377
url = f'{self.base_url}/rr_upload?name=/{directory}/{filename}'
7478
r = requests.post(url, data=file)
7579
if not r.ok:

0 commit comments

Comments
 (0)