-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Changes from 3 commits
2b04fe2
b973b0b
0e0c10f
2982c8f
230c79a
7e64911
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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}', | ||
}), | ||
}, | ||
}, | ||
} | ||
|
||
async handle({ userId }) { | ||
const json = await this.fetch({ userId }) | ||
const { total_downloads: downloads } = json | ||
return renderDownloadsBadge({ downloads, labelOverride: 'downloads' }) | ||
} | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added the test with 230c79a. |
||
|
||
t.create('total user downloads (invalid)') | ||
.get('/udt/999999999999999999999999.json') | ||
.expectBadge({ label: 'crates.io', message: 'invalid' }) |
There was a problem hiding this comment.
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 linkThere was a problem hiding this comment.
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