mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-02 15:51:48 +03:00
Merge pull request #64 from asm89/sandbox-api-key
Add optional api_key parameter to sandbox
This commit is contained in:
commit
6795e118ae
@ -28,6 +28,19 @@ class Configuration implements ConfigurationInterface
|
||||
->children()
|
||||
->scalarNode('enabled')->defaultTrue()->end()
|
||||
->scalarNode('endpoint')->defaultValue('/app_dev.php')->end()
|
||||
->arrayNode('authentication')
|
||||
->children()
|
||||
->scalarNode('name')->isRequired()->end()
|
||||
->scalarNode('delivery')
|
||||
->isRequired()
|
||||
->validate()
|
||||
// header|query|request, but only query is implemented for now
|
||||
->ifNotInArray(array('query'))
|
||||
->thenInvalid("Unknown authentication delivery type '%s'.")
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end()
|
||||
->end();
|
||||
|
@ -36,5 +36,9 @@ class NelmioApiDocExtension extends Extension
|
||||
$loader->load('formatters.xml');
|
||||
$loader->load('request_listener.xml');
|
||||
$loader->load('services.xml');
|
||||
|
||||
if (isset($config['sandbox']['authentication'])) {
|
||||
$container->setParameter('nelmio_api_doc.sandbox.authentication', $config['sandbox']['authentication']);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -15,6 +15,11 @@ use Symfony\Component\Templating\EngineInterface;
|
||||
|
||||
class HtmlFormatter extends AbstractFormatter
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $authentication;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
@ -35,6 +40,14 @@ class HtmlFormatter extends AbstractFormatter
|
||||
*/
|
||||
private $engine;
|
||||
|
||||
/**
|
||||
* @param array $authentication
|
||||
*/
|
||||
public function setAuthentication(array $authentication = null)
|
||||
{
|
||||
$this->authentication = $authentication;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $apiName
|
||||
*/
|
||||
@ -95,12 +108,13 @@ class HtmlFormatter extends AbstractFormatter
|
||||
private function getGlobalVars()
|
||||
{
|
||||
return array(
|
||||
'apiName' => $this->apiName,
|
||||
'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,
|
||||
'date' => date(DATE_RFC822),
|
||||
'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),
|
||||
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -179,6 +179,9 @@ configure this sandbox using the following parameters:
|
||||
# app/config/config.yml
|
||||
nelmio_api_doc:
|
||||
sandbox:
|
||||
authentication: # default null, if set, the value of the api key is read from the query string and appended to every sandbox api call
|
||||
name: access_token
|
||||
delivery: query # only query delivery is supported for now
|
||||
enabled: true # default: true, you can set this parameter to `false` to disable the sandbox
|
||||
endpoint: http://sandbox.example.com/ # default: /app_dev.php, use this parameter to define which URL to call through the sandbox
|
||||
|
||||
|
@ -9,6 +9,7 @@
|
||||
<parameter key="nelmio_api_doc.formatter.markdown_formatter.class">Nelmio\ApiDocBundle\Formatter\MarkdownFormatter</parameter>
|
||||
<parameter key="nelmio_api_doc.formatter.simple_formatter.class">Nelmio\ApiDocBundle\Formatter\SimpleFormatter</parameter>
|
||||
<parameter key="nelmio_api_doc.formatter.html_formatter.class">Nelmio\ApiDocBundle\Formatter\HtmlFormatter</parameter>
|
||||
<parameter key="nelmio_api_doc.sandbox.authentication">null</parameter>
|
||||
</parameters>
|
||||
|
||||
<services>
|
||||
@ -35,6 +36,9 @@
|
||||
<call method="setEndpoint">
|
||||
<argument>%nelmio_api_doc.sandbox.endpoint%</argument>
|
||||
</call>
|
||||
<call method="setAuthentication">
|
||||
<argument>%nelmio_api_doc.sandbox.authentication%</argument>
|
||||
</call>
|
||||
</service>
|
||||
</services>
|
||||
|
||||
|
@ -141,12 +141,18 @@ table tbody tr:last-child td {
|
||||
|
||||
#header h1 {
|
||||
font-size: 1.2em;
|
||||
float: left;
|
||||
}
|
||||
|
||||
#header a {
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
#api_key_wrapper {
|
||||
float: right;
|
||||
padding: 10px 0 10px 0;
|
||||
}
|
||||
|
||||
#colophon {
|
||||
margin: 0 15px 40px 15px;
|
||||
padding: 10px 0;
|
||||
|
@ -15,6 +15,12 @@
|
||||
<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>
|
||||
{% endif %}
|
||||
<br style="clear: both;" />
|
||||
</div>
|
||||
<div class="container" id="resources_container">
|
||||
<ul id="resources">
|
||||
@ -170,6 +176,12 @@
|
||||
// disable all the fiels and buttons
|
||||
$('input, button', $(this)).attr('disabled', 'disabled');
|
||||
|
||||
// append the api key
|
||||
if (api_key_parameter) {
|
||||
url += url.indexOf('?') > 0 ? '&' : '?';
|
||||
url += api_key_parameter + '=' + $('#api_key').val();
|
||||
}
|
||||
|
||||
// and trigger the API call
|
||||
$.ajax({
|
||||
url: '{{ endpoint }}' + url,
|
||||
@ -257,6 +269,25 @@
|
||||
$element.find('input.value').val(content_type);
|
||||
|
||||
});
|
||||
|
||||
{% if authentication %}
|
||||
var api_key_parameter = '{{ authentication.name }}';
|
||||
var search = window.location.search;
|
||||
|
||||
var api_key_start = search.indexOf(api_key_parameter) + api_key_parameter.length + 1;
|
||||
|
||||
if (api_key_start > 0 ) {
|
||||
var api_key_end = search.indexOf('&', api_key_start);
|
||||
|
||||
var api_key = -1 == api_key_end
|
||||
? search.substr(api_key_start)
|
||||
: search.substring(api_key_start, api_key_end);
|
||||
|
||||
$('#api_key').val(api_key);
|
||||
}
|
||||
{% else %}
|
||||
var api_key_parameter = false;
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
</script>
|
||||
</body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user