Skip to content

Commit 12ce257

Browse files
committed
added qty to POST /printjobs, added pagination to GET /computers, GET /printers, GET /printjobs and GET /printjobs/states
1 parent ccd5871 commit 12ce257

File tree

1 file changed

+57
-10
lines changed

1 file changed

+57
-10
lines changed

printnodeapi/computers.py

+57-10
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,25 @@ def __init__(self, auth, factory):
1111
self._auth = auth
1212
self._factory = factory
1313

14-
def get_computers(self, computer=None):
14+
def get_computers(self, computer=None, limit=None, after=None, dir=None):
15+
16+
params = self._create_pagination_params(limit, after, dir)
17+
1518
if self._is_multi_query(computer):
16-
results = self._auth.get('/computers')
17-
computers = self._factory.create_computers(results)
19+
url = '/computers'
20+
if params is not None:
21+
url = url + '?' + params
22+
computers = self._factory.create_computers(self._auth.get(url))
1823
if isinstance(computer, str):
1924
return [c for c in computers if c.name == computer]
2025
else:
2126
return computers
2227
else:
2328
computer_id = self._get_computer_id(computer)
24-
results = self._auth.get('/computers/{}'.format(computer_id))
29+
url = '/computers/{}'.format(computer_id)
30+
if params is not None:
31+
url = url + '?' + params
32+
results = self._auth.get(url)
2533
if len(results) == 0:
2634
raise LookupError(
2735
'computer not found with ID {}'.format(computer_id))
@@ -41,15 +49,19 @@ def get_scales(self, computer, dev_name=None, dev_num=None):
4149
scales = self._factory.create_scales(self._auth.get(url))
4250
return scales
4351

44-
def get_states(self, pjob_set=None):
52+
def get_states(self, pjob_set=None, limit=None, after=None, dir=None):
4553

4654
pjob_set = str(pjob_set)+"/" if pjob_set else ""
55+
url = "/printjobs/"+pjob_set+"states"
56+
params = self._create_pagination_params(limit, after, dir)
57+
if params is not None:
58+
url = url + '?' + params
4759
states = self._factory.create_states_map(
48-
self._auth.get("/printjobs/"+pjob_set+"states"))
60+
self._auth.get(url))
4961

5062
return states
5163

52-
def get_printers(self, computer=None, printer=None):
64+
def get_printers(self, computer=None, printer=None, limit=None, after=None, dir=None):
5365
"""queries API for printers.
5466
the printer argument can be:
5567
* id of the printer, in which case a single printer is returned
@@ -64,9 +76,13 @@ def get_printers(self, computer=None, printer=None):
6476
that doesn't exist
6577
"""
6678

79+
params = self._create_pagination_params(limit, after, dir)
80+
6781
if self._is_multi_query(printer):
6882
computer_ids = ','.join(map(str, self._get_computer_ids(computer)))
6983
url = '/computers/{}/printers'.format(computer_ids)
84+
if params is not None:
85+
url = url + '?' + params
7086
printers = self._factory.create_printers(self._auth.get(url))
7187
assert all(isinstance(p, Printer) for p in printers)
7288
if isinstance(printer, str):
@@ -75,17 +91,23 @@ def get_printers(self, computer=None, printer=None):
7591
return printers
7692
else:
7793
printer_id = self._get_printer_id(printer)
78-
results = self._auth.get('/printers/{}'.format(printer_id))
79-
printers = self._factory.create_printers(results)
94+
url = '/printers/{}'.format(printer_id)
95+
if params is not None:
96+
url = url + '?' + params
97+
printers = self._factory.create_printers(self._auth.get(url))
8098
assert all(isinstance(p, Printer) for p in printers)
8199
if len(printers) == 0:
82100
raise LookupError('no printer with ID {}'.format(printer_id))
83101
return printers[0]
84102

85-
def get_printjobs(self, computer=None, printer=None, printjob=None):
103+
def get_printjobs(self, computer=None, printer=None, printjob=None, limit=None, after=None, dir=None):
104+
params = self._create_pagination_params(limit, after, dir)
105+
86106
if self._is_multi_query(printjob):
87107
if computer is None and printer is None:
88108
url = '/printjobs'
109+
if params is not None:
110+
url = url + '?' + params
89111
printjobs = self._factory.create_printjobs(self._auth.get(url))
90112
if isinstance(printjob, str):
91113
return [pj for pj in printjobs if pj.title == printjob]
@@ -103,6 +125,8 @@ def get_printjobs(self, computer=None, printer=None, printjob=None):
103125
printer_ids = [p.id for p in printers]
104126
printer_url = ','.join(map(str, printer_ids))
105127
url = '/printers/{}/printjobs'.format(printer_url)
128+
if params is not None:
129+
url = url + '?' + params
106130
printjobs = self._factory.create_printjobs(self._auth.get(url))
107131

108132
if isinstance(printjob, str):
@@ -124,6 +148,7 @@ def submit_printjob(
124148
printer=None,
125149
job_type='pdf', # PDF|RAW
126150
title='PrintJob',
151+
qty=None,
127152
options=None,
128153
authentication=None,
129154
uri=None,
@@ -173,6 +198,9 @@ def submit_printjob(
173198
if options is not None:
174199
printjob_data.update({"options": options})
175200

201+
if qty is not None:
202+
printjob_data.update({"qty": qty})
203+
176204
printjob_id = self._auth.post('/printjobs', printjob_data)
177205

178206
return printjob_id
@@ -255,6 +283,25 @@ def _get_printer_by_name(self, printer_name, computer_id=None):
255283
for printer in printers
256284
if printer.name == printer_name]
257285

286+
def _create_pagination_params(self, limit, after, dir):
287+
params = []
288+
if limit is not None:
289+
if isinstance(limit, int):
290+
params.append('limit={}'.format(limit))
291+
else:
292+
raise TypeError('limit: "{}"'.format(type(limit)))
293+
if after is not None:
294+
if isinstance(after, int):
295+
params.append('after={}'.format(after))
296+
else:
297+
raise TypeError('after: "{}"'.format(type(after)))
298+
if dir is not None:
299+
if dir == 'asc' or dir == 'desc':
300+
params.append('dir={}'.format(dir))
301+
else:
302+
raise TypeError("dir must equal 'asc' or 'desc'")
303+
if len(params) > 0:
304+
return '&'.join(params)
258305

259306
class LookupFailedError(RuntimeError):
260307

0 commit comments

Comments
 (0)