Skip to content

Commit

Permalink
Added Serialization and Rate Limit:
Browse files Browse the repository at this point in the history
- Verification object can now be serialized / unserialized.
- Client retires rate limit responses from API. This is not configurable.
- TODO: Make rate limit retry configurable.
- TODO: Test backoff time matches API response.
  • Loading branch information
tjlytle committed Jun 1, 2016
1 parent 23c6323 commit 60f4c4e
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 2 deletions.
10 changes: 9 additions & 1 deletion src/Message/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function send($message)
$message = $this->createMessageFromArray($message);
}

$params = $message->getRequestData();
$params = $message->getRequestData(false);

$request = new Request(
\Nexmo\Client::BASE_REST . '/sms/json'
Expand All @@ -59,6 +59,14 @@ public function send($message)
switch($part['status']){
case '0':
continue; //all okay
case '1':
if(preg_match('#\[\s+(\d+)\s+\]#', $part['error-text'], $match)){
usleep($match[1] + 1);
} else {
sleep(1);
}

return $this->send($message);
case '5':
$e = new Exception\Server($part['error-text'], $part['status']);
$e->setEntity($message);
Expand Down
36 changes: 35 additions & 1 deletion src/Verify/Verification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use Nexmo\Entity\Psr7Trait;
use Nexmo\Entity\RequestArrayTrait;

class Verification implements VerificationInterface, \ArrayAccess
class Verification implements VerificationInterface, \ArrayAccess, \Serializable
{
use Psr7Trait;
use RequestArrayTrait;
Expand Down Expand Up @@ -559,4 +559,38 @@ protected function getReadOnlyException($offset)
$offset
));
}

public function serialize()
{
$data = [
'requestData' => $this->requestData
];

if($request = $this->getRequest()){
$data['request'] = \Zend\Diactoros\Request\Serializer::toString($request);
}

if($response = $this->getResponse()){
$data['response'] = \Zend\Diactoros\Response\Serializer::toString($response);
}

return serialize($data);
}

public function unserialize($serialized)
{
$data = unserialize($serialized);

$this->requestData = $data['requestData'];

if(isset($data['request'])){
$this->request = \Zend\Diactoros\Request\Serializer::fromString($data['request']);
}

if(isset($data['response'])){
$this->response = \Zend\Diactoros\Response\Serializer::fromString($data['response']);
}
}


}
22 changes: 22 additions & 0 deletions test/Message/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,28 @@ public function testCanSearchBySingleInboundId()
$this->assertSame($response, $message->getResponse());
}

public function testRateLimitRetires()
{
$rate = $this->getResponse('ratelimit');
$success = $this->getResponse('success');

$args = [
'to' => '14845551345',
'from' => '1105551334',
'text' => 'test message'
];

$this->nexmoClient->send(Argument::that(function(Request $request) use ($args){
$this->assertRequestJsonBodyContains('to', $args['to'], $request);
$this->assertRequestJsonBodyContains('from', $args['from'], $request);
$this->assertRequestJsonBodyContains('text', $args['text'], $request);
return true;
}))->willReturn($rate, $rate, $success);

$message = $this->messageClient->send(new Text($args['to'], $args['from'], $args['text']));
$this->assertEquals($success, $message->getResponse());
}

/**
* Get the API response we'd expect for a call to the API. Message API currently returns 200 all the time, so only
* change between success / fail is body of the message.
Expand Down
11 changes: 11 additions & 0 deletions test/Message/responses/ratelimit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"message-count": "1",
"messages": [
{
"to": "14843472194",
"status": "1",
"error-text": "Throughput Rate Exceeded - please wait [ 97 ] and retry",
"network": "310260"
}
]
}
28 changes: 28 additions & 0 deletions test/Verify/VerificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,34 @@ public function testExceptionForCheckFail()
$this->exsisting->check('4321');
}

/**
* @dataProvider getSerializeResponses
*/
public function testSerialize($response)
{
$this->exsisting->setResponse($response);
$this->exsisting->getResponse()->getBody()->rewind();
$this->exsisting->getResponse()->getBody()->getContents();
$serialized = serialize($this->exsisting);
/* @var $unserialized Verification */
$unserialized = unserialize($serialized);

$this->assertInstanceOf(get_class($this->exsisting), $unserialized);

$this->assertEquals($this->exsisting->getAccountId(), $unserialized->getAccountId());
$this->assertEquals($this->exsisting->getStatus(), $unserialized->getStatus());

$this->assertEquals($this->exsisting->getResponseData(), $unserialized->getResponseData());
}

public function getSerializeResponses()
{
return [
[$this->getResponse('search')],
[$this->getResponse('start')],
];
}

/**
* @dataProvider getClientProxyMethods
*/
Expand Down

0 comments on commit 60f4c4e

Please sign in to comment.