From 77cb2c8e72af4e81324a818be901e65ce089161b Mon Sep 17 00:00:00 2001 From: JakobDev Date: Mon, 14 Mar 2022 16:17:35 +0100 Subject: [PATCH 1/3] [Flathub]Add downloads badge --- services/flathub/flathub-downloads.service.js | 31 +++++++++++++++++++ services/flathub/flathub-downloads.tester.js | 12 +++++++ 2 files changed, 43 insertions(+) create mode 100644 services/flathub/flathub-downloads.service.js create mode 100644 services/flathub/flathub-downloads.tester.js diff --git a/services/flathub/flathub-downloads.service.js b/services/flathub/flathub-downloads.service.js new file mode 100644 index 0000000000000..9645adaa9aa97 --- /dev/null +++ b/services/flathub/flathub-downloads.service.js @@ -0,0 +1,31 @@ +import Joi from 'joi' +import { BaseJsonService } from '../index.js' +import { renderDownloadsBadge } from '../downloads.js' + +const schema = Joi.object({ + downloads_total: Joi.number().integer().required(), +}).required() + +export default class FlathubDownloads extends BaseJsonService { + static category = 'downloads' + static route = { base: 'flathub/downloads', pattern: ':packageName' } + static examples = [ + { + title: 'Flathub', + namedParams: { + packageName: 'org.mozilla.firefox', + }, + staticPreview: renderDownloadsBadge({ downloads: '277136' }), + }, + ] + + static defaultBadgeData = { label: 'downloads' } + + async handle({ packageName }) { + const data = await this._requestJson({ + schema, + url: `https://flathub.org/api/v2/stats/${encodeURIComponent(packageName)}`, + }) + return renderDownloadsBadge({ downloads: data.downloads_total }) + } +} diff --git a/services/flathub/flathub-downloads.tester.js b/services/flathub/flathub-downloads.tester.js new file mode 100644 index 0000000000000..c77b73aaf1fc0 --- /dev/null +++ b/services/flathub/flathub-downloads.tester.js @@ -0,0 +1,12 @@ +import { isMetric } from '../test-validators.js' +import { createServiceTester } from '../tester.js' +export const t = await createServiceTester() + +t.create('Flathub Downloads (valid)').get('/org.mozilla.firefox.json').expectBadge({ + label: 'downloads', + message: isMetric, +}) + +t.create('Flathub Downloads (not found)') + .get('/not.a.package.json') + .expectBadge({ label: 'downloads', message: 'not found' }) From be62111d6df7c91ded51127315e970b0ada0ef8c Mon Sep 17 00:00:00 2001 From: JakobDev Date: Mon, 14 Mar 2022 17:10:00 +0100 Subject: [PATCH 2/3] Run prettier Signed-off-by: JakobDev --- services/flathub/flathub-downloads.service.js | 4 +++- services/flathub/flathub-downloads.tester.js | 10 ++++++---- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/services/flathub/flathub-downloads.service.js b/services/flathub/flathub-downloads.service.js index 9645adaa9aa97..a91c100663a63 100644 --- a/services/flathub/flathub-downloads.service.js +++ b/services/flathub/flathub-downloads.service.js @@ -24,7 +24,9 @@ export default class FlathubDownloads extends BaseJsonService { async handle({ packageName }) { const data = await this._requestJson({ schema, - url: `https://flathub.org/api/v2/stats/${encodeURIComponent(packageName)}`, + url: `https://flathub.org/api/v2/stats/${encodeURIComponent( + packageName + )}`, }) return renderDownloadsBadge({ downloads: data.downloads_total }) } diff --git a/services/flathub/flathub-downloads.tester.js b/services/flathub/flathub-downloads.tester.js index c77b73aaf1fc0..493ff94cc4a64 100644 --- a/services/flathub/flathub-downloads.tester.js +++ b/services/flathub/flathub-downloads.tester.js @@ -2,10 +2,12 @@ import { isMetric } from '../test-validators.js' import { createServiceTester } from '../tester.js' export const t = await createServiceTester() -t.create('Flathub Downloads (valid)').get('/org.mozilla.firefox.json').expectBadge({ - label: 'downloads', - message: isMetric, -}) +t.create('Flathub Downloads (valid)') + .get('/org.mozilla.firefox.json') + .expectBadge({ + label: 'downloads', + message: isMetric, + }) t.create('Flathub Downloads (not found)') .get('/not.a.package.json') From 034198f73b9bc923a95a7c696d33a0887d52bbfa Mon Sep 17 00:00:00 2001 From: JakobDev Date: Sun, 8 May 2022 21:47:01 +0200 Subject: [PATCH 3/3] Replace downloads_total with installs_total --- services/flathub/flathub-downloads.service.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/flathub/flathub-downloads.service.js b/services/flathub/flathub-downloads.service.js index a91c100663a63..bc5cde09348ba 100644 --- a/services/flathub/flathub-downloads.service.js +++ b/services/flathub/flathub-downloads.service.js @@ -3,7 +3,7 @@ import { BaseJsonService } from '../index.js' import { renderDownloadsBadge } from '../downloads.js' const schema = Joi.object({ - downloads_total: Joi.number().integer().required(), + installs_total: Joi.number().integer().required(), }).required() export default class FlathubDownloads extends BaseJsonService { @@ -28,6 +28,6 @@ export default class FlathubDownloads extends BaseJsonService { packageName )}`, }) - return renderDownloadsBadge({ downloads: data.downloads_total }) + return renderDownloadsBadge({ downloads: data.installs_total }) } }