mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-10 03:29:25 +03:00
Merge pull request #85 from vslinko/json-declaration-method
Make request format configurable
This commit is contained in:
commit
bd3e63c479
@ -28,6 +28,19 @@ class Configuration implements ConfigurationInterface
|
|||||||
->children()
|
->children()
|
||||||
->scalarNode('enabled')->defaultTrue()->end()
|
->scalarNode('enabled')->defaultTrue()->end()
|
||||||
->scalarNode('endpoint')->defaultValue('/app_dev.php')->end()
|
->scalarNode('endpoint')->defaultValue('/app_dev.php')->end()
|
||||||
|
->arrayNode('request_format')
|
||||||
|
->addDefaultsIfNotSet()
|
||||||
|
->children()
|
||||||
|
->enumNode('method')
|
||||||
|
->values(array('format_param', 'accept_header'))
|
||||||
|
->defaultValue('format_param')
|
||||||
|
->end()
|
||||||
|
->enumNode('default_format')
|
||||||
|
->values(array('json', 'xml'))
|
||||||
|
->defaultValue('json')
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
|
->end()
|
||||||
->arrayNode('authentication')
|
->arrayNode('authentication')
|
||||||
->children()
|
->children()
|
||||||
->scalarNode('name')->isRequired()->end()
|
->scalarNode('name')->isRequired()->end()
|
||||||
|
@ -31,6 +31,8 @@ class NelmioApiDocExtension extends Extension
|
|||||||
$container->setParameter('nelmio_api_doc.api_name', $config['name']);
|
$container->setParameter('nelmio_api_doc.api_name', $config['name']);
|
||||||
$container->setParameter('nelmio_api_doc.sandbox.enabled', $config['sandbox']['enabled']);
|
$container->setParameter('nelmio_api_doc.sandbox.enabled', $config['sandbox']['enabled']);
|
||||||
$container->setParameter('nelmio_api_doc.sandbox.endpoint', $config['sandbox']['endpoint']);
|
$container->setParameter('nelmio_api_doc.sandbox.endpoint', $config['sandbox']['endpoint']);
|
||||||
|
$container->setParameter('nelmio_api_doc.sandbox.request_format.method', $config['sandbox']['request_format']['method']);
|
||||||
|
$container->setParameter('nelmio_api_doc.sandbox.request_format.default_format', $config['sandbox']['request_format']['default_format']);
|
||||||
|
|
||||||
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
|
||||||
$loader->load('formatters.xml');
|
$loader->load('formatters.xml');
|
||||||
|
@ -40,6 +40,16 @@ class HtmlFormatter extends AbstractFormatter
|
|||||||
*/
|
*/
|
||||||
private $engine;
|
private $engine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $requestFormatMethod;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $defaultRequestFormat;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param array $authentication
|
* @param array $authentication
|
||||||
*/
|
*/
|
||||||
@ -80,6 +90,19 @@ class HtmlFormatter extends AbstractFormatter
|
|||||||
$this->engine = $engine;
|
$this->engine = $engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param string $method
|
||||||
|
*/
|
||||||
|
public function setRequestFormatMethod($method)
|
||||||
|
{
|
||||||
|
$this->requestFormatMethod = $method;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function setDefaultRequestFormat($format)
|
||||||
|
{
|
||||||
|
$this->defaultRequestFormat = $format;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
@ -133,13 +156,15 @@ class HtmlFormatter extends AbstractFormatter
|
|||||||
private function getGlobalVars()
|
private function getGlobalVars()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'apiName' => $this->apiName,
|
'apiName' => $this->apiName,
|
||||||
'authentication' => $this->authentication,
|
'authentication' => $this->authentication,
|
||||||
'endpoint' => $this->endpoint,
|
'endpoint' => $this->endpoint,
|
||||||
'enableSandbox' => $this->enableSandbox,
|
'enableSandbox' => $this->enableSandbox,
|
||||||
'date' => date(DATE_RFC822),
|
'requestFormatMethod' => $this->requestFormatMethod,
|
||||||
'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),
|
'defaultRequestFormat' => $this->defaultRequestFormat,
|
||||||
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
|
'date' => date(DATE_RFC822),
|
||||||
|
'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),
|
||||||
|
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -37,6 +37,12 @@
|
|||||||
<call method="setEndpoint">
|
<call method="setEndpoint">
|
||||||
<argument>%nelmio_api_doc.sandbox.endpoint%</argument>
|
<argument>%nelmio_api_doc.sandbox.endpoint%</argument>
|
||||||
</call>
|
</call>
|
||||||
|
<call method="setRequestFormatMethod">
|
||||||
|
<argument>%nelmio_api_doc.sandbox.request_format.method%</argument>
|
||||||
|
</call>
|
||||||
|
<call method="setDefaultRequestFormat">
|
||||||
|
<argument>%nelmio_api_doc.sandbox.request_format.default_format%</argument>
|
||||||
|
</call>
|
||||||
<call method="setAuthentication">
|
<call method="setAuthentication">
|
||||||
<argument>%nelmio_api_doc.sandbox.authentication%</argument>
|
<argument>%nelmio_api_doc.sandbox.authentication%</argument>
|
||||||
</call>
|
</call>
|
||||||
|
@ -148,9 +148,9 @@ table tbody tr:last-child td {
|
|||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
#api_key_wrapper {
|
#sandbox_configuration {
|
||||||
float: right;
|
float: right;
|
||||||
padding: 10px 0 10px 0;
|
padding: 10px 0 10px 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
#colophon {
|
#colophon {
|
||||||
|
@ -15,11 +15,16 @@
|
|||||||
<body>
|
<body>
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<a href=""><h1>{{ apiName }}</h1></a>
|
<a href=""><h1>{{ apiName }}</h1></a>
|
||||||
{% if authentication %}
|
<div id="sandbox_configuration">
|
||||||
<div id="api_key_wrapper">
|
request format:
|
||||||
api key: <input type="text" id="api_key"/>
|
<select id="request_format">
|
||||||
|
<option value="json"{{ defaultRequestFormat == 'json' ? ' selected' : '' }}>JSON</option>
|
||||||
|
<option value="xml"{{ defaultRequestFormat == 'xml' ? ' selected' : '' }}>XML</option>
|
||||||
|
</select>
|
||||||
|
{% if authentication %}
|
||||||
|
api key: <input type="text" id="api_key"/>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
|
||||||
<br style="clear: both;" />
|
<br style="clear: both;" />
|
||||||
</div>
|
</div>
|
||||||
<div class="container" id="resources_container">
|
<div class="container" id="resources_container">
|
||||||
@ -37,42 +42,42 @@
|
|||||||
|
|
||||||
{% if enableSandbox %}
|
{% if enableSandbox %}
|
||||||
var toggleButtonText = function ($btn) {
|
var toggleButtonText = function ($btn) {
|
||||||
if ($btn.text() === 'Default') {
|
if ($btn.text() === 'Default') {
|
||||||
$btn.text('Raw');
|
$btn.text('Raw');
|
||||||
} else {
|
} else {
|
||||||
$btn.text('Default');
|
$btn.text('Default');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var renderRawBody = function ($container) {
|
var renderRawBody = function ($container) {
|
||||||
var rawData, $btn;
|
var rawData, $btn;
|
||||||
|
|
||||||
rawData = $container.data('raw-response');
|
rawData = $container.data('raw-response');
|
||||||
$btn = $container.parents('.pane').find('.to-raw');
|
$btn = $container.parents('.pane').find('.to-raw');
|
||||||
|
|
||||||
$container.addClass('prettyprinted');
|
$container.addClass('prettyprinted');
|
||||||
$container.html(rawData);
|
$container.html($('<div/>').text(rawData).html());
|
||||||
|
|
||||||
$btn.removeClass('to-raw');
|
$btn.removeClass('to-raw');
|
||||||
$btn.addClass('to-prettify');
|
$btn.addClass('to-prettify');
|
||||||
|
|
||||||
toggleButtonText($btn);
|
toggleButtonText($btn);
|
||||||
};
|
};
|
||||||
|
|
||||||
var renderPrettifiedBody = function ($container) {
|
var renderPrettifiedBody = function ($container) {
|
||||||
var rawData, $btn;
|
var rawData, $btn;
|
||||||
|
|
||||||
rawData = $container.data('raw-response');
|
rawData = $container.data('raw-response');
|
||||||
$btn = $container.parents('.pane').find('.to-prettify');
|
$btn = $container.parents('.pane').find('.to-prettify');
|
||||||
|
|
||||||
$container.removeClass('prettyprinted');
|
$container.removeClass('prettyprinted');
|
||||||
$container.html(prettifyResponse(rawData));
|
$container.html(prettifyResponse(rawData));
|
||||||
prettyPrint && prettyPrint();
|
prettyPrint && prettyPrint();
|
||||||
|
|
||||||
$btn.removeClass('to-prettify');
|
$btn.removeClass('to-prettify');
|
||||||
$btn.addClass('to-raw');
|
$btn.addClass('to-raw');
|
||||||
|
|
||||||
toggleButtonText($btn);
|
toggleButtonText($btn);
|
||||||
};
|
};
|
||||||
|
|
||||||
$('.tabs li').click(function() {
|
$('.tabs li').click(function() {
|
||||||
@ -88,7 +93,7 @@
|
|||||||
var prettifyResponse = function(text) {
|
var prettifyResponse = function(text) {
|
||||||
try {
|
try {
|
||||||
var data = typeof text === 'string' ? JSON.parse(text) : text;
|
var data = typeof text === 'string' ? JSON.parse(text) : text;
|
||||||
text = JSON.stringify(data, undefined, ' ');
|
text = JSON.stringify(data, undefined, ' ');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -105,11 +110,7 @@
|
|||||||
|
|
||||||
container.data('raw-response', data);
|
container.data('raw-response', data);
|
||||||
|
|
||||||
if ('<' === data[0]) {
|
renderPrettifiedBody(container);
|
||||||
renderRawBody(container);
|
|
||||||
} else {
|
|
||||||
renderPrettifiedBody(container);
|
|
||||||
}
|
|
||||||
|
|
||||||
container.parents('.pane').find('.to-prettify').text('Raw');
|
container.parents('.pane').find('.to-prettify').text('Raw');
|
||||||
container.parents('.pane').find('.to-raw').text('Raw');
|
container.parents('.pane').find('.to-raw').text('Raw');
|
||||||
@ -134,42 +135,48 @@
|
|||||||
var url = $(this).attr('action'),
|
var url = $(this).attr('action'),
|
||||||
method = $(this).attr('method'),
|
method = $(this).attr('method'),
|
||||||
self = this,
|
self = this,
|
||||||
params = {
|
params = {},
|
||||||
_format: 'json' // default format if not overriden in the form
|
|
||||||
},
|
|
||||||
headers = {},
|
headers = {},
|
||||||
content = $(this).find('textarea.content').val();
|
content = $(this).find('textarea.content').val(),
|
||||||
result_container = $('.result', $(this).parent());
|
result_container = $('.result', $(this).parent());
|
||||||
|
|
||||||
|
var requestFormat = $('#request_format').val();
|
||||||
|
var requestFormatMethod = '{{ requestFormatMethod }}';
|
||||||
|
if (requestFormatMethod == 'format_param') {
|
||||||
|
params['_format'] = requestFormat;
|
||||||
|
} else if (requestFormatMethod == 'accept_header') {
|
||||||
|
headers['Accept'] = 'application/' + requestFormat;
|
||||||
|
}
|
||||||
|
|
||||||
// retrieve all the parameters to send
|
// retrieve all the parameters to send
|
||||||
$('.parameters .tuple', $(this)).each(function() {
|
$('.parameters .tuple', $(this)).each(function() {
|
||||||
var key, value;
|
var key, value;
|
||||||
|
|
||||||
key = $('.key', $(this)).val();
|
key = $('.key', $(this)).val();
|
||||||
value = $('.value', $(this)).val();
|
value = $('.value', $(this)).val();
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
params[key] = value;
|
params[key] = value;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// retrieve the additional headers to send
|
// retrieve the additional headers to send
|
||||||
$('.headers .tuple', $(this)).each(function() {
|
$('.headers .tuple', $(this)).each(function() {
|
||||||
var key, value;
|
var key, value;
|
||||||
|
|
||||||
key = $('.key', $(this)).val();
|
key = $('.key', $(this)).val();
|
||||||
value = $('.value', $(this)).val();
|
value = $('.value', $(this)).val();
|
||||||
|
|
||||||
if (value) {
|
if (value) {
|
||||||
headers[key] = value;
|
headers[key] = value;
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// fix parameters in URL
|
// fix parameters in URL
|
||||||
for (var key in $.extend({}, params)) {
|
for (var key in $.extend({}, params)) {
|
||||||
if (url.indexOf('{' + key + '}') !== -1) {
|
if (url.indexOf('{' + key + '}') !== -1) {
|
||||||
url = url.replace('{' + key + '}', params[key]);
|
url = url.replace('{' + key + '}', params[key]);
|
||||||
delete params[key];
|
delete params[key];
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -184,16 +191,16 @@
|
|||||||
|
|
||||||
// and trigger the API call
|
// and trigger the API call
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '{{ endpoint }}' + url,
|
url: '{{ endpoint }}' + url,
|
||||||
type: method,
|
type: method,
|
||||||
data: content.length ? content : params,
|
data: content.length ? content : params,
|
||||||
headers: headers,
|
headers: headers,
|
||||||
complete: function(xhr) {
|
complete: function(xhr) {
|
||||||
displayResponse(xhr, method, url, result_container);
|
displayResponse(xhr, method, url, result_container);
|
||||||
|
|
||||||
// and enable them back
|
// and enable them back
|
||||||
$('input:not(.content-type), button', $(self)).removeAttr('disabled');
|
$('input:not(.content-type), button', $(self)).removeAttr('disabled');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user