Skip to content

Commit

Permalink
adds Twilio Proxy for Http Client
Browse files Browse the repository at this point in the history
  • Loading branch information
nixilla committed Jun 21, 2018
1 parent 4017cd5 commit 2538108
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 7 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,23 @@ services:
- [ "setTimeout", [ "%buzz.client.timeout%" ] ]
- [ "setLogger", [ "@nixilla.api.logger" ] ]
```

If you're using `twilio/sdk` you may want to override their Http Client in config_dev.yml

```yaml
# app/config/config_dev.yml
imports:
- { resource: config.yml }
services:
twilio.http.client:
class: Nixilla\Api\LoggerBundle\Proxy\Twilio\CurlClient
calls:
- [ "setLogger", [ "@nixilla.api.logger" ] ]
twilio.rest.client:
class: Twilio\Rest\Client
arguments: [ "%twilio.username%", "%twilio.password%", ~, ~, '@twilio.http.client']
```
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
"sebastian/phpcpd": "^4.0",
"leanphp/phpspec-code-coverage": "^4.2",
"php-coveralls/php-coveralls": "^2.1",
"symfony/expression-language": "~2.3||~3"
"symfony/expression-language": "~2.3||~3",
"twilio/sdk": "^5.19"
},
"autoload": {
"psr-4": {
Expand Down
7 changes: 3 additions & 4 deletions spec/DataCollector/ApiDataCollectorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
namespace spec\Nixilla\Api\LoggerBundle\DataCollector;

use Nixilla\Api\LoggerBundle\DataCollector\ApiDataCollector;
use Nixilla\Api\LoggerBundle\Logger\Api;
use Nixilla\Api\LoggerBundle\Logger\ApiInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

class ApiDataCollectorSpec extends ObjectBehavior
{
function let(Api $logger)
function let(ApiInterface $logger)
{
$this->beConstructedWith($logger);
}
Expand All @@ -28,7 +27,7 @@ function it_implements_getName()
$this->getName()->shouldNotReturn(null);
}

function it_implements_collect_method(Api $logger, Request $request, Response $response)
function it_implements_collect_method(ApiInterface $logger, Request $request, Response $response)
{
$logger->getCalls()->shouldBeCalled()->willReturn([['time' => 5 ]]);
$logger->getCallsCount()->shouldBeCalled()->willReturn(5);
Expand Down
15 changes: 15 additions & 0 deletions spec/Proxy/Twilio/CurlClientSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace spec\Nixilla\Api\LoggerBundle\Proxy\Twilio;

use Nixilla\Api\LoggerBundle\Proxy\Twilio\CurlClient;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class CurlClientSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(CurlClient::class);
}
}
4 changes: 2 additions & 2 deletions src/DataCollector/ApiDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Nixilla\Api\LoggerBundle\DataCollector;

use Nixilla\Api\LoggerBundle\Logger\Api;
use Nixilla\Api\LoggerBundle\Logger\ApiInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;
Expand All @@ -17,7 +17,7 @@ class ApiDataCollector extends DataCollector
/**
* {@inheritDoc}
*/
public function __construct(Api $logger)
public function __construct(ApiInterface $logger)
{
$this->logger = $logger;
}
Expand Down
59 changes: 59 additions & 0 deletions src/Proxy/Twilio/CurlClient.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace Nixilla\Api\LoggerBundle\Proxy\Twilio;

use Nixilla\Api\LoggerBundle\Traits\Log;
use Twilio\Http\CurlClient as BaseClient;
use Twilio\Http\Response;

class CurlClient extends BaseClient
{
use Log;

public function request(
$method,
$url,
$params = [],
$data = [],
$headers = [],
$user = null,
$password = null,
$timeout = null
)
{
$start = microtime(true);

/** @var Response $result */
$result = parent::request($method, $url, $params, $data, $headers, $user, $password, $timeout);

if($this->hasLogger())
{
$time = microtime(true) - $start;

$this->getLogger()->logCall(
$url,
'',
$method,
$time,
$this->fixHeaders($headers),
$data,
$result->getHeaders(),
json_encode($result->getContent())
);
}

return $result;
}

private function fixHeaders($headers)
{
$fixed = [];

foreach ($headers as $name => $value)
{
$fixed[] = sprintf('%s: %s', $name, $value);
}

return $fixed;
}
}

0 comments on commit 2538108

Please sign in to comment.