From 10d271dfd4efc8eb34fe586562dcd94903ede635 Mon Sep 17 00:00:00 2001 From: chris48s Date: Mon, 11 Sep 2023 10:43:51 +0100 Subject: [PATCH] add python package total downloads from [pepy] badge --- services/pepy/pepy-downloads.service.js | 53 +++++++++++++++++++++++++ services/pepy/pepy-downloads.tester.js | 11 +++++ 2 files changed, 64 insertions(+) create mode 100644 services/pepy/pepy-downloads.service.js create mode 100644 services/pepy/pepy-downloads.tester.js diff --git a/services/pepy/pepy-downloads.service.js b/services/pepy/pepy-downloads.service.js new file mode 100644 index 0000000000000..8fd242c643162 --- /dev/null +++ b/services/pepy/pepy-downloads.service.js @@ -0,0 +1,53 @@ +import Joi from 'joi' +import { nonNegativeInteger } from '../validators.js' +import { BaseJsonService, pathParams } from '../index.js' +import { renderDownloadsBadge } from '../downloads.js' + +const schema = Joi.object({ + total_downloads: nonNegativeInteger, +}).required() + +const description = ` +Python package total downloads from [Pepy](https://www.pepy.tech/). + +Download stats from pepy count package downloads from PyPI and known mirrors.` + +export default class PepyDownloads extends BaseJsonService { + static category = 'downloads' + + static route = { + base: 'pepy', + pattern: 'dt/:packageName', + } + + static openApi = { + '/pepy/dt/{packageName}': { + get: { + summary: 'Pepy Total Downlods', + description, + parameters: pathParams({ + name: 'packageName', + example: 'django', + }), + }, + }, + } + + static _cacheLength = 21600 + + static defaultBadgeData = { label: 'downloads' } + + async fetch({ packageName }) { + return this._requestJson({ + url: `https://api.pepy.tech/api/v2/projects/${packageName}`, + schema, + }) + } + + async handle({ packageName }) { + const data = await this.fetch({ packageName }) + return renderDownloadsBadge({ + downloads: data.total_downloads, + }) + } +} diff --git a/services/pepy/pepy-downloads.tester.js b/services/pepy/pepy-downloads.tester.js new file mode 100644 index 0000000000000..a90172f7b1073 --- /dev/null +++ b/services/pepy/pepy-downloads.tester.js @@ -0,0 +1,11 @@ +import { createServiceTester } from '../tester.js' +import { isMetric } from '../test-validators.js' +export const t = await createServiceTester() + +t.create('downloads (valid)') + .get('/dt/django.json') + .expectBadge({ label: 'downloads', message: isMetric }) + +t.create('downloads (not found)') + .get('/dt/not-a-package.json') + .expectBadge({ label: 'downloads', message: 'not found' })