diff --git a/client/app/components/queries/api-key-dialog.js b/client/app/components/queries/api-key-dialog.js
index 796f186219..6f4451b3b0 100644
--- a/client/app/components/queries/api-key-dialog.js
+++ b/client/app/components/queries/api-key-dialog.js
@@ -4,26 +4,46 @@ const ApiKeyDialog = {
API Key
-
{{$ctrl.apiKey}}
+
Example API Calls:
Results in CSV format:
-
{{$ctrl.csvUrl}}
+
{{$ctrl.csvUrlBase + $ctrl.query.api_key}}
Results in JSON format:
-
{{$ctrl.jsonUrl}}
+
{{$ctrl.jsonUrlBase + $ctrl.query.api_key}}
`,
- controller(clientConfig) {
+ controller($http, clientConfig, currentUser) {
'ngInject';
- this.apiKey = this.resolve.query.api_key;
- this.csvUrl = `${clientConfig.basePath}api/queries/${this.resolve.query.id}/results.csv?api_key=${this.apiKey}`;
- this.jsonUrl = `${clientConfig.basePath}api/queries/${this.resolve.query.id}/results.json?api_key=${this.apiKey}`;
+ this.canEdit = currentUser.id === this.resolve.query.user.id || currentUser.hasPermission('admin');
+ this.disableRegenerateApiKeyButton = false;
+ this.query = this.resolve.query;
+ this.csvUrlBase = `${clientConfig.basePath}api/queries/${this.resolve.query.id}/results.csv?api_key=`;
+ this.jsonUrlBase = `${clientConfig.basePath}api/queries/${this.resolve.query.id}/results.json?api_key=`;
+
+ this.regenerateQueryApiKey = () => {
+ this.disableRegenerateApiKeyButton = true;
+ $http
+ .post(`api/queries/${this.resolve.query.id}/regenerate_api_key`)
+ .success((data) => {
+ this.query.api_key = data.api_key;
+ this.disableRegenerateApiKeyButton = false;
+ })
+ .error(() => {
+ this.disableRegenerateApiKeyButton = false;
+ });
+ };
},
bindings: {
resolve: '<',
diff --git a/redash/handlers/api.py b/redash/handlers/api.py
index 012c121f63..4479ca12ea 100644
--- a/redash/handlers/api.py
+++ b/redash/handlers/api.py
@@ -35,7 +35,8 @@
QueryForkResource, QueryListResource,
QueryRecentResource, QueryRefreshResource,
QueryResource, QuerySearchResource,
- QueryTagsResource)
+ QueryTagsResource,
+ QueryRegenerateApiKeyResource)
from redash.handlers.query_results import (JobResource,
QueryResultDropdownResource,
QueryDropdownsResource,
@@ -115,6 +116,9 @@ def json_representation(data, code, headers=None):
api.add_org_resource(QueryRefreshResource, '/api/queries/