Skip to content

Commit

Permalink
Merge branch 'release/0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
nixilla committed May 22, 2019
2 parents cc0ba2c + c559ed1 commit 442efb8
Show file tree
Hide file tree
Showing 21 changed files with 199 additions and 302 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/nixilla/api-logger-bundle/badges/quality-score.png?b=develop)](https://scrutinizer-ci.com/g/nixilla/api-logger-bundle/?branch=develop)
[![License](https://poser.pugx.org/nixilla/api-logger-bundle/license.svg)](https://packagist.org/packages/nixilla/api-logger-bundle)

## Versions

For `buzz<0.16` please use `nixilla/api-logger-bundle:^0.4` and for latest `buzz:^1`, use `nixilla/api-logger-bundle:^0.5`

## Installation

Step 1: composer
Expand All @@ -31,6 +35,19 @@ Step 2: enable bundle by adding it to AppKernel

Step 3: configuration

For latest buzz all you need to do is add this line into `config/packages/buzz.yml`

```yaml
services:
Buzz\Browser:
arguments: ['@Buzz\Client\BuzzClientInterface', '@Psr\Http\Message\RequestFactoryInterface']
calls:
# other middleware classes here
- ['addMiddleware', ['@Nixilla\Api\LoggerBundle\Middleware\ApiLoggerMiddleware']]
```
For earlier version of buzz <0.16 and earlier version of this bundle <0.5 you need to configure like this:
If you're use HWIOAuthBundle and you want to monitor all OAuth API calls, you can now override default
`hwi_oauth.http_client` service used by this bundle by adding this few lines to your `config_dev.yml` file

Expand Down Expand Up @@ -85,4 +102,4 @@ services:
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 @@ -17,9 +17,10 @@
],
"require": {
"php": ">=5.6",
"ext-json": "*",
"symfony/framework-bundle": "~2.3||~3||~4",
"twig/twig": "~1.24||~2",
"kriswallsmith/buzz": "<0.16"
"kriswallsmith/buzz": "^1"
},
"require-dev" :{
"phpspec/phpspec": "^4.3",
Expand Down
1 change: 1 addition & 0 deletions spec/DependencyInjection/NixillaApiLoggerExtensionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function it_is_initializable()

function it_implements_required_load_method(ContainerBuilder $container)
{
$container->removeBindings(Argument::any())->shouldBeCalled();
$container->fileExists(Argument::any())->willReturn(true);
$container->setDefinition(Argument::any(), Argument::any())->shouldBeCalled();
$container->setParameter(Argument::any(), Argument::any())->shouldBeCalled();
Expand Down
74 changes: 74 additions & 0 deletions spec/Middleware/ApiLoggerMiddlewareSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

namespace spec\Nixilla\Api\LoggerBundle\Middleware;

use Buzz\Middleware\MiddlewareInterface;
use Nixilla\Api\LoggerBundle\Logger\ApiInterface;
use Nixilla\Api\LoggerBundle\Middleware\ApiLoggerMiddleware;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UriInterface;

class ApiLoggerMiddlewareSpec extends ObjectBehavior
{
function let(ApiInterface $logger)
{
$this->beConstructedWith($logger);
}

function it_is_initializable()
{
$this->shouldHaveType(ApiLoggerMiddleware::class);
$this->shouldHaveType(MiddlewareInterface::class);
}

function it_can_handle_request(RequestInterface $request)
{
$next = function() {
return;
};

$this->handleRequest($request, $next);
}

function it_can_handle_response(RequestInterface $request, ResponseInterface $response, UriInterface $uri, StreamInterface $stream)
{
$next = function() {
return;
};

$request->getUri()->willReturn($uri);
$request->getHeader(Argument::any())->shouldBeCalled();
$request->getHeaders()->willReturn([]);
$request->getBody()->willReturn('key=value&some=value');
$request->getMethod()->shouldBeCalled();

$response->getHeaders()->willReturn([]);
$response->getBody()->willReturn($stream);

$this->handleResponse($request, $response, $next);
}

function it_can_handle_response_with_json(RequestInterface $request, ResponseInterface $response, UriInterface $uri, StreamInterface $stream)
{
$next = function() {
return;
};

$request->getUri()->willReturn($uri);
$request->getHeader('Content-Type')->willReturn('application/json');
$request->getHeaders()->willReturn([]);
$request->getBody()->willReturn('{"key":"value"}');
$request->getMethod()->shouldBeCalled();

$response->getHeaders()->willReturn([]);
$response->getBody()->willReturn($stream);

$this->handleResponse($request, $response, $next);
}


}
21 changes: 0 additions & 21 deletions spec/Proxy/Buzz/Client/CurlSpec.php

This file was deleted.

49 changes: 0 additions & 49 deletions spec/Proxy/Buzz/Client/FileGetContentsSpec.php

This file was deleted.

35 changes: 0 additions & 35 deletions spec/Proxy/Twilio/CurlClientSpec.php

This file was deleted.

12 changes: 6 additions & 6 deletions spec/Twig/CurlFormatterSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,27 @@ function it_is_initializable()
function it_provides_Twig_filter_which_formats_input_into_curl_command()
{
$this->formatForCurl([
'host' => 'http://endpoint',
'host' => 'http://endpoint/',
'path' => 'api/endoint',
'method' => 'POST',
'params' => ['foo' => 'bar', 'baz' => 1],
'request_headers' => [ 'Authorization: Bearer lakdjfghalkdjfgh' ]
'request_headers' => [ 'Authorization' => [ 'Bearer lakdjfghalkdjfgh'] ]
])->shouldBeEqualTo("curl -v -X POST -H \"Authorization: Bearer lakdjfghalkdjfgh\" http://endpoint/api/endoint --data 'foo=bar&baz=1'");

$this->formatForCurl([
'host' => 'http://endpoint',
'host' => 'http://endpoint/',
'path' => 'api/endoint',
'method' => 'PATCH',
'params' => ['foo' => 'bar', 'baz' => ['key' => 'value']],
'request_headers' => [ 'Authorization: Bearer lakdjfghalkdjfgh', 'Content-Type: application/json' ]
'request_headers' => [ 'Authorization' => ['Bearer lakdjfghalkdjfgh'], 'Content-Type' => [ 'application/json' ]]
])->shouldBeEqualTo("curl -v -X PATCH -H \"Authorization: Bearer lakdjfghalkdjfgh\" -H \"Content-Type: application/json\" http://endpoint/api/endoint --data '{\"foo\":\"bar\",\"baz\":{\"key\":\"value\"}}'");

$this->formatForCurl([
'host' => 'http://endpoint',
'host' => 'http://endpoint/',
'path' => 'api/endoint',
'method' => 'GET',
'params' => ['foo' => 'bar', 'baz' => ['key' => 'value']],
'request_headers' => [ 'Authorization: Bearer lakdjfghalkdjfgh', 'Content-Type: application/json' ]
'request_headers' => [ 'Authorization' => ['Bearer lakdjfghalkdjfgh'], 'Content-Type' => [ 'application/json' ]]
])->shouldBeEqualTo("curl -v -X GET -H \"Authorization: Bearer lakdjfghalkdjfgh\" -H \"Content-Type: application/json\" http://endpoint/api/endoint");
}

Expand Down
2 changes: 1 addition & 1 deletion src/DataCollector/ApiDataCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class ApiDataCollector extends DataCollector
{
/**
* @var \Nixilla\Api\LoggerBundle\Logger\Api
* @var \Nixilla\Api\LoggerBundle\Logger\ApiInterface
*/
protected $logger;

Expand Down
7 changes: 5 additions & 2 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,11 @@ class Configuration implements ConfigurationInterface
{
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$treeBuilder->root('nixilla_api_logger');
$treeBuilder = new TreeBuilder('nixilla_api_logger');

if ( ! method_exists($treeBuilder, 'getRootNode')) {
$treeBuilder->root('nixilla_api_logger');
}

return $treeBuilder;
}
Expand Down
10 changes: 0 additions & 10 deletions src/Logger/Api.php

This file was deleted.

5 changes: 3 additions & 2 deletions src/Logger/ApiLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Nixilla\Api\LoggerBundle\Logger;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;

class ApiLogger implements ApiInterface
{
Expand All @@ -21,9 +22,9 @@ class ApiLogger implements ApiInterface
protected $debug = false;


public function __construct(LoggerInterface $logger, $debug = false)
public function __construct(LoggerInterface $logger = null, $debug = false)
{
$this->logger = $logger;
$this->logger = $logger ?: new NullLogger();
$this->debug = $debug;
}

Expand Down
61 changes: 61 additions & 0 deletions src/Middleware/ApiLoggerMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Nixilla\Api\LoggerBundle\Middleware;

use Buzz\Middleware\MiddlewareInterface;
use Nixilla\Api\LoggerBundle\Logger\ApiInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class ApiLoggerMiddleware implements MiddlewareInterface
{
/** @var ApiInterface */
private $logger;

/** @var float */
private $startTime;

/**
* ApiLoggerMiddleware constructor.
*
* @param ApiInterface $logger
*/
public function __construct(ApiInterface $logger)
{
$this->logger = $logger;
}

public function handleRequest(RequestInterface $request, callable $next)
{
$this->startTime = microtime(true);

return $next($request);

}

public function handleResponse(RequestInterface $request, ResponseInterface $response, callable $next)
{
$time = microtime(true) - $this->startTime;

if($request->getHeader('Content-Type') == 'application/json')
{
$payload = json_decode($request->getBody(), true) ?: [];
}
else {
parse_str($request->getBody(), $payload);
}

$this->logger->logCall(
sprintf('%s://%s', $request->getUri()->getScheme(), $request->getUri()->getHost()),
$request->getUri()->getPath(),
$request->getMethod(),
$time,
$request->getHeaders(),
$payload,
$response->getHeaders(),
$response->getBody()->getContents()
);

return $next($request, $response);
}
}
Loading

0 comments on commit 442efb8

Please sign in to comment.