{id}.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. const debug = require('debug')('joystream:colossus:api:discovery')
  2. const MAX_CACHE_AGE = 30 * 60 * 1000
  3. const USE_CACHE = true
  4. module.exports = function (discoveryClient) {
  5. const doc = {
  6. // parameters for all operations in this path
  7. parameters: [
  8. {
  9. name: 'id',
  10. in: 'path',
  11. required: true,
  12. description: 'Storage Provider Id',
  13. schema: {
  14. type: 'string', // integer ?
  15. },
  16. },
  17. ],
  18. // Resolve Service Information
  19. async get(req, res) {
  20. let parsedId
  21. try {
  22. parsedId = parseInt(req.params.id)
  23. } catch (err) {
  24. return res.status(400).end()
  25. }
  26. const id = parsedId
  27. let cacheMaxAge = req.query.max_age
  28. if (cacheMaxAge) {
  29. try {
  30. cacheMaxAge = parseInt(cacheMaxAge)
  31. } catch (err) {
  32. cacheMaxAge = MAX_CACHE_AGE
  33. }
  34. } else {
  35. cacheMaxAge = 0
  36. }
  37. // todo - validate id before querying
  38. try {
  39. debug(`resolving ${id}`)
  40. const info = await discoveryClient.discover(id, USE_CACHE, cacheMaxAge)
  41. if (info === null) {
  42. debug('info not found')
  43. res.status(404).end()
  44. } else {
  45. res.status(200).send(info)
  46. }
  47. } catch (err) {
  48. debug(`${err}`)
  49. res.status(404).end()
  50. }
  51. },
  52. }
  53. // OpenAPI specs
  54. doc.get.apiDoc = {
  55. description: 'Resolve Service Information',
  56. operationId: 'discover',
  57. // tags: ['asset', 'data'],
  58. responses: {
  59. 200: {
  60. description: 'Wrapped JSON Service Information',
  61. content: {
  62. 'application/json': {
  63. schema: {
  64. required: ['serialized'],
  65. properties: {
  66. serialized: {
  67. type: 'string',
  68. },
  69. signature: {
  70. type: 'string',
  71. },
  72. },
  73. },
  74. },
  75. },
  76. },
  77. },
  78. }
  79. return doc
  80. }