Skip to content

Commit 14bb8bb

Browse files
Add possibility to specify custom response verifier
1 parent 5d778aa commit 14bb8bb

File tree

2 files changed

+35
-1
lines changed

2 files changed

+35
-1
lines changed

src/PleskX/Api/Client.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ class Client
2525

2626
protected $_operatorsCache = [];
2727

28+
/**
29+
* @var callable
30+
*/
31+
protected $_verifyResponseCallback;
32+
2833
/**
2934
* Create client
3035
*
@@ -71,6 +76,16 @@ public function setVersion($version)
7176
$this->_version = $version;
7277
}
7378

79+
/**
80+
* Set custom function to verify response of API call according your own needs. Default verifying will be used if it is not specified
81+
*
82+
* @param callable|null $function
83+
*/
84+
public function setVerifyResponse(callable $function = null)
85+
{
86+
$this->_verifyResponseCallback = $function;
87+
}
88+
7489
/**
7590
* Retrieve host used for communication
7691
*
@@ -143,7 +158,10 @@ public function request($request, $mode = self::RESPONSE_SHORT)
143158
} else {
144159
$xml = $this->_performHttpRequest($request);
145160
}
146-
$this->_verifyResponse($xml);
161+
162+
$this->_verifyResponseCallback
163+
? call_user_func($this->_verifyResponseCallback, $xml)
164+
: $this->_verifyResponse($xml);
147165

148166
return (self::RESPONSE_FULL == $mode) ? $xml : $xml->xpath('//result')[0];
149167
}

tests/ApiClientTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22
// Copyright 1999-2016. Parallels IP Holdings GmbH.
3+
use \PleskX\Api\Client\Exception;
34

45
class ApiClientTest extends TestCase
56
{
@@ -155,4 +156,19 @@ public function testGetProtocol()
155156
$this->assertEquals('http', $client->getProtocol());
156157
}
157158

159+
public function testSetVerifyResponse()
160+
{
161+
static::$_client->setVerifyResponse(function ($xml) {
162+
if ($xml->xpath('//proto')) {
163+
throw new Exception('proto');
164+
}
165+
});
166+
try {
167+
static::$_client->server()->getProtos();
168+
} catch (Exception $e) {
169+
$this->assertEquals('proto', $e->getMessage());
170+
} finally {
171+
static::$_client->setVerifyResponse();
172+
}
173+
}
158174
}

0 commit comments

Comments
 (0)