/* REST API Controller library for the /ipfs route */ import wlogger from '../../../adapters/wlogger.js' class IpfsRESTControllerLib { constructor (localConfig = {}) { // Dependency Injection. this.adapters = localConfig.adapters if (!this.adapters) { throw new Error( 'Instance of Adapters library required when instantiating /ipfs REST Controller.' ) } this.useCases = localConfig.useCases if (!this.useCases) { throw new Error( 'Instance of Use Cases library required when instantiating /ipfs REST Controller.' ) } // Encapsulate dependencies // this.UserModel = this.adapters.localdb.Users // this.userUseCases = this.useCases.user // Bind 'this' object to all subfunctions this.getStatus = this.getStatus.bind(this) this.getPeers = this.getPeers.bind(this) this.getRelays = this.getRelays.bind(this) this.handleError = this.handleError.bind(this) } /** * @api {get} /ipfs Get status on IPFS infrastructure * @apiPermission public * @apiName GetIpfsStatus * @apiGroup REST BCH * * @apiExample Example usage: * curl -H "Content-Type: application/json" -X GET localhost:5001/ipfs * */ async getStatus (ctx) { try { const status = await this.adapters.ipfs.getStatus() ctx.body = { status } } catch (err) { wlogger.error('Error in ipfs/controller.js/getStatus(): ') // ctx.throw(422, err.message) this.handleError(ctx, err) } } // Return information on IPFS peers this node is connected to. async getPeers (ctx) { try { const showAll = ctx.request.body.showAll const peers = await this.adapters.ipfs.getPeers(showAll) ctx.body = { peers } } catch (err) { wlogger.error('Error in ipfs/controller.js/getPeers(): ') // ctx.throw(422, err.message) this.handleError(ctx, err) } } // Get data about the known Circuit Relays. Hydrate with data from peers list. async getRelays (ctx) { try { const relays = await this.adapters.ipfs.getRelays() ctx.body = { relays } } catch (err) { wlogger.error('Error in ipfs/controller.js/getRelays(): ') // ctx.throw(422, err.message) this.handleError(ctx, err) } } // DRY error handler handleError (ctx, err) { // If an HTTP status is specified by the buisiness logic, use that. if (err.status) { if (err.message) { ctx.throw(err.status, err.message) } else { ctx.throw(err.status) } } else { // By default use a 422 error if the HTTP status is not specified. ctx.throw(422, err.message) } } } // module.exports = IpfsRESTControllerLib export default IpfsRESTControllerLib