-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add python package total downloads from [pepy] badge
- Loading branch information
Showing
2 changed files
with
64 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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, | ||
}) | ||
} | ||
} |
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 @@ | ||
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' }) |