{id}.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. // Storage providers discoveryClient must use ipfs client and not rely
  41. // on joystream http discovery to avoid potentially an infinite request loop
  42. // back to our own api endpoint.
  43. if (!discoveryClient.ipfs) {
  44. return res.status(500)
  45. }
  46. const info = await discoveryClient.discover(id, USE_CACHE, cacheMaxAge)
  47. if (info === null) {
  48. debug('info not found')
  49. res.status(404).end()
  50. } else {
  51. res.status(200).send(info)
  52. }
  53. } catch (err) {
  54. debug(`${err}`)
  55. res.status(404).end()
  56. }
  57. },
  58. }
  59. // OpenAPI specs
  60. doc.get.apiDoc = {
  61. description: 'Resolve Service Information',
  62. operationId: 'discover',
  63. // tags: ['asset', 'data'],
  64. responses: {
  65. 200: {
  66. description: 'Wrapped JSON Service Information',
  67. content: {
  68. 'application/json': {
  69. schema: {
  70. required: ['serialized'],
  71. properties: {
  72. serialized: {
  73. type: 'string',
  74. },
  75. signature: {
  76. type: 'string',
  77. },
  78. },
  79. },
  80. },
  81. },
  82. },
  83. },
  84. }
  85. return doc
  86. }