Add optional api_key parameter to sandbox

This commit is contained in:
Alexander 2012-08-10 13:55:35 +02:00
parent 4b69e1149a
commit fc5b8c4e63
7 changed files with 81 additions and 6 deletions

View File

@ -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();

View File

@ -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']);
}
}
}

View File

@ -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
*/
@ -96,6 +109,7 @@ class HtmlFormatter extends AbstractFormatter
{
return array(
'apiName' => $this->apiName,
'authentication' => $this->authentication,
'endpoint' => $this->endpoint,
'enableSandbox' => $this->enableSandbox,
'date' => date(DATE_RFC822),

View File

@ -177,6 +177,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

View File

@ -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>

View File

@ -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;

View File

@ -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>