Skip to content

Commit

Permalink
usageFetcher api
Browse files Browse the repository at this point in the history
  • Loading branch information
Bamieh committed Nov 4, 2019
1 parent 27d6b67 commit 0037d75
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/legacy/core_plugins/telemetry/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
*/

import { CoreSetup } from 'src/core/server';
import { registerOptInRoutes } from './opt_in';
import { registerTelemetryConfigRoutes } from './telemetry_config';
import { registerTelemetryDataRoutes } from './telemetry_stats';

export function registerRoutes(core: CoreSetup) {
registerOptInRoutes(core);
registerTelemetryConfigRoutes(core);
registerTelemetryDataRoutes(core);
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,36 @@ import Joi from 'joi';
import { boomify } from 'boom';
import { CoreSetup } from 'src/core/server';

export function registerOptInRoutes(core: CoreSetup) {
async function updateTelemetrySavedObjectFromReq(req: any) {
const savedObjectsClient = req.getSavedObjectsClient();
return await savedObjectsClient.update('telemetry', 'telemetry', req.payload);
}

export function registerTelemetryConfigRoutes(core: CoreSetup) {
const { server } = core.http as any;

server.route({
method: 'POST',
path: '/api/telemetry/v2/usageFetcher',
options: {
validate: {
payload: Joi.object({
usageFetcher: Joi.string()
.allow(['browser', 'server'])
.required(),
}),
},
},
handler: async (req: any, h: any) => {
try {
await updateTelemetrySavedObjectFromReq(req);
} catch (err) {
return boomify(err);
}
return h.response({}).code(200);
},
});

server.route({
method: 'POST',
path: '/api/telemetry/v2/optIn',
Expand All @@ -35,18 +62,8 @@ export function registerOptInRoutes(core: CoreSetup) {
},
},
handler: async (req: any, h: any) => {
const savedObjectsClient = req.getSavedObjectsClient();
try {
await savedObjectsClient.create(
'telemetry',
{
enabled: req.payload.enabled,
},
{
id: 'telemetry',
overwrite: true,
}
);
await updateTelemetrySavedObjectFromReq(req);
} catch (err) {
return boomify(err);
}
Expand Down

0 comments on commit 0037d75

Please sign in to comment.