Skip to content

Commit 68cdc1c

Browse files
fixed pagination and removed credits
1 parent a6add22 commit 68cdc1c

File tree

2 files changed

+57
-58
lines changed

2 files changed

+57
-58
lines changed

src/PrintNode/Client.php

Lines changed: 53 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -106,31 +106,17 @@ public function viewWhoAmI()
106106

107107
}
108108

109-
/**
110-
* Makes a credits request to the PrintNode API, returning the credit
111-
* balance for the active account as a string
112-
*
113-
* @return string
114-
*/
115-
public function viewCredits()
116-
{
117-
118-
$this->lastResponse = $this->makeRequest('credits', 'GET');
119-
120-
return $this->lastResponse->bodyJson;
121-
122-
}
123-
124109
/**
125110
* Makes a computers request to the PrintNode API, returning an array
126111
* of all the registered computers on the active account.
127112
*
128-
* @param int $offset (Optional) The start index for the records the API should return
129113
* @param int $limit (Optional) The number of records the API should return
114+
* @param int $after (Optional) The computer id after (or before, depending on $dir) which to start returning records
115+
* @param string $dir (Optional) "asc" for ascending or "desc" for descending, ordered by computer id
130116
* @param string|array $computerSet (Optional) 'set' string or array of computer ids to which the response should be limited
131117
* @return \PrintNode\Entity\Computer[]
132118
*/
133-
public function viewComputers($offset = 0, $limit = 500, $computerSet = null)
119+
public function viewComputers($limit = null, $after = null, $dir = null, $computerSet = null)
134120
{
135121

136122
if (isset($computerSet)) {
@@ -139,7 +125,7 @@ public function viewComputers($offset = 0, $limit = 500, $computerSet = null)
139125
$url = 'computers';
140126
}
141127

142-
$url = $this->applyLimitOffsetToUrl($url, $offset, $limit);
128+
$url = $this->applyPaginationToUrl($url, $limit, $after, $dir);
143129

144130
return $this->makeRequestMapped($url, 'GET', '\PrintNode\Entity\Computer');
145131

@@ -158,13 +144,14 @@ public function viewComputers($offset = 0, $limit = 500, $computerSet = null)
158144
* Both arguments can be combined to return only certain printers on certain
159145
* computers
160146
*
161-
* @param int $offset (Optional) The start index for the records the API should return
162147
* @param int $limit (Optional) The number of records the API should return
148+
* @param int $after (Optional) The id after (or before, depending on $dir) which to start returning records
149+
* @param string $dir (Optional) "asc" for ascending or "desc" for descending, ordered by computer id
163150
* @param string|array $printerSet (Optional) 'set' string or array of printer ids to which the response should be limited
164151
* @param string|array $computerSet (Optional) 'set' string or array of computer ids to which the response should be limited
165152
* @return \PrintNode\Entity\Printer[]
166153
*/
167-
public function viewPrinters($offset = 0, $limit = 500, $printerSet = null, $computerSet = null)
154+
public function viewPrinters($limit = null, $after = null, $dir = null, $printerSet = null, $computerSet = null)
168155
{
169156

170157
if (isset($computerSet) && isset($printerSet)){
@@ -177,7 +164,7 @@ public function viewPrinters($offset = 0, $limit = 500, $printerSet = null, $com
177164
$url = 'printers';
178165
}
179166

180-
$url = $this->applyLimitOffsetToUrl($url, $offset, $limit);
167+
$url = $this->applyPaginationToUrl($url, $limit, $after, $dir);
181168

182169
return $this->makeRequestMapped($url, 'GET', '\PrintNode\Entity\Printer');
183170

@@ -196,13 +183,14 @@ public function viewPrinters($offset = 0, $limit = 500, $printerSet = null, $com
196183
* Both arguments can be combined to return only certain print jobs on
197184
* certain printers
198185
*
199-
* @param int $offset (Optional) The start index for the records the API should return
200186
* @param int $limit (Optional) The number of records the API should return
187+
* @param int $after (Optional) The after (or before, depending on $dir) which to start returning records
188+
* @param string $dir (Optional) "asc" for ascending or "desc" for descending, ordered by computer id
201189
* @param string|array $printJobSet (Optional) 'set' string or array of print job ids to which the response should be limited
202190
* @param string|array $printerSet (Optional) 'set' string or array of printer ids to which the response should be limited
203191
* @return \PrintNode\Entity\PrintJob[]
204192
*/
205-
public function viewPrintJobs($offset = 0, $limit = 500, $printJobSet = null, $printerSet = null)
193+
public function viewPrintJobs($limit = null, $after = null, $dir = null, $printJobSet = null, $printerSet = null)
206194
{
207195

208196
$url = 'printjobs';
@@ -215,7 +203,7 @@ public function viewPrintJobs($offset = 0, $limit = 500, $printJobSet = null, $p
215203
$url = sprintf('printers/%s/printjobs', $this->setImplode($printerSet));
216204
}
217205

218-
$url = $this->applyLimitOffsetToUrl($url, $offset, $limit);
206+
$url = $this->applyPaginationToUrl($url, $limit, $after, $dir);
219207

220208
return $this->makeRequestMapped($url, 'GET', '\PrintNode\Entity\PrintJob');
221209

@@ -230,12 +218,13 @@ public function viewPrintJobs($offset = 0, $limit = 500, $printJobSet = null, $p
230218
*
231219
* Returned is an array of states in an array keyed by the print job id.
232220
*
233-
* @param int $offset (Optional) The start index for the records the API should return
234221
* @param int $limit (Optional) The number of records the API should return
222+
* @param int $after (Optional) The id after (or before, depending on $dir) which to start returning records
223+
* @param string $dir (Optional) "asc" for ascending or "desc" for descending, ordered by computer id
235224
* @param string|array $printJobSet (Optional) 'set' string or array of print job ids to which the response should be limited
236225
* @return array
237226
*/
238-
public function viewPrintJobState($offset = 0, $limit = 500, $printJobSet = null)
227+
public function viewPrintJobState($limit = null, $after = null, $dir = null, $printJobSet = null)
239228
{
240229

241230
$url = 'printjobs/states';
@@ -244,7 +233,7 @@ public function viewPrintJobState($offset = 0, $limit = 500, $printJobSet = null
244233
$url = sprintf('printjobs/%s/states', $this->setImplode($printJobSet));
245234
}
246235

247-
$url = $this->applyLimitOffsetToUrl($url, $offset, $limit);
236+
$url = $this->applyPaginationToUrl($url, $limit, $after, $dir);
248237

249238
$this->lastResponse = $this->makeRequest($url, 'GET');
250239

@@ -310,55 +299,64 @@ public function viewScales($computerId, $deviceName = null, $deviceNumber = null
310299
*
311300
* Returned is an array of downloads in an array keyed by the download id.
312301
*
313-
* @param int $offset (Optional) The start index for the records the API should return
314-
* @param int $limit (Optional) The number of records the API should return
315302
* @param string|array (Optional) $downloadSet 'set' string or array of download ids to which the response should be limited
316303
* @return array
317304
*/
318-
public function viewClientDownloads($offset = 0, $limit = 1, $downloadSet = null)
305+
public function viewClientDownloads($downloadSet = null)
319306
{
320-
321307
$url = 'download/clients';
322308

323309
if (isset($downloadSet)) {
324310
$url = sprintf('download/clients/%s', $this->setImplode($downloadSet));
325311
}
326312

327-
$url = $this->applyLimitOffsetToUrl($url, $offset, $limit);
328-
329313
return $this->makeRequestMapped($url, 'GET', '\PrintNode\Entity\ClientDownload');
330314

331315
}
332316

333317
/**
334-
* Appends the offset and limit arguments to a given api endpoint url
318+
* Appends limit argument to a given api endpoint url
335319
*
336-
* @param string $url (Optional) The url to which any limits or offsets will be applied
337-
* @param int $offset (Optional) The offset to apply to the url
320+
* @param string $url (Optional) The url to which limit will be applied
338321
* @param int $limit (Optional) The limit to apply to the url
322+
* @param int $after (Optional) The id after (or before, depending on $dir) which to start returning records
323+
* @param string $dir (Optional) "asc" for ascending or "desc" for descending, ordered by computer id
339324
* @return string
340325
* @throws \PrintNode\Exception\InvalidArgumentException
341326
*/
342-
public function applyLimitOffsetToUrl($url, $offset, $limit)
327+
public function applyPaginationToUrl($url, $limit, $after, $dir)
343328
{
344-
345-
if (!\is_numeric($offset)) {
346-
throw new \PrintNode\Exception\InvalidArgumentException('Offset must be a number');
347-
}
348-
349-
if (!\is_numeric($limit)) {
350-
throw new \PrintNode\Exception\InvalidArgumentException('Limit must be a number');
351-
}
352-
353-
if ($offset < 0) {
354-
throw new \PrintNode\Exception\InvalidArgumentException('Offset cannot be negative');
355-
}
356-
357-
if ($limit < 1) {
358-
throw new \PrintNode\Exception\InvalidArgumentException('Limit must be greater than zero');
359-
}
360-
361-
return sprintf('%s?offset=%s&limit=%s', $url, $offset, $limit);
329+
$vars = Array();
330+
if (!is_null($limit)) {
331+
if (!\is_numeric($limit)) {
332+
throw new \PrintNode\Exception\InvalidArgumentException('Limit must be a number');
333+
}
334+
335+
if ($limit < 1) {
336+
throw new \PrintNode\Exception\InvalidArgumentException('Limit must be greater than zero');
337+
}
338+
$vars[] = sprintf("limit=%d", $limit);
339+
}
340+
341+
if (!is_null($after)) {
342+
if (!\is_numeric($after)) {
343+
throw new \PrintNode\Exception\InvalidArgumentException('After must be a number');
344+
}
345+
$vars[] = sprintf("after=%d", $limit);
346+
}
347+
348+
if (!is_null($dir)) {
349+
if (($dir !== "asc") && ($dir !== "desc")) {
350+
throw new \PrintNode\Exception\InvalidArgumentException('Dir must be "asc" or "desc"');
351+
}
352+
$vars[] = sprintf("dir=%s", $dir);
353+
}
354+
355+
if (count($vars) > 0) {
356+
return $url . "?" . implode("&", $vars);
357+
} else {
358+
return $url;
359+
}
362360

363361
}
364362

src/PrintNode/Entity/Computer.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,16 @@ class Computer extends Entity
110110
/**
111111
* Returns an array of the printers present on this computer
112112
*
113-
* @param int $offset
114113
* @param int $limit
114+
* @param int after
115+
* @param string dir
115116
* @param mixed $printerSet
116117
* @return array
117118
*/
118-
public function viewPrinters($offset = 0, $limit = 500, $printerSet = null)
119+
public function viewPrinters($limit = null, $after = null, $dir = null, $printerSet = null)
119120
{
120121

121-
return $this->client->viewPrinters($offset, $limit, $printerSet, $this->id);
122+
return $this->client->viewPrinters($limit, $after, $dir, $printerSet, $this->id);
122123

123124
}
124125

0 commit comments

Comments
 (0)