mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
Added a configuration parameter to determine the default body format.
This commit is contained in:
parent
705e01625e
commit
a9f0613cee
@ -46,6 +46,10 @@ class Configuration implements ConfigurationInterface
|
||||
->scalarNode('enabled')->defaultTrue()->end()
|
||||
->scalarNode('endpoint')->defaultNull()->end()
|
||||
->scalarNode('accept_type')->defaultValue('')->end()
|
||||
->enumNode('body_format')
|
||||
->values(array('form', 'json'))
|
||||
->defaultValue('form')
|
||||
->end()
|
||||
->arrayNode('request_format')
|
||||
->addDefaultsIfNotSet()
|
||||
->children()
|
||||
|
@ -34,6 +34,7 @@ class NelmioApiDocExtension extends Extension
|
||||
$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.accept_type', $config['sandbox']['accept_type']);
|
||||
$container->setParameter('nelmio_api_doc.sandbox.body_format', $config['sandbox']['body_format']);
|
||||
$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'));
|
||||
|
@ -50,6 +50,11 @@ class HtmlFormatter extends AbstractFormatter
|
||||
*/
|
||||
private $acceptType;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $bodyFormat;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
@ -108,6 +113,14 @@ class HtmlFormatter extends AbstractFormatter
|
||||
$this->acceptType = $acceptType;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $bodyFormat
|
||||
*/
|
||||
public function setBodyFormat($bodyFormat)
|
||||
{
|
||||
$this->bodyFormat = $bodyFormat;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $method
|
||||
*/
|
||||
@ -179,6 +192,7 @@ class HtmlFormatter extends AbstractFormatter
|
||||
'enableSandbox' => $this->enableSandbox,
|
||||
'requestFormatMethod' => $this->requestFormatMethod,
|
||||
'acceptType' => $this->acceptType,
|
||||
'bodyFormat' => $this->bodyFormat,
|
||||
'defaultRequestFormat' => $this->defaultRequestFormat,
|
||||
'date' => date(DATE_RFC822),
|
||||
'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),
|
||||
|
@ -49,6 +49,9 @@
|
||||
<call method="setAcceptType">
|
||||
<argument>%nelmio_api_doc.sandbox.accept_type%</argument>
|
||||
</call>
|
||||
<call method="setBodyFormat">
|
||||
<argument>%nelmio_api_doc.sandbox.body_format%</argument>
|
||||
</call>
|
||||
<call method="setAuthentication">
|
||||
<argument>%nelmio_api_doc.sandbox.authentication%</argument>
|
||||
</call>
|
||||
|
@ -16,10 +16,10 @@
|
||||
<div id="header">
|
||||
<a href=""><h1>{{ apiName }}</h1></a>
|
||||
<div id="sandbox_configuration">
|
||||
content format:
|
||||
<select id="content_format">
|
||||
<option value="x-www-form-urlencoded">Form Data</option>
|
||||
<option value="json">JSON</option>
|
||||
body format:
|
||||
<select id="body_format">
|
||||
<option value="x-www-form-urlencoded"{{ bodyFormat == 'form' ? ' selected' : '' }}>Form Data</option>
|
||||
<option value="json"{{ bodyFormat == 'json' ? ' selected' : '' }}>JSON</option>
|
||||
</select>
|
||||
request format:
|
||||
<select id="request_format">
|
||||
@ -158,6 +158,7 @@
|
||||
method = method.split('|').sort().pop();
|
||||
}
|
||||
|
||||
// set default requestFormat
|
||||
var requestFormat = $('#request_format').val();
|
||||
var requestFormatMethod = '{{ requestFormatMethod }}';
|
||||
if (requestFormatMethod == 'format_param') {
|
||||
@ -166,8 +167,12 @@
|
||||
headers['Accept'] = 'application/' + requestFormat;
|
||||
}
|
||||
|
||||
// set content format
|
||||
var contentFormat = $('#content_format').val();
|
||||
// set default bodyFormat
|
||||
var bodyFormat = $('#body_format').val();
|
||||
|
||||
if(!('Content-type' in headers)) {
|
||||
headers['Content-type'] = 'application/'+bodyFormat;
|
||||
}
|
||||
|
||||
// retrieve all the parameters to send
|
||||
$('.parameters .tuple', $(this)).each(function() {
|
||||
@ -192,9 +197,6 @@
|
||||
headers[key] = value;
|
||||
}
|
||||
|
||||
if(!('Content-type' in headers)) {
|
||||
headers['Content-type'] = 'application/'+contentFormat;
|
||||
}
|
||||
});
|
||||
|
||||
// fix parameters in URL
|
||||
@ -226,13 +228,11 @@
|
||||
|
||||
// prepare final parameters
|
||||
var body = params;
|
||||
if(contentFormat == 'json') {
|
||||
if(bodyFormat == 'json') {
|
||||
body = JSON.stringify(body);
|
||||
}
|
||||
var data = content.length ? content : body;
|
||||
|
||||
console.log(contentFormat); console.log(data);
|
||||
|
||||
// and trigger the API call
|
||||
$.ajax({
|
||||
url: endpoint + url,
|
||||
|
Loading…
x
Reference in New Issue
Block a user