Skip to content
This repository has been archived by the owner on Feb 12, 2024. It is now read-only.

Commit

Permalink
feat(http): make interface-ipfs-core compliant
Browse files Browse the repository at this point in the history
Fixes #439
  • Loading branch information
dignifiedquire committed Sep 15, 2016
1 parent d3f98fe commit 8145dfb
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 27 deletions.
Empty file added current.json
Empty file.
59 changes: 51 additions & 8 deletions src/http-api/resources/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ exports.parseAddrs = (request, reply) => {
}

exports.peers = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
request.server.app.ipfs.swarm.peers((err, peers) => {
const ipfs = request.server.app.ipfs
ipfs.swarm.peers((err, peers) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -37,18 +37,35 @@ exports.peers = {
}

return reply({
Strings: Object.keys(peers)
.map((key) =>
`${peers[key].multiaddrs[0].toString()}/ipfs/${peers[key].id.toB58String()}`)
Strings: peers.map((addr) => addr.toString())
})
})
}
}

exports.addrs = {
handler: (request, reply) => {
const ipfs = request.server.app.ipfs
ipfs.libp2p.swarm.addrs((err, addrs) => {
if (err) {
log.error(err)
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

return reply({
Addrs: addrs.map((addr) => addr.toString())
})
})
}
}

exports.localAddrs = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
request.server.app.ipfs.swarm.localAddrs((err, addrs) => {
const ipfs = request.server.app.ipfs
ipfs.swarm.localAddrs((err, addrs) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -71,8 +88,9 @@ exports.connect = {
// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const addr = request.pre.args.addr
const ipfs = request.server.app.ipfs

request.server.app.ipfs.swarm.connect(addr, (err) => {
ipfs.swarm.connect(addr, (err) => {
if (err) {
log.error(err)
return reply({
Expand All @@ -87,3 +105,28 @@ exports.connect = {
})
}
}

exports.disconnect = {
// uses common parseAddr method that returns a `addr`
parseArgs: exports.parseAddrs,

// main route handler which is called after the above `parseArgs`, but only if the args were valid
handler: (request, reply) => {
const addr = request.pre.args.addr
const ipfs = request.server.app.ipfs

ipfs.libp2p.swarm.disconnect(addr, (err) => {
if (err) {
log.error(err)
return reply({
Message: err.toString(),
Code: 0
}).code(500)
}

return reply({
Strings: [`disconnect ${addr} success`]
})
})
}
}
31 changes: 17 additions & 14 deletions src/http-api/routes/swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ module.exports = (server) => {
}
})

// api.route({
// method: '*',
// path: '/api/v0/swarm/addrs',
// config: {
// handler: resources.swarm.addrs.handler
// }
// })
api.route({
method: '*',
path: '/api/v0/swarm/addrs',
config: {
handler: resources.swarm.addrs.handler
}
})

api.route({
method: '*',
Expand All @@ -40,13 +40,16 @@ module.exports = (server) => {
}
})

// api.route({
// method: '*',
// path: '/api/v0/swarm/disconnect',
// config: {
// handler: resources.swarm.disconnect
// }
// })
api.route({
method: '*',
path: '/api/v0/swarm/disconnect',
config: {
pre: [
{ method: resources.swarm.disconnect.parseArgs, assign: 'args' }
],
handler: resources.swarm.disconnect.handler
}
})

// TODO
// api.route({
Expand Down
7 changes: 2 additions & 5 deletions test/http-api/interface-ipfs-core-over-ipfs-api/test-swarm.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

'use strict'

/*
const test = require('interface-ipfs-core')
const FactoryClient = require('./../../utils/factory-http')

Expand All @@ -17,7 +16,5 @@ const common = {
fc.dismantle(callback)
}
}
*/
// TODO
// Needs: https://github.com/ipfs/js-libp2p-ipfs/pull/16
// test.swarm(common)

test.swarm(common)

0 comments on commit 8145dfb

Please sign in to comment.