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 [CratesUserDownloads] service and tester #10619

Merged
merged 6 commits into from
Oct 20, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
30 changes: 29 additions & 1 deletion services/crates/crates-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,21 @@ const versionResponseSchema = Joi.object({
version: versionSchema.required(),
}).required()

const userStatsSchema = Joi.object({
total_downloads: nonNegativeInteger.required(),
}).required()

class BaseCratesService extends BaseJsonService {
static defaultBadgeData = { label: 'crates.io' }

/**
* Fetches data from the crates.io API.
*
* @param {object} options - The options for the request
* @param {string} options.crate - The crate name.
* @param {string} [options.version] - The crate version number (optional).
* @returns {Promise<object>} the JSON response from the API.
*/
async fetch({ crate, version }) {
const url = version
? `https://crates.io/api/v1/crates/${crate}/${version}`
Expand Down Expand Up @@ -54,7 +66,23 @@ class BaseCratesService extends BaseJsonService {
}
}

class BaseCratesUserService extends BaseJsonService {
static defaultBadgeData = { label: 'crates.io' }

/**
* Fetches data from the crates.io API.
*
* @param {object} options - The options for the request
* @param {string} options.userId - The user ID.
* @returns {Promise<object>} the JSON response from the API.
*/
async fetch({ userId }) {
const url = `https://crates.io/api/v1/users/${userId}/stats`
return this._requestJson({ schema: userStatsSchema, url })
}
}

const description =
'[Crates.io](https://crates.io/) is a package registry for Rust.'

export { BaseCratesService, description }
export { BaseCratesService, BaseCratesUserService, description }
32 changes: 32 additions & 0 deletions services/crates/crates-user-downloads.service.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { renderDownloadsBadge } from '../downloads.js'
import { pathParams } from '../index.js'
import { BaseCratesUserService, description } from './crates-base.js'

export default class CratesUserDownloads extends BaseCratesUserService {
static category = 'downloads'
static route = {
base: 'crates',
pattern: 'udt/:userId',
}

static openApi = {
'/crates/udt/{userId}': {
get: {
summary: 'Crates.io User Total Downloads',
description,
parameters: pathParams({
name: 'userId',
example: '3027',
description:
'The user ID can be found using https://crates.io/api/v1/users/{username}',
Copy link
Member

Choose a reason for hiding this comment

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

This renders as a link in the docs, but following it doesn't take you anywhere useful. Can we put it in `backticks` so it renders a <code> block instead of a link

Copy link
Collaborator Author

@jNullj jNullj Oct 18, 2024

Choose a reason for hiding this comment

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

Yea, it looks better with code block
Made changes at 2982c8f

}),
},
},
}

async handle({ userId }) {
const json = await this.fetch({ userId })
const { total_downloads: downloads } = json
return renderDownloadsBadge({ downloads, labelOverride: 'downloads' })
}
}
13 changes: 13 additions & 0 deletions services/crates/crates-user-downloads.tester.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { isMetric } from '../test-validators.js'
import { createServiceTester } from '../tester.js'
export const t = await createServiceTester()

t.create('total user downloads')
.get('/udt/3027.json')
.expectBadge({ label: 'downloads', message: isMetric })

// we can not test for id not linked to user as it returns 0 downloads
Copy link
Member

Choose a reason for hiding this comment

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

I reckon we should put in a test case for a non-existant user id anyway, .expectBadge({ label: 'downloads', message: '0' }) in the assertion and explain in a comment that this API returns {"total_downloads": 0} with a 200 OK instead of a 404 response for a not found user id.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Added the test with 230c79a.
I realized they use int 32 for userid so i picked 2^32 as its the last id which has the least chance of getting taken.


t.create('total user downloads (invalid)')
.get('/udt/999999999999999999999999.json')
.expectBadge({ label: 'crates.io', message: 'invalid' })
Loading