Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add python package total downloads from [pepy] badge #9564

Merged
merged 1 commit into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions services/pepy/pepy-downloads.service.js
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.`
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As noted in #4319 (comment) I've decided to add this as "python package total downloads from pepy", not "PyPI total downloads" due to inclusion of downloads from known mirrors in these stats.


export default class PepyDownloads extends BaseJsonService {
static category = 'downloads'

static route = {
base: 'pepy',
pattern: 'dt/:packageName',
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've only added total for now, but in principle we could make this :period(dw|dm|dt) or something in future without introducing a redirect

}

static openApi = {
'/pepy/dt/{packageName}': {
get: {
summary: 'Pepy Total Downlods',
description,
parameters: pathParams({
name: 'packageName',
example: 'django',
}),
},
},
}

static _cacheLength = 21600
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pepy only updates once per day so we can put quite a long cache on this


static defaultBadgeData = { label: 'downloads' }

async fetch({ packageName }) {
return this._requestJson({
url: `https://api.pepy.tech/api/v2/projects/${packageName}`,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed in psincraian/pepy#573 we might need to supply an API key here in future but for now, we'll start off using anonymous access.

schema,
})
}

async handle({ packageName }) {
const data = await this.fetch({ packageName })
return renderDownloadsBadge({
downloads: data.total_downloads,
})
}
}
11 changes: 11 additions & 0 deletions services/pepy/pepy-downloads.tester.js
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' })
Loading