Skip to content

Commit 2750653

Browse files
author
AndyEveritt
committed
add get_directory
1 parent b6bebe7 commit 2750653

1 file changed

Lines changed: 21 additions & 13 deletions

File tree

DuetWebAPI/DuetWebAPI.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# Released under The MIT License. Full text available via https://opensource.org/licenses/MIT
1313
#
1414
# Requires Python3
15-
from typing import Dict
15+
from typing import Dict, List
1616
import requests
1717
import json
1818
import sys
@@ -86,15 +86,15 @@ def post_code(self, code) -> Dict:
8686
raise ValueError
8787
return json.loads(r.text)
8888

89-
def get_file(self, filename: str, path: str = 'gcodes') -> str:
89+
def get_file(self, filename: str, directory: str = 'gcodes') -> str:
9090
"""
9191
filename: name of the file you want to download including extension
92-
path: the folder that the file is in, options are ['gcodes', 'macros', 'sys']
92+
directory: the folder that the file is in, options are ['gcodes', 'macros', 'sys']
9393
9494
returns the file as a string
9595
"""
9696
url = f'{self._base_url}/rr_download'
97-
r = requests.get(url, {'name': f'/{path}/{filename}'})
97+
r = requests.get(url, {'name': f'/{directory}/{filename}'})
9898
if not r.ok:
9999
raise ValueError
100100
return r.text
@@ -111,8 +111,12 @@ def delete_file(self, filename):
111111
def move_file(self, from_path, to_path, force=False):
112112
pass
113113

114-
def get_directory(self, directory):
115-
pass
114+
def get_directory(self, directory) -> List[Dict]:
115+
url = f'{self._base_url}/rr_filelist'
116+
r = requests.get(url, {'dir': f'/{directory}'})
117+
if not r.ok:
118+
raise ValueError
119+
return json.loads(r.text)['files']
116120

117121
def put_directory(self, directory):
118122
pass
@@ -130,14 +134,14 @@ def post_code(self, code) -> str:
130134
r = requests.post(url, data=code)
131135
return r.text
132136

133-
def get_file(self, filename, path='gcodes'):
137+
def get_file(self, filename, directory):
134138
"""
135139
filename: name of the file you want to download including extension
136-
path: the folder that the file is in, options are ['gcodes', 'macros', 'sys']
140+
directory: the folder that the file is in, options are ['gcodes', 'macros', 'sys']
137141
138142
returns the file as a string
139143
"""
140-
url = f'{self._base_url}/machine/file/{path}/{filename}'
144+
url = f'{self._base_url}/machine/file/{directory}'
141145
r = requests.get(url)
142146
if not r.ok:
143147
raise ValueError
@@ -155,8 +159,12 @@ def delete_file(self, filename):
155159
def move_file(self, from_path, to_path, force=False):
156160
pass
157161

158-
def get_directory(self, directory):
159-
pass
162+
def get_directory(self, directory) -> List[Dict]:
163+
url = f'{self._base_url}/machine/directory/{directory}'
164+
r = requests.get(url)
165+
if not r.ok:
166+
raise ValueError
167+
return json.loads(r.text)
160168

161169
def put_directory(self, directory):
162170
pass
@@ -169,6 +177,6 @@ def put_directory(self, directory):
169177

170178
riley = factory.get_api('http://riley')
171179
force_rig = factory.get_api('http://forcerig')
172-
force_rig.get_file('1001.gcode')
173-
riley.get_file('winder.gcode')
180+
force_rig.get_directory('sys')
181+
riley.get_directory('sys')
174182
pass

0 commit comments

Comments
 (0)