@@ -11,17 +11,25 @@ def __init__(self, auth, factory):
11
11
self ._auth = auth
12
12
self ._factory = factory
13
13
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
+
15
18
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 ))
18
23
if isinstance (computer , str ):
19
24
return [c for c in computers if c .name == computer ]
20
25
else :
21
26
return computers
22
27
else :
23
28
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 )
25
33
if len (results ) == 0 :
26
34
raise LookupError (
27
35
'computer not found with ID {}' .format (computer_id ))
@@ -41,15 +49,19 @@ def get_scales(self, computer, dev_name=None, dev_num=None):
41
49
scales = self ._factory .create_scales (self ._auth .get (url ))
42
50
return scales
43
51
44
- def get_states (self , pjob_set = None ):
52
+ def get_states (self , pjob_set = None , limit = None , after = None , dir = None ):
45
53
46
54
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
47
59
states = self ._factory .create_states_map (
48
- self ._auth .get ("/printjobs/" + pjob_set + "states" ))
60
+ self ._auth .get (url ))
49
61
50
62
return states
51
63
52
- def get_printers (self , computer = None , printer = None ):
64
+ def get_printers (self , computer = None , printer = None , limit = None , after = None , dir = None ):
53
65
"""queries API for printers.
54
66
the printer argument can be:
55
67
* id of the printer, in which case a single printer is returned
@@ -64,9 +76,13 @@ def get_printers(self, computer=None, printer=None):
64
76
that doesn't exist
65
77
"""
66
78
79
+ params = self ._create_pagination_params (limit , after , dir )
80
+
67
81
if self ._is_multi_query (printer ):
68
82
computer_ids = ',' .join (map (str , self ._get_computer_ids (computer )))
69
83
url = '/computers/{}/printers' .format (computer_ids )
84
+ if params is not None :
85
+ url = url + '?' + params
70
86
printers = self ._factory .create_printers (self ._auth .get (url ))
71
87
assert all (isinstance (p , Printer ) for p in printers )
72
88
if isinstance (printer , str ):
@@ -75,17 +91,23 @@ def get_printers(self, computer=None, printer=None):
75
91
return printers
76
92
else :
77
93
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 ))
80
98
assert all (isinstance (p , Printer ) for p in printers )
81
99
if len (printers ) == 0 :
82
100
raise LookupError ('no printer with ID {}' .format (printer_id ))
83
101
return printers [0 ]
84
102
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
+
86
106
if self ._is_multi_query (printjob ):
87
107
if computer is None and printer is None :
88
108
url = '/printjobs'
109
+ if params is not None :
110
+ url = url + '?' + params
89
111
printjobs = self ._factory .create_printjobs (self ._auth .get (url ))
90
112
if isinstance (printjob , str ):
91
113
return [pj for pj in printjobs if pj .title == printjob ]
@@ -103,6 +125,8 @@ def get_printjobs(self, computer=None, printer=None, printjob=None):
103
125
printer_ids = [p .id for p in printers ]
104
126
printer_url = ',' .join (map (str , printer_ids ))
105
127
url = '/printers/{}/printjobs' .format (printer_url )
128
+ if params is not None :
129
+ url = url + '?' + params
106
130
printjobs = self ._factory .create_printjobs (self ._auth .get (url ))
107
131
108
132
if isinstance (printjob , str ):
@@ -124,6 +148,7 @@ def submit_printjob(
124
148
printer = None ,
125
149
job_type = 'pdf' , # PDF|RAW
126
150
title = 'PrintJob' ,
151
+ qty = None ,
127
152
options = None ,
128
153
authentication = None ,
129
154
uri = None ,
@@ -173,6 +198,9 @@ def submit_printjob(
173
198
if options is not None :
174
199
printjob_data .update ({"options" : options })
175
200
201
+ if qty is not None :
202
+ printjob_data .update ({"qty" : qty })
203
+
176
204
printjob_id = self ._auth .post ('/printjobs' , printjob_data )
177
205
178
206
return printjob_id
@@ -255,6 +283,25 @@ def _get_printer_by_name(self, printer_name, computer_id=None):
255
283
for printer in printers
256
284
if printer .name == printer_name ]
257
285
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 )
258
305
259
306
class LookupFailedError (RuntimeError ):
260
307
0 commit comments