mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +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()
|
||||
->scalarNode('enabled')->defaultTrue()->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')
|
||||
->children()
|
||||
->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.sandbox.enabled', $config['sandbox']['enabled']);
|
||||
$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->load('formatters.xml');
|
||||
|
@ -40,6 +40,16 @@ class HtmlFormatter extends AbstractFormatter
|
||||
*/
|
||||
private $engine;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $requestFormatMethod;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $defaultRequestFormat;
|
||||
|
||||
/**
|
||||
* @param array $authentication
|
||||
*/
|
||||
@ -80,6 +90,19 @@ class HtmlFormatter extends AbstractFormatter
|
||||
$this->engine = $engine;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
*/
|
||||
public function setRequestFormatMethod($method)
|
||||
{
|
||||
$this->requestFormatMethod = $method;
|
||||
}
|
||||
|
||||
public function setDefaultRequestFormat($format)
|
||||
{
|
||||
$this->defaultRequestFormat = $format;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
@ -133,13 +156,15 @@ class HtmlFormatter extends AbstractFormatter
|
||||
private function getGlobalVars()
|
||||
{
|
||||
return array(
|
||||
'apiName' => $this->apiName,
|
||||
'authentication' => $this->authentication,
|
||||
'endpoint' => $this->endpoint,
|
||||
'enableSandbox' => $this->enableSandbox,
|
||||
'date' => date(DATE_RFC822),
|
||||
'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),
|
||||
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
|
||||
'apiName' => $this->apiName,
|
||||
'authentication' => $this->authentication,
|
||||
'endpoint' => $this->endpoint,
|
||||
'enableSandbox' => $this->enableSandbox,
|
||||
'requestFormatMethod' => $this->requestFormatMethod,
|
||||
'defaultRequestFormat' => $this->defaultRequestFormat,
|
||||
'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">
|
||||
<argument>%nelmio_api_doc.sandbox.endpoint%</argument>
|
||||
</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">
|
||||
<argument>%nelmio_api_doc.sandbox.authentication%</argument>
|
||||
</call>
|
||||
|
@ -148,9 +148,9 @@ table tbody tr:last-child td {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
#api_key_wrapper {
|
||||
float: right;
|
||||
padding: 10px 0 10px 0;
|
||||
#sandbox_configuration {
|
||||
float: right;
|
||||
padding: 10px 0 10px 0;
|
||||
}
|
||||
|
||||
#colophon {
|
||||
|
@ -15,11 +15,16 @@
|
||||
<body>
|
||||
<div id="header">
|
||||
<a href=""><h1>{{ apiName }}</h1></a>
|
||||
{% if authentication %}
|
||||
<div id="api_key_wrapper">
|
||||
api key: <input type="text" id="api_key"/>
|
||||
<div id="sandbox_configuration">
|
||||
request format:
|
||||
<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>
|
||||
{% endif %}
|
||||
<br style="clear: both;" />
|
||||
</div>
|
||||
<div class="container" id="resources_container">
|
||||
@ -37,42 +42,42 @@
|
||||
|
||||
{% if enableSandbox %}
|
||||
var toggleButtonText = function ($btn) {
|
||||
if ($btn.text() === 'Default') {
|
||||
$btn.text('Raw');
|
||||
} else {
|
||||
$btn.text('Default');
|
||||
}
|
||||
if ($btn.text() === 'Default') {
|
||||
$btn.text('Raw');
|
||||
} else {
|
||||
$btn.text('Default');
|
||||
}
|
||||
};
|
||||
|
||||
var renderRawBody = function ($container) {
|
||||
var rawData, $btn;
|
||||
var rawData, $btn;
|
||||
|
||||
rawData = $container.data('raw-response');
|
||||
$btn = $container.parents('.pane').find('.to-raw');
|
||||
rawData = $container.data('raw-response');
|
||||
$btn = $container.parents('.pane').find('.to-raw');
|
||||
|
||||
$container.addClass('prettyprinted');
|
||||
$container.html(rawData);
|
||||
$container.addClass('prettyprinted');
|
||||
$container.html($('<div/>').text(rawData).html());
|
||||
|
||||
$btn.removeClass('to-raw');
|
||||
$btn.addClass('to-prettify');
|
||||
$btn.removeClass('to-raw');
|
||||
$btn.addClass('to-prettify');
|
||||
|
||||
toggleButtonText($btn);
|
||||
toggleButtonText($btn);
|
||||
};
|
||||
|
||||
var renderPrettifiedBody = function ($container) {
|
||||
var rawData, $btn;
|
||||
var rawData, $btn;
|
||||
|
||||
rawData = $container.data('raw-response');
|
||||
$btn = $container.parents('.pane').find('.to-prettify');
|
||||
rawData = $container.data('raw-response');
|
||||
$btn = $container.parents('.pane').find('.to-prettify');
|
||||
|
||||
$container.removeClass('prettyprinted');
|
||||
$container.html(prettifyResponse(rawData));
|
||||
prettyPrint && prettyPrint();
|
||||
$container.removeClass('prettyprinted');
|
||||
$container.html(prettifyResponse(rawData));
|
||||
prettyPrint && prettyPrint();
|
||||
|
||||
$btn.removeClass('to-prettify');
|
||||
$btn.addClass('to-raw');
|
||||
$btn.removeClass('to-prettify');
|
||||
$btn.addClass('to-raw');
|
||||
|
||||
toggleButtonText($btn);
|
||||
toggleButtonText($btn);
|
||||
};
|
||||
|
||||
$('.tabs li').click(function() {
|
||||
@ -88,7 +93,7 @@
|
||||
var prettifyResponse = function(text) {
|
||||
try {
|
||||
var data = typeof text === 'string' ? JSON.parse(text) : text;
|
||||
text = JSON.stringify(data, undefined, ' ');
|
||||
text = JSON.stringify(data, undefined, ' ');
|
||||
} catch (err) {
|
||||
}
|
||||
|
||||
@ -105,11 +110,7 @@
|
||||
|
||||
container.data('raw-response', data);
|
||||
|
||||
if ('<' === data[0]) {
|
||||
renderRawBody(container);
|
||||
} else {
|
||||
renderPrettifiedBody(container);
|
||||
}
|
||||
renderPrettifiedBody(container);
|
||||
|
||||
container.parents('.pane').find('.to-prettify').text('Raw');
|
||||
container.parents('.pane').find('.to-raw').text('Raw');
|
||||
@ -134,42 +135,48 @@
|
||||
var url = $(this).attr('action'),
|
||||
method = $(this).attr('method'),
|
||||
self = this,
|
||||
params = {
|
||||
_format: 'json' // default format if not overriden in the form
|
||||
},
|
||||
params = {},
|
||||
headers = {},
|
||||
content = $(this).find('textarea.content').val();
|
||||
content = $(this).find('textarea.content').val(),
|
||||
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
|
||||
$('.parameters .tuple', $(this)).each(function() {
|
||||
var key, value;
|
||||
var key, value;
|
||||
|
||||
key = $('.key', $(this)).val();
|
||||
value = $('.value', $(this)).val();
|
||||
key = $('.key', $(this)).val();
|
||||
value = $('.value', $(this)).val();
|
||||
|
||||
if (value) {
|
||||
params[key] = value;
|
||||
}
|
||||
if (value) {
|
||||
params[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
// retrieve the additional headers to send
|
||||
$('.headers .tuple', $(this)).each(function() {
|
||||
var key, value;
|
||||
var key, value;
|
||||
|
||||
key = $('.key', $(this)).val();
|
||||
value = $('.value', $(this)).val();
|
||||
key = $('.key', $(this)).val();
|
||||
value = $('.value', $(this)).val();
|
||||
|
||||
if (value) {
|
||||
headers[key] = value;
|
||||
}
|
||||
if (value) {
|
||||
headers[key] = value;
|
||||
}
|
||||
});
|
||||
|
||||
// fix parameters in URL
|
||||
for (var key in $.extend({}, params)) {
|
||||
if (url.indexOf('{' + key + '}') !== -1) {
|
||||
url = url.replace('{' + key + '}', params[key]);
|
||||
delete params[key];
|
||||
url = url.replace('{' + key + '}', params[key]);
|
||||
delete params[key];
|
||||
}
|
||||
};
|
||||
|
||||
@ -184,16 +191,16 @@
|
||||
|
||||
// and trigger the API call
|
||||
$.ajax({
|
||||
url: '{{ endpoint }}' + url,
|
||||
type: method,
|
||||
data: content.length ? content : params,
|
||||
headers: headers,
|
||||
complete: function(xhr) {
|
||||
displayResponse(xhr, method, url, result_container);
|
||||
url: '{{ endpoint }}' + url,
|
||||
type: method,
|
||||
data: content.length ? content : params,
|
||||
headers: headers,
|
||||
complete: function(xhr) {
|
||||
displayResponse(xhr, method, url, result_container);
|
||||
|
||||
// and enable them back
|
||||
$('input:not(.content-type), button', $(self)).removeAttr('disabled');
|
||||
}
|
||||
// and enable them back
|
||||
$('input:not(.content-type), button', $(self)).removeAttr('disabled');
|
||||
}
|
||||
});
|
||||
|
||||
return false;
|
||||
|
Loading…
x
Reference in New Issue
Block a user