forked from elastic/kibana
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move /shorten to /api/shorten_url (elastic#21808)
* Move /shorten to /api/shorten_url * better API test assertions * add API documenation * use async await * import dependencies instead of pass in
- Loading branch information
Showing
21 changed files
with
426 additions
and
215 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[[url-shortening-api]] | ||
== URL Shortening API | ||
|
||
Kibana URLs contain the state of the application making them very long and cumbersome. | ||
Internet Explorer has URL length restrictions, and some wiki and markup parsers don't do well with the full-length version of the Kibana URL. | ||
|
||
The short URLs enabled by this API are designed to make sharing Kibana URLs easier. | ||
|
||
* <<url-shortening-api-api-shorten-url>> | ||
|
||
include::url_shortening/shorten_url.asciidoc[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
[[url-shortening-api-api-shorten-url]] | ||
=== Shorten URL | ||
|
||
The Shorten URL API allows for converting a Kibana URL into a token. | ||
|
||
==== Request | ||
|
||
`POST /api/shorten_url` | ||
|
||
==== Request Body | ||
|
||
The request body must be a JSON object containing the following properties: | ||
|
||
`url` (required):: | ||
(string) Kibana URL, relative to `/app/kibana`, to be shortened. | ||
|
||
==== Response body | ||
|
||
The response body will have a top level `urlId` property that contains | ||
the shortened URL token for the provided request body. | ||
|
||
==== Examples | ||
|
||
|
||
[source,js] | ||
-------------------------------------------------- | ||
POST api/shorten_url | ||
{ | ||
"url": "/app/kibana#/dashboard?_g=()&_a=(description:'',filters:!(),fullScreenMode:!f,options:(darkTheme:!f,hidePanelTitles:!f,useMargins:!t),panels:!((embeddableConfig:(),gridData:(h:15,i:'1',w:24,x:0,y:0),id:'8f4d0c00-4c86-11e8-b3d7-01146121b73d',panelIndex:'1',type:visualization,version:'7.0.0-alpha1')),query:(language:lucene,query:''),timeRestore:!f,title:'New%20Dashboard',viewMode:edit)", | ||
} | ||
-------------------------------------------------- | ||
// KIBANA | ||
|
||
A successful call returns a response code of `200` and a response body | ||
containing a JSON structure similar to the following example: | ||
|
||
[source,js] | ||
-------------------------------------------------- | ||
{ | ||
"urlId": "f73b295ff92718b26bc94edac766d8e3" | ||
} | ||
-------------------------------------------------- | ||
|
||
A shortened Kibana URL can then be constructed for easier sharing. | ||
`http://localhost:5601/goto/f73b295ff92718b26bc94edac766d8e3` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
export { urlShorteningMixin } from './url_shortening_mixin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { handleShortUrlError } from './lib/short_url_error'; | ||
import { shortUrlAssertValid } from './lib/short_url_assert_valid'; | ||
import { shortUrlLookupProvider } from './lib/short_url_lookup'; | ||
import { createGotoRoute } from './goto'; | ||
import { createShortenUrlRoute } from './shorten_url'; | ||
|
||
|
||
export function createRoutes(server, config) { | ||
const shortUrlLookup = shortUrlLookupProvider(server); | ||
|
||
server.route(createGotoRoute({ server, config, shortUrlLookup })); | ||
server.route(createShortenUrlRoute({ shortUrlLookup })); | ||
|
||
// TODO remove deprecated '/shorten' API in master (7.0) | ||
server.route({ | ||
method: 'POST', | ||
path: '/shorten', | ||
handler: async function (request, reply) { | ||
server.log( | ||
['warning', 'deprecation'], | ||
`'/shorten' API has been deprecated and will be removed in 7.0, use the '/api/shorten_url' API instead`); | ||
try { | ||
shortUrlAssertValid(request.payload.url); | ||
const urlId = await shortUrlLookup.generateUrlId(request.payload.url, request); | ||
reply(urlId); | ||
} catch (err) { | ||
reply(handleShortUrlError(err)); | ||
} | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { handleShortUrlError } from './lib/short_url_error'; | ||
import { shortUrlAssertValid } from './lib/short_url_assert_valid'; | ||
|
||
export const createGotoRoute = ({ server, config, shortUrlLookup }) => ({ | ||
method: 'GET', | ||
path: '/goto/{urlId}', | ||
handler: async function (request, reply) { | ||
try { | ||
const url = await shortUrlLookup.getUrl(request.params.urlId, request); | ||
shortUrlAssertValid(url); | ||
|
||
const uiSettings = request.getUiSettingsService(); | ||
const stateStoreInSessionStorage = await uiSettings.get('state:storeInSessionStorage'); | ||
if (!stateStoreInSessionStorage) { | ||
reply().redirect(config.get('server.basePath') + url); | ||
return; | ||
} | ||
|
||
const app = server.getHiddenUiAppById('stateSessionStorageRedirect'); | ||
reply.renderApp(app, { | ||
redirectUrl: url, | ||
}); | ||
} catch (err) { | ||
reply(handleShortUrlError(err)); | ||
} | ||
} | ||
}); |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { handleShortUrlError } from './lib/short_url_error'; | ||
import { shortUrlAssertValid } from './lib/short_url_assert_valid'; | ||
|
||
export const createShortenUrlRoute = ({ shortUrlLookup }) => ({ | ||
method: 'POST', | ||
path: '/api/shorten_url', | ||
handler: async function (request, reply) { | ||
try { | ||
shortUrlAssertValid(request.payload.url); | ||
const urlId = await shortUrlLookup.generateUrlId(request.payload.url, request); | ||
reply({ urlId }); | ||
} catch (err) { | ||
reply(handleShortUrlError(err)); | ||
} | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
import { createRoutes } from './routes/create_routes'; | ||
|
||
export function urlShorteningMixin(kbnServer, server, config) { | ||
createRoutes(server, config); | ||
} |
Oops, something went wrong.