|
| 1 | +import logging |
| 2 | +import os |
| 3 | +from typing import Dict, List |
| 4 | + |
| 5 | +import requests |
| 6 | + |
| 7 | +from .base import DuetAPI |
| 8 | + |
| 9 | + |
| 10 | +class DSFAPI(DuetAPI): |
| 11 | + """ |
| 12 | + Duet Software Framework REST API Interface. |
| 13 | +
|
| 14 | + Used with a Duet 3 + SBC. |
| 15 | + Must use RRF3. |
| 16 | + """ |
| 17 | + api_name = 'DSF_REST' |
| 18 | + |
| 19 | + def get_model(self, **_ignored) -> Dict: |
| 20 | + url = f'{self._base_url}/machine/status' |
| 21 | + r = requests.get(url) |
| 22 | + j = r.json() |
| 23 | + return j |
| 24 | + |
| 25 | + def post_code(self, code: str) -> Dict: |
| 26 | + url = f'{self._base_url}/machine/code' |
| 27 | + r = requests.post(url, data=code) |
| 28 | + return {'response': r.text} |
| 29 | + |
| 30 | + def get_file(self, filename: str, directory: str = 'gcodes') -> str: |
| 31 | + """ |
| 32 | + filename: name of the file you want to download including extension |
| 33 | + directory: the folder that the file is in, options are ['gcodes', 'macros', 'sys'] |
| 34 | +
|
| 35 | + returns the file as a string |
| 36 | + """ |
| 37 | + url = f'{self._base_url}/machine/file/{directory}/{filename}' |
| 38 | + r = requests.get(url) |
| 39 | + if not r.ok: |
| 40 | + raise ValueError |
| 41 | + return r.text |
| 42 | + |
| 43 | + def put_file(self, file: str, directory: str = 'gcodes') -> Dict: |
| 44 | + """ |
| 45 | + file: the path to the file you want to upload from your PC |
| 46 | + directory: the folder that the file is in, options are ['gcodes', 'macros', 'sys'] |
| 47 | +
|
| 48 | + returns the file as a string |
| 49 | + """ |
| 50 | + file = os.path.abspath(file).replace('\\', '/') |
| 51 | + filename = file.split('/')[-1] |
| 52 | + url = f'{self._base_url}/machine/file/{directory}/{filename}' |
| 53 | + with open(file, 'rb') as f: |
| 54 | + r = requests.put(url, data=f, headers={'Content-Type': 'application/octet-stream'}) |
| 55 | + if not r.ok: |
| 56 | + raise ValueError |
| 57 | + return {'err': 0} |
| 58 | + |
| 59 | + def get_fileinfo(self, filename: str = None, directory: str = 'gcodes') -> Dict: |
| 60 | + url = f'{self._base_url}/machine/fileinfo/{directory}/{filename}' |
| 61 | + r = requests.get(url) |
| 62 | + if not r.ok: |
| 63 | + raise ValueError |
| 64 | + return r.json() |
| 65 | + |
| 66 | + def delete_file(self, filename: str, directory: str = 'gcodes') -> Dict: |
| 67 | + url = f'{self._base_url}/machine/file/{directory}/{filename}' |
| 68 | + r = requests.delete(url) |
| 69 | + if not r.ok: |
| 70 | + raise ValueError |
| 71 | + return {'err': 0} |
| 72 | + |
| 73 | + def move_file(self, from_path: str, to_path: str, force: bool = False) -> Dict: |
| 74 | + url = f'{self._base_url}/machine/file/move' |
| 75 | + r = requests.post(url, {'from': f'{from_path}', 'to': f'{to_path}', 'force': force}) |
| 76 | + if not r.ok: |
| 77 | + raise ValueError |
| 78 | + return {'err': 0} |
| 79 | + |
| 80 | + def get_directory(self, directory: str) -> List[Dict]: |
| 81 | + url = f'{self._base_url}/machine/directory/{directory}' |
| 82 | + r = requests.get(url) |
| 83 | + if not r.ok: |
| 84 | + raise ValueError |
| 85 | + return r.json() |
| 86 | + |
| 87 | + def put_directory(self, directory: str) -> Dict: |
| 88 | + url = f'{self._base_url}/machine/directory/{directory}' |
| 89 | + r = requests.put(url) |
| 90 | + if not r.ok: |
| 91 | + raise ValueError |
| 92 | + return {'err': 0} |
0 commit comments