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()
|
->children()
|
||||||
->scalarNode('enabled')->defaultTrue()->end()
|
->scalarNode('enabled')->defaultTrue()->end()
|
||||||
->scalarNode('endpoint')->defaultValue('/app_dev.php')->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()
|
->end()
|
||||||
->end();
|
->end();
|
||||||
|
@ -36,5 +36,9 @@ class NelmioApiDocExtension extends Extension
|
|||||||
$loader->load('formatters.xml');
|
$loader->load('formatters.xml');
|
||||||
$loader->load('request_listener.xml');
|
$loader->load('request_listener.xml');
|
||||||
$loader->load('services.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
|
class HtmlFormatter extends AbstractFormatter
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
private $authentication;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
@ -35,6 +40,14 @@ class HtmlFormatter extends AbstractFormatter
|
|||||||
*/
|
*/
|
||||||
private $engine;
|
private $engine;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array $authentication
|
||||||
|
*/
|
||||||
|
public function setAuthentication(array $authentication = null)
|
||||||
|
{
|
||||||
|
$this->authentication = $authentication;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param string $apiName
|
* @param string $apiName
|
||||||
*/
|
*/
|
||||||
@ -95,12 +108,13 @@ class HtmlFormatter extends AbstractFormatter
|
|||||||
private function getGlobalVars()
|
private function getGlobalVars()
|
||||||
{
|
{
|
||||||
return array(
|
return array(
|
||||||
'apiName' => $this->apiName,
|
'apiName' => $this->apiName,
|
||||||
'endpoint' => $this->endpoint,
|
'authentication' => $this->authentication,
|
||||||
'enableSandbox' => $this->enableSandbox,
|
'endpoint' => $this->endpoint,
|
||||||
'date' => date(DATE_RFC822),
|
'enableSandbox' => $this->enableSandbox,
|
||||||
'css' => file_get_contents(__DIR__ . '/../Resources/public/css/screen.css'),
|
'date' => date(DATE_RFC822),
|
||||||
'js' => file_get_contents(__DIR__ . '/../Resources/public/js/all.js'),
|
'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
|
# app/config/config.yml
|
||||||
nelmio_api_doc:
|
nelmio_api_doc:
|
||||||
sandbox:
|
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
|
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
|
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.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.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.formatter.html_formatter.class">Nelmio\ApiDocBundle\Formatter\HtmlFormatter</parameter>
|
||||||
|
<parameter key="nelmio_api_doc.sandbox.authentication">null</parameter>
|
||||||
</parameters>
|
</parameters>
|
||||||
|
|
||||||
<services>
|
<services>
|
||||||
@ -35,6 +36,9 @@
|
|||||||
<call method="setEndpoint">
|
<call method="setEndpoint">
|
||||||
<argument>%nelmio_api_doc.sandbox.endpoint%</argument>
|
<argument>%nelmio_api_doc.sandbox.endpoint%</argument>
|
||||||
</call>
|
</call>
|
||||||
|
<call method="setAuthentication">
|
||||||
|
<argument>%nelmio_api_doc.sandbox.authentication%</argument>
|
||||||
|
</call>
|
||||||
</service>
|
</service>
|
||||||
</services>
|
</services>
|
||||||
|
|
||||||
|
@ -141,12 +141,18 @@ table tbody tr:last-child td {
|
|||||||
|
|
||||||
#header h1 {
|
#header h1 {
|
||||||
font-size: 1.2em;
|
font-size: 1.2em;
|
||||||
|
float: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
#header a {
|
#header a {
|
||||||
text-decoration: none;
|
text-decoration: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#api_key_wrapper {
|
||||||
|
float: right;
|
||||||
|
padding: 10px 0 10px 0;
|
||||||
|
}
|
||||||
|
|
||||||
#colophon {
|
#colophon {
|
||||||
margin: 0 15px 40px 15px;
|
margin: 0 15px 40px 15px;
|
||||||
padding: 10px 0;
|
padding: 10px 0;
|
||||||
|
@ -15,6 +15,12 @@
|
|||||||
<body>
|
<body>
|
||||||
<div id="header">
|
<div id="header">
|
||||||
<a href=""><h1>{{ apiName }}</h1></a>
|
<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>
|
||||||
<div class="container" id="resources_container">
|
<div class="container" id="resources_container">
|
||||||
<ul id="resources">
|
<ul id="resources">
|
||||||
@ -170,6 +176,12 @@
|
|||||||
// disable all the fiels and buttons
|
// disable all the fiels and buttons
|
||||||
$('input, button', $(this)).attr('disabled', 'disabled');
|
$('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
|
// and trigger the API call
|
||||||
$.ajax({
|
$.ajax({
|
||||||
url: '{{ endpoint }}' + url,
|
url: '{{ endpoint }}' + url,
|
||||||
@ -257,6 +269,25 @@
|
|||||||
$element.find('input.value').val(content_type);
|
$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 %}
|
{% endif %}
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user