diff --git a/services/flathub/flathub-downloads.service.js b/services/flathub/flathub-downloads.service.js new file mode 100644 index 0000000000000..bc5cde09348ba --- /dev/null +++ b/services/flathub/flathub-downloads.service.js @@ -0,0 +1,33 @@ +import Joi from 'joi' +import { BaseJsonService } from '../index.js' +import { renderDownloadsBadge } from '../downloads.js' + +const schema = Joi.object({ + installs_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.installs_total }) + } +} diff --git a/services/flathub/flathub-downloads.tester.js b/services/flathub/flathub-downloads.tester.js new file mode 100644 index 0000000000000..493ff94cc4a64 --- /dev/null +++ b/services/flathub/flathub-downloads.tester.js @@ -0,0 +1,14 @@ +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' })