Skip to content

Commit 455dd11

Browse files
authored
Merge pull request #6 from AndyEveritt/raw
Raw
2 parents 1e258ce + 02a2f4b commit 455dd11

5 files changed

Lines changed: 21 additions & 15 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Method | Description
3434
------ | -----------
3535
`get_model(key: str = None) -> Dict` | Get Duet object model. RRF3 only
3636
`send_code(code: str) -> Dict` | Send G/M/T-code to Duet
37-
`get_file(filename: str, directory: str = 'gcodes') -> str` | Download file from Duet
38-
`upload_file(file: StringIO \| TextIOWrapper, filename: str, directory: str = 'gcodes') -> Dict` | Upload file to Duet
37+
`get_file(filename: str, directory: str = 'gcodes', binary: bool = False) -> str` | Download file from Duet
38+
`upload_file(file: str \| bytes \| StringIO \| TextIOWrapper \| BytesIO, filename: str, directory: str = 'gcodes') -> Dict` | Upload file to Duet
3939
`get_fileinfo(filename: str = None, directory: str = 'gcodes') -> Dict` | Get file info
4040
`delete_file(filename: str, directory: str = 'gcodes') -> Dict` | Delete file on Duet
4141
`move_file(from_path: str, to_path: str, force: bool = False) -> Dict` | Move file on Duet, can be used to rename files

duetwebapi/api/base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ def send_code(self, code: str) -> Dict:
3737
""" Send G/M/T-code to Duet """
3838
raise NotImplementedError
3939

40-
def get_file(self, filename: str, directory: str = 'gcodes') -> str:
40+
def get_file(self, filename: str, directory: str = 'gcodes', binary: bool = False) -> str:
4141
""" Get file from Duet """
4242
raise NotImplementedError
4343

44-
def upload_file(self, file: Union[StringIO, TextIOWrapper], filename: str, directory: str = 'gcodes') -> Dict:
44+
def upload_file(self, file: Union[str, bytes, StringIO, TextIOWrapper, BytesIO], filename: str, directory: str = 'gcodes') -> Dict:
4545
""" Upload file to Duet """
4646
raise NotImplementedError
4747

duetwebapi/api/dsf_api.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,27 @@ 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[str, bytes, StringIO, TextIOWrapper, BytesIO], 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']
48-
49-
returns the file as a string
5052
"""
5153
url = f'{self.base_url}/machine/file/{directory}/{filename}'
5254
r = requests.put(url, data=file, headers={'Content-Type': 'application/octet-stream'})

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[str, bytes, StringIO, TextIOWrapper, BytesIO], 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:

requirements_dev.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
autopep8==1.5.4
2-
bleach==3.2.1
2+
bleach==3.3.0
33
bumpversion
44
certifi==2020.6.20
55
chardet==3.0.4

0 commit comments

Comments
 (0)