Skip to content

Commit

Permalink
Merge branch 'release/0.3.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
nixilla committed Jun 7, 2016
2 parents 9f03272 + b6ffa0e commit 5c4ab21
Show file tree
Hide file tree
Showing 9 changed files with 172 additions and 9 deletions.
17 changes: 17 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar update --prefer-dist

script: bin/phpspec run -fpretty --verbose

cache:
directories:
- $HOME/.composer/cache
10 changes: 9 additions & 1 deletion Proxy/Buzz/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ public function send(RequestInterface $request, MessageInterface $response, arra
if($this->hasLogger())
{
$time = microtime(true) - $start;
parse_str($request->getContent(), $payload);

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

$this->getLogger()->logCall(
$request->getHost(),
$request->getResource(),
Expand Down
5 changes: 4 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ API Logger Bundle

API logger bundle help your app with API calls monitoring.

.. image:: https://travis-ci.org/nixilla/api-logger-bundle.svg?branch=master
:target: https://travis-ci.org/nixilla/api-logger-bundle

Installation
------------

Expand Down Expand Up @@ -62,7 +65,7 @@ If you're using `sensio/buzz-bundle`, you may want to override the `buzz.client`
services:
buzz.client:
class: %buzz.client.class%
class: "%buzz.client.class%"
calls:
- [ "setTimeout", [ "%buzz.client.timeout%" ] ]
- [ "setLogger", [ "@nixilla.api.logger" ] ]
Expand Down
8 changes: 7 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,10 @@ services:

nixilla.api.logger:
class: "%nixilla.api.logger.class%"
arguments: [ "@logger", "%kernel.debug%" ]
arguments: [ "@logger", "%kernel.debug%" ]

nixilla.curl_format.twig_extension:
class: Nixilla\Api\LoggerBundle\Twig\CurlFormatter
public: true
tags:
- { name: twig.extension }
8 changes: 4 additions & 4 deletions Resources/views/DataCollector/api.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,11 @@
<th>Request payload</th>
<td>{{ call.params|yaml_encode }}</td>
</tr>
<tr>
<th>cURL command</th>
<td>{{ 'curl -X ' ~ call.method|upper ~ ' --data "' ~ call.params|url_encode ~ '" ' ~ call.host ~ call.path }}</td>
</tr>
{% endif %}
<tr>
<th>cURL command</th>
<td>{{ call|format_curl_command }}</td>
</tr>
<tr>
<th>Response headers:</th>
<td>{{ call.response_headers|yaml_encode }}</td>
Expand Down
58 changes: 58 additions & 0 deletions Twig/CurlFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

namespace Nixilla\Api\LoggerBundle\Twig;

class CurlFormatter extends \Twig_Extension
{
public function getName()
{
return 'nixilla_api_logger';
}

public function getFilters()
{
return array(
new \Twig_SimpleFilter('format_curl_command', array($this, 'formatForCurl'))
);
}

public function formatForCurl(array $singleCall)
{
return sprintf(
'curl -v -X %s%s %s/%s%s',
$singleCall['method'],
$this->generateHeaders($singleCall['request_headers']),
$singleCall['host'],
$singleCall['path'],
$this->formatParams($singleCall)
);
}

private function generateHeaders($request_headers)
{
$output = '';

foreach($request_headers as $value)
{
$output .= sprintf(' -H "%s"', $value);
}

return $output;
}

private function formatParams($singleCall)
{
if($singleCall['method'] != 'GET')
{
foreach($singleCall['request_headers'] as $header)
{
if($header == 'Content-Type: application/json')
{
return sprintf(" --data '%s'", json_encode($singleCall['params']));
}
}

return sprintf(" --data '%s'", http_build_query($singleCall['params']));
}
}
}
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
],
"require": {
"php": ">=5.4",
"symfony/framework-bundle": "~2.3|~3.0"
"symfony/framework-bundle": "~2.3|~3.0",
"twig/twig": "^1.24"
},
"require-dev" :{
"phpspec/phpspec": "^2.5"
},
"autoload": {
"psr-0": {
"Nixilla\\Api\\LoggerBundle": ""
}
},
"config": {
"bin-dir": "bin"
},
"target-dir": "Nixilla/Api/LoggerBundle"
}
}
20 changes: 20 additions & 0 deletions spec/Nixilla/Api/LoggerBundle/NixillaApiLoggerBundleSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace spec\Nixilla\Api\LoggerBundle;

use Nixilla\Api\LoggerBundle\NixillaApiLoggerBundle;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class NixillaApiLoggerBundleSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Nixilla\Api\LoggerBundle\NixillaApiLoggerBundle');
}

function it_should_be_a_bundle()
{
$this->shouldHaveType('Symfony\Component\HttpKernel\Bundle\Bundle');
}
}
44 changes: 44 additions & 0 deletions spec/Nixilla/Api/LoggerBundle/Twig/CurlFormatterSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace spec\Nixilla\Api\LoggerBundle\Twig;

use Nixilla\Api\LoggerBundle\Twig\CurlFormatter;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class CurlFormatterSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType('Nixilla\Api\LoggerBundle\Twig\CurlFormatter');
}

function it_provides_Twig_filter_which_formats_input_into_curl_command()
{
$this->formatForCurl([
'host' => 'http://endpoint',
'path' => 'api/endoint',
'method' => 'POST',
'params' => ['foo' => 'bar', 'baz' => 1],
'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',
'path' => 'api/endoint',
'method' => 'PATCH',
'params' => ['foo' => 'bar', 'baz' => ['key' => 'value']],
'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\"}}'");
}

function it_has_a_name()
{
$this->getName()->shouldNotReturn(null);
}

function it_has_required_getFilters_method()
{
$this->getFilters()->shouldHaveCount(1);
}
}

0 comments on commit 5c4ab21

Please sign in to comment.