diff --git a/services/galaxytoolshed/galaxytoolshed-base.js b/services/galaxytoolshed/galaxytoolshed-base.js new file mode 100644 index 0000000000000..5c15b3436b261 --- /dev/null +++ b/services/galaxytoolshed/galaxytoolshed-base.js @@ -0,0 +1,54 @@ +import Joi from 'joi' +import { nonNegativeInteger } from '../validators.js' +import { NotFound, BaseJsonService } from '../index.js' + +const orderedInstallableRevisionsSchema = Joi.array() + .items(Joi.string()) + .required() + +const repositoryRevisionInstallInfoSchema = Joi.array() + .ordered( + Joi.object({ + times_downloaded: nonNegativeInteger, + }).required() + ) + .items(Joi.any()) + +export default class BaseGalaxyToolshedService extends BaseJsonService { + static defaultBadgeData = { label: 'galaxytoolshed' } + static baseUrl = 'https://toolshed.g2.bx.psu.edu' + + async fetchOrderedInstallableRevisionsSchema({ repository, owner }) { + return this._requestJson({ + schema: orderedInstallableRevisionsSchema, + url: `${this.constructor.baseUrl}/api/repositories/get_ordered_installable_revisions?name=${repository}&owner=${owner}`, + }) + } + + async fetchRepositoryRevisionInstallInfoSchema({ + repository, + owner, + changesetRevision, + }) { + return this._requestJson({ + schema: repositoryRevisionInstallInfoSchema, + url: `${this.constructor.baseUrl}/api/repositories/get_repository_revision_install_info?name=${repository}&owner=${owner}&changeset_revision=${changesetRevision}`, + }) + } + + async fetchLastOrderedInstallableRevisionsSchema({ repository, owner }) { + const changesetRevisions = + await this.fetchOrderedInstallableRevisionsSchema({ + repository, + owner, + }) + if (!Array.isArray(changesetRevisions) || !changesetRevisions.length) { + throw new NotFound({ prettyMessage: 'changesetRevision not found' }) + } + return this.fetchRepositoryRevisionInstallInfoSchema({ + repository, + owner, + changesetRevision: changesetRevisions[0], + }) + } +} diff --git a/services/galaxytoolshed/galaxytoolshed-downloads.service.js b/services/galaxytoolshed/galaxytoolshed-downloads.service.js new file mode 100644 index 0000000000000..7a13907ffc769 --- /dev/null +++ b/services/galaxytoolshed/galaxytoolshed-downloads.service.js @@ -0,0 +1,34 @@ +import { renderDownloadsBadge } from '../downloads.js' +import BaseGalaxyToolshedService from './galaxytoolshed-base.js' + +export default class GalaxyToolshedDownloads extends BaseGalaxyToolshedService { + static category = 'downloads' + static route = { + base: 'galaxytoolshed/downloads', + pattern: ':repository/:owner', + } + + static examples = [ + { + title: 'Galaxy Toolshed - Downloads', + namedParams: { + repository: 'sra_tools', + owner: 'iuc', + }, + staticPreview: renderDownloadsBadge({ downloads: 10000 }), + }, + ] + + static defaultBadgeData = { + label: 'downloads', + } + + async handle({ repository, owner }) { + const response = await this.fetchLastOrderedInstallableRevisionsSchema({ + repository, + owner, + }) + const { times_downloaded: downloads } = response[0] + return renderDownloadsBadge({ downloads }) + } +} diff --git a/services/galaxytoolshed/galaxytoolshed-downloads.tester.js b/services/galaxytoolshed/galaxytoolshed-downloads.tester.js new file mode 100644 index 0000000000000..2695e297b781c --- /dev/null +++ b/services/galaxytoolshed/galaxytoolshed-downloads.tester.js @@ -0,0 +1,28 @@ +import { isMetric } from '../test-validators.js' +import { createServiceTester } from '../tester.js' + +export const t = await createServiceTester() + +t.create('downloads - raw').get('/sra_tools/iuc.json').expectBadge({ + label: 'downloads', + message: isMetric, +}) + +t.create('downloads - repository not found') + .get('/sra_tool/iuc.json') + .expectBadge({ + label: 'downloads', + message: 'not found', + }) + +t.create('downloads - owner not found').get('/sra_tools/iu.json').expectBadge({ + label: 'downloads', + message: 'not found', +}) + +t.create('downloads - changesetRevision not found') + .get('/bioqc/badilla.json') + .expectBadge({ + label: 'downloads', + message: 'changesetRevision not found', + })