Skip to content

Commit

Permalink
migrate examples to openApi part 15; affects [pingpong polymart spige…
Browse files Browse the repository at this point in the history
…t] (#9561)

* migrate some services from examples to openApi

also move common pingpong bits into a base class

* improve descriptions
  • Loading branch information
chris48s authored Dec 4, 2023
1 parent 3ca6091 commit 791e635
Show file tree
Hide file tree
Showing 13 changed files with 171 additions and 164 deletions.
22 changes: 22 additions & 0 deletions services/pingpong/pingpong-base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { BaseJsonService, InvalidParameter } from '../index.js'

export const description = `
[PingPong](https://pingpong.one/) is a status page and monitoring service.
To see more details about this badge and obtain your api key, visit
[https://my.pingpong.one/integrations/badge-status/](https://my.pingpong.one/integrations/badge-status/)
`

export const baseUrl = 'https://api.pingpong.one/widget/shields'

export class BasePingPongService extends BaseJsonService {
static category = 'monitoring'

static validateApiKey({ apiKey }) {
if (!apiKey.startsWith('sp_')) {
throw new InvalidParameter({
prettyMessage: 'invalid api key',
})
}
}
}
42 changes: 16 additions & 26 deletions services/pingpong/pingpong-status.service.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,29 @@
import Joi from 'joi'
import { BaseJsonService, InvalidParameter, InvalidResponse } from '../index.js'
import { InvalidResponse, pathParams } from '../index.js'
import { BasePingPongService, baseUrl, description } from './pingpong-base.js'

const schema = Joi.object({
status: Joi.string().required(),
}).required()

const pingpongDocumentation = `
To see more details about this badge and obtain your api key, visit
[https://my.pingpong.one/integrations/badge-status/](https://my.pingpong.one/integrations/badge-status/)
`

export default class PingPongStatus extends BaseJsonService {
static category = 'monitoring'
export default class PingPongStatus extends BasePingPongService {
static route = { base: 'pingpong/status', pattern: ':apiKey' }

static examples = [
{
title: 'PingPong status',
namedParams: { apiKey: 'sp_2e80bc00b6054faeb2b87e2464be337e' },
staticPreview: this.render({ status: 'Operational' }),
documentation: pingpongDocumentation,
keywords: ['statuspage', 'status page'],
static openApi = {
'/pingpong/status/{apiKey}': {
get: {
summary: 'PingPong status',
description,
parameters: pathParams({
name: 'apiKey',
example: 'sp_2e80bc00b6054faeb2b87e2464be337e',
}),
},
},
]
}

static defaultBadgeData = { label: 'status' }

static validateApiKey({ apiKey }) {
if (!apiKey.startsWith('sp_')) {
throw new InvalidParameter({
prettyMessage: 'invalid api key',
})
}
}

static render({ status }) {
switch (status) {
case 'Operational':
Expand All @@ -54,13 +44,13 @@ export default class PingPongStatus extends BaseJsonService {
async fetch({ apiKey }) {
return this._requestJson({
schema,
url: `https://api.pingpong.one/widget/shields/status/${apiKey}`,
url: `${baseUrl}/status/${apiKey}`,
})
}

async handle({ apiKey }) {
this.constructor.validateApiKey({ apiKey })
const { status } = await this.fetch({ apiKey })
const { status } = await this.fetch({ apiKey, schema })
return this.constructor.render({ status })
}
}
42 changes: 16 additions & 26 deletions services/pingpong/pingpong-uptime.service.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
import Joi from 'joi'
import { coveragePercentage } from '../color-formatters.js'
import { BaseJsonService, InvalidParameter } from '../index.js'
import { pathParams } from '../index.js'
import { BasePingPongService, baseUrl, description } from './pingpong-base.js'

const schema = Joi.object({
uptime: Joi.number().min(0).max(100).required(),
}).required()

const pingpongDocumentation = `
To see more details about this badge and obtain your api key, visit
[https://my.pingpong.one/integrations/badge-status/](https://my.pingpong.one/integrations/badge-status/)
`

export default class PingPongUptime extends BaseJsonService {
static category = 'monitoring'
export default class PingPongUptime extends BasePingPongService {
static route = { base: 'pingpong/uptime', pattern: ':apiKey' }

static examples = [
{
title: 'PingPong uptime (last 30 days)',
namedParams: { apiKey: 'sp_2e80bc00b6054faeb2b87e2464be337e' },
staticPreview: this.render({ uptime: 100 }),
documentation: pingpongDocumentation,
keywords: ['statuspage', 'status page'],
static openApi = {
'/pingpong/uptime/{apiKey}': {
get: {
summary: 'PingPong uptime (last 30 days)',
description,
parameters: pathParams({
name: 'apiKey',
example: 'sp_2e80bc00b6054faeb2b87e2464be337e',
}),
},
},
]
}

static defaultBadgeData = { label: 'uptime' }

static validateApiKey({ apiKey }) {
if (!apiKey.startsWith('sp_')) {
throw new InvalidParameter({
prettyMessage: 'invalid api key',
})
}
}

static render({ uptime }) {
return {
message: `${uptime}%`,
Expand All @@ -45,13 +35,13 @@ export default class PingPongUptime extends BaseJsonService {
async fetch({ apiKey }) {
return this._requestJson({
schema,
url: `https://api.pingpong.one/widget/shields/uptime/${apiKey}`,
url: `${baseUrl}/uptime/${apiKey}`,
})
}

async handle({ apiKey }) {
this.constructor.validateApiKey({ apiKey })
const { uptime } = await this.fetch({ apiKey })
const { uptime } = await this.fetch({ apiKey, schema })
return this.constructor.render({ uptime })
}
}
4 changes: 2 additions & 2 deletions services/polymart/polymart-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const resourceFoundOrNotSchema = Joi.alternatives(
notFoundResourceSchema,
)

const documentation = `
const description = `
<p>You can find your resource ID in the url for your resource page.</p>
<p>Example: <code>https://polymart.org/resource/polymart-plugin.323</code> - Here the Resource ID is 323.</p>`

Expand All @@ -48,4 +48,4 @@ class BasePolymartService extends BaseJsonService {
}
}

export { documentation, BasePolymartService }
export { description, BasePolymartService }
21 changes: 12 additions & 9 deletions services/polymart/polymart-downloads.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { pathParams } from '../index.js'
import { NotFound } from '../../core/base-service/errors.js'
import { renderDownloadsBadge } from '../downloads.js'
import { BasePolymartService, documentation } from './polymart-base.js'
import { BasePolymartService, description } from './polymart-base.js'

export default class PolymartDownloads extends BasePolymartService {
static category = 'downloads'
Expand All @@ -10,16 +11,18 @@ export default class PolymartDownloads extends BasePolymartService {
pattern: ':resourceId',
}

static examples = [
{
title: 'Polymart Downloads',
namedParams: {
resourceId: '323',
static openApi = {
'/polymart/downloads/{resourceId}': {
get: {
summary: 'Polymart Downloads',
description,
parameters: pathParams({
name: 'resourceId',
example: '323',
}),
},
staticPreview: renderDownloadsBadge({ downloads: 655 }),
documentation,
},
]
}

static defaultBadgeData = {
label: 'downloads',
Expand Down
24 changes: 13 additions & 11 deletions services/polymart/polymart-latest-version.service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { pathParams } from '../index.js'
import { NotFound } from '../../core/base-service/errors.js'
import { renderVersionBadge } from '../version.js'
import { BasePolymartService, documentation } from './polymart-base.js'
import { BasePolymartService, description } from './polymart-base.js'

export default class PolymartLatestVersion extends BasePolymartService {
static category = 'version'

Expand All @@ -9,18 +11,18 @@ export default class PolymartLatestVersion extends BasePolymartService {
pattern: ':resourceId',
}

static examples = [
{
title: 'Polymart Version',
namedParams: {
resourceId: '323',
static openApi = {
'/polymart/version/{resourceId}': {
get: {
summary: 'Polymart Version',
description,
parameters: pathParams({
name: 'resourceId',
example: '323',
}),
},
staticPreview: renderVersionBadge({
version: 'v1.2.9',
}),
documentation,
},
]
}

static defaultBadgeData = {
label: 'polymart',
Expand Down
42 changes: 19 additions & 23 deletions services/polymart/polymart-rating.service.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { pathParams } from '../index.js'
import { starRating, metric } from '../text-formatters.js'
import { floorCount } from '../color-formatters.js'
import { NotFound } from '../../core/base-service/errors.js'
import { BasePolymartService, documentation } from './polymart-base.js'
import { BasePolymartService, description } from './polymart-base.js'

export default class PolymartRatings extends BasePolymartService {
static category = 'rating'
Expand All @@ -11,30 +12,25 @@ export default class PolymartRatings extends BasePolymartService {
pattern: ':format(rating|stars)/:resourceId',
}

static examples = [
{
title: 'Polymart Stars',
pattern: 'stars/:resourceId',
namedParams: {
resourceId: '323',
static openApi = {
'/polymart/{format}/{resourceId}': {
get: {
summary: 'Polymart Rating',
description,
parameters: pathParams(
{
name: 'format',
example: 'rating',
schema: { type: 'string', enum: this.getEnum('format') },
},
{
name: 'resourceId',
example: '323',
},
),
},
staticPreview: this.render({
format: 'stars',
total: 14,
average: 5,
}),
documentation,
},
{
title: 'Polymart Rating',
pattern: 'rating/:resourceId',
namedParams: {
resourceId: '323',
},
staticPreview: this.render({ total: 14, average: 5 }),
documentation,
},
]
}

static defaultBadgeData = {
label: 'rating',
Expand Down
7 changes: 3 additions & 4 deletions services/spiget/spiget-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ const resourceSchema = Joi.object({
}).required(),
}).required()

const documentation = `
const description = `
<p><a href="https://spiget.org/">Spiget</a> holds information about SpigotMC Resources, Plugins and Authors.</p>
<p>You can find your resource ID in the url for your resource page.</p>
<p>Example: <code>https://www.spigotmc.org/resources/essentialsx.9089/</code> - Here the Resource ID is 9089.</p>`

const keywords = ['spigot', 'spigotmc']

class BaseSpigetService extends BaseJsonService {
async fetch({
resourceId,
Expand All @@ -34,4 +33,4 @@ class BaseSpigetService extends BaseJsonService {
}
}

export { keywords, documentation, BaseSpigetService }
export { description, BaseSpigetService }
22 changes: 13 additions & 9 deletions services/spiget/spiget-download-size.service.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseSpigetService, documentation, keywords } from './spiget-base.js'
import { pathParams } from '../index.js'
import { BaseSpigetService, description } from './spiget-base.js'

export default class SpigetDownloadSize extends BaseSpigetService {
static category = 'size'
Expand All @@ -8,15 +9,18 @@ export default class SpigetDownloadSize extends BaseSpigetService {
pattern: ':resourceId',
}

static examples = [
{
title: 'Spiget Download Size',
namedParams: { resourceId: '15904' },
staticPreview: this.render({ size: 2.5, unit: 'MB' }),
documentation,
keywords,
static openApi = {
'/spiget/download-size/{resourceId}': {
get: {
summary: 'Spiget Download Size',
description,
parameters: pathParams({
name: 'resourceId',
example: '15904',
}),
},
},
]
}

static defaultBadgeData = {
label: 'size',
Expand Down
Loading

0 comments on commit 791e635

Please sign in to comment.