Skip to content

Commit

Permalink
feat(go): Update to go-ipfs-api v0.0.3
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Jan 21, 2020
1 parent 33d22cd commit efdf0d2
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 143 deletions.
10 changes: 1 addition & 9 deletions android/bridge/src/main/java/ipfs/gomobile/android/IPFS.java
Original file line number Diff line number Diff line change
Expand Up @@ -113,11 +113,7 @@ synchronized public void start()
throw new NodeStartException("Node start failed", e);
}

try {
shell = Ipfs.newUDSShell(absSockPath);
} catch (Exception e) {
throw new ShellInitException("Shell init failed", e);
}
shell = Ipfs.newUDSShell(absSockPath);
}

synchronized public void stop() throws NodeStopException {
Expand Down Expand Up @@ -170,10 +166,6 @@ public class SockManagerException extends Exception {
SockManagerException(String message, Throwable err) { super(message, err); }
}

public class ShellInitException extends Exception {
ShellInitException(String message, Throwable err) { super(message, err); }
}

public class ShellRequestException extends Exception {
ShellRequestException(String message) { super(message); }
}
Expand Down
93 changes: 7 additions & 86 deletions go/bind/ipfs/shell.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,23 @@
package ipfs

import (
"bytes"
"context"
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"

ipfs_api "github.com/ipfs/go-ipfs-api"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)

type Shell struct {
client *http.Client
url string
ishell *ipfs_api.Shell
url string
}

func NewShell(maddr string) (*Shell, error) {
var client *http.Client

a, err := ma.NewMultiaddr(maddr)
if err != nil {
return nil, fmt.Errorf("unable to parse multiaddr: %s", err)
}

_, host, err := manet.DialArgs(a)
if err != nil {
return nil, err
}

ma.ForEach(a, func(c ma.Component) bool {
switch c.Protocol().Code {
case ma.P_IP4, ma.P_IP6:
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
},
}
case ma.P_UNIX:
client = &http.Client{
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DisableKeepAlives: true,
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", c.Value())
},
},
}

host = "unix"
default:
return false
}

return true
})

if client == nil {
return nil, fmt.Errorf("unable to create a shell, `%s` is not supported", maddr)
func NewShell(url string) *Shell {
return &Shell{
ishell: ipfs_api.NewShell(url),
url: url,
}

ishell := ipfs_api.NewShellWithClient(host, client)
return &Shell{client, host, ishell}, nil
}

func (s *Shell) NewRequest(command string) *RequestBuilder {
Expand All @@ -76,35 +26,6 @@ func (s *Shell) NewRequest(command string) *RequestBuilder {
}
}

func (s *Shell) Request(uri string, body []byte) ([]byte, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}

command := strings.TrimLeft(u.EscapedPath(), "/")
ireq := ipfs_api.NewRequest(context.Background(), s.url, command)
if len(body) > 0 {
ireq.Body = bytes.NewReader(body)
}

for k, v := range u.Query() {
ireq.Opts[k] = strings.Join(v, ",")
}

res, err := ireq.Send(s.client)
if err != nil {
return nil, err
}

defer res.Close()
if res.Error != nil {
return nil, res.Error
}

return ioutil.ReadAll(res.Output)
}

type RequestBuilder struct {
rb *ipfs_api.RequestBuilder
}
Expand Down Expand Up @@ -154,10 +75,10 @@ func (req *RequestBuilder) Header(name, value string) {
// Helpers

// New unix socket domain shell
func NewUDSShell(sockpath string) (*Shell, error) {
func NewUDSShell(sockpath string) *Shell {
return NewShell("/unix/" + sockpath)
}

func NewTCPShell(port string) (*Shell, error) {
func NewTCPShell(port string) *Shell {
return NewShell("/ip4/127.0.0.1/tcp/" + port)
}
57 changes: 28 additions & 29 deletions go/bind/ipfs/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package ipfs

import (
"encoding/json"
"strings"
"testing"
)

func testIDRequest(t *testing.T, raw_json []byte) {
func testIDRequest(t *testing.T, node *Node, raw_json []byte) {
id := struct {
PeerID string `json:"id"`
}{}
Expand All @@ -16,27 +15,31 @@ func testIDRequest(t *testing.T, raw_json []byte) {
t.Fatal(err)
}

if !strings.HasPrefix(id.PeerID, "Qm") {
t.Fatalf("PeerID isn't prefixed by `Qm` got `%.2s` has prefix instead", id.PeerID)
nodeID := node.ipfsMobile.IpfsNode.Identity.String()
if nodeID != id.PeerID {
t.Fatalf("PeerID should be equal to `%s` got `%s`", nodeID, id.PeerID)
}
}

// const xkcbURI = "/ipns/xkcd.hacdias.com/latest/info.json"
func testConfigRequest(t *testing.T, node *Node, raw_json []byte) {
nodeConfig, err := node.ipfsMobile.IpfsNode.Repo.Config()
if err != nil {
t.Fatalf("unable to get node config: %s", err)
}

// func testCatRequest(t *testing.T, raw_json []byte) {
// latest := struct {
// Num int `json:"num"`
// }{}
config := struct {
Value string `json:"Value"`
}{}

// err := json.Unmarshal(raw_json, &latest)
// if err != nil {
// t.Fatal(err)
// }
err = json.Unmarshal(raw_json, &config)
if err != nil {
t.Fatal(err)
}

// if latest.Num == 0 {
// t.Fatalf("latest.Num should be > 0, got `%d`", latest.Num)
// }
// }
if nodeConfig.Identity.PeerID != config.Value {
t.Fatalf("config.Identity should be equal to `%s` got `%s`", nodeConfig.Identity.PeerID, config.Value)
}
}

func TestShell(t *testing.T) {
sm, clean := testingSockmanager(t)
Expand All @@ -61,13 +64,13 @@ func TestShell(t *testing.T) {
}

// commands
casesCommand := map[string]struct {
casesCommand := []struct {
Command string
Args []string
AssertMethod func(t *testing.T, raw_json []byte)
AssertMethod func(t *testing.T, node *Node, raw_json []byte)
}{
"id": {"id", []string{}, testIDRequest},
// "cat xkcb": {"cat", []string{xkcbURI}, testCatRequest},
{"id", []string{}, testIDRequest},
{"config", []string{"Identity.PeerID"}, testConfigRequest},
}

for clientk, clienttc := range casesClient {
Expand All @@ -77,13 +80,9 @@ func TestShell(t *testing.T) {
t.Fatal(err)
}

shell, err := NewShell(maddr)
if err != nil {
t.Fatal(err)
}

for cmdk, cmdtc := range casesCommand {
t.Run(cmdk, func(t *testing.T) {
shell := NewShell(maddr)
for _, cmdtc := range casesCommand {
t.Run(cmdtc.Command, func(t *testing.T) {
req := shell.NewRequest(cmdtc.Command)
for _, arg := range cmdtc.Args {
req.Argument(arg)
Expand All @@ -94,7 +93,7 @@ func TestShell(t *testing.T) {
t.Error(err)
}

cmdtc.AssertMethod(t, res)
cmdtc.AssertMethod(t, node, res)
})
}

Expand Down
3 changes: 1 addition & 2 deletions go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ go 1.13
require (
github.com/AndreasBriese/bbloom v0.0.0-20190825152654-46b345b51c96 // indirect
github.com/ipfs/go-ipfs v0.4.22-0.20200103222221-2b9a2d5eda90
github.com/ipfs/go-ipfs-api v0.0.2
github.com/ipfs/go-ipfs-api v0.0.3
github.com/ipfs/go-ipfs-config v0.2.0
github.com/libp2p/go-libp2p v0.5.0
github.com/libp2p/go-libp2p-core v0.3.0
github.com/libp2p/go-libp2p-metrics v0.1.0 // indirect
github.com/multiformats/go-multiaddr v0.2.0
github.com/multiformats/go-multiaddr-net v0.1.1
github.com/pkg/errors v0.8.1
Expand Down
7 changes: 4 additions & 3 deletions go/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ github.com/ipfs/go-fs-lock v0.0.3 h1:iZYGm2rJdZdTgUqRGXhsCfT2Porq6lV/5bRklTu5bCc
github.com/ipfs/go-fs-lock v0.0.3/go.mod h1:j2WZOYsBPcZG7UJUh1tE61Nj4eZ1vImU69AQd78qGuM=
github.com/ipfs/go-ipfs v0.4.22-0.20200103222221-2b9a2d5eda90 h1:JTPUcX5UqJD18S8/Hhv0Y7aahxMptNb8O/5uTvzavEY=
github.com/ipfs/go-ipfs v0.4.22-0.20200103222221-2b9a2d5eda90/go.mod h1:AGFzV7HF4vwO9FC3DcqAFgzXjrzXGWhPyqFIU374ov0=
github.com/ipfs/go-ipfs-api v0.0.2 h1:Yg+c8oAMVrN1m0OUGfrWBF+rhfqb7uJ123gU2g1giec=
github.com/ipfs/go-ipfs-api v0.0.2/go.mod h1:0FhXgCzrLu7qNmdxZvgYqD9jFzJxzz1NAVt3OQ0WOIc=
github.com/ipfs/go-ipfs-api v0.0.3 h1:1XZBfVDGj0GyyO5WItLrz2opCwezIm9LfFcBfe+sRxM=
github.com/ipfs/go-ipfs-api v0.0.3/go.mod h1:EgBqlEzrA22SnNKq4tcP2GDPKxbfF+uRTd2YFmR1uUk=
github.com/ipfs/go-ipfs-blockstore v0.0.1 h1:O9n3PbmTYZoNhkgkEyrXTznbmktIXif62xLX+8dPHzc=
github.com/ipfs/go-ipfs-blockstore v0.0.1/go.mod h1:d3WClOmRQKFnJ0Jz/jj/zmksX0ma1gROTlovZKBmN08=
github.com/ipfs/go-ipfs-blockstore v0.1.0/go.mod h1:5aD0AvHPi7mZc6Ci1WCAhiBQu2IsfTduLl+422H6Rqw=
Expand All @@ -229,12 +229,13 @@ github.com/ipfs/go-ipfs-exchange-interface v0.0.1 h1:LJXIo9W7CAmugqI+uofioIpRb6r
github.com/ipfs/go-ipfs-exchange-interface v0.0.1/go.mod h1:c8MwfHjtQjPoDyiy9cFquVtVHkO9b9Ob3FG91qJnWCM=
github.com/ipfs/go-ipfs-exchange-offline v0.0.1 h1:P56jYKZF7lDDOLx5SotVh5KFxoY6C81I1NSHW1FxGew=
github.com/ipfs/go-ipfs-exchange-offline v0.0.1/go.mod h1:WhHSFCVYX36H/anEKQboAzpUws3x7UeEGkzQc3iNkM0=
github.com/ipfs/go-ipfs-files v0.0.1/go.mod h1:INEFm0LL2LWXBhNJ2PMIIb2w45hpXgPjNoE7yA8Y1d4=
github.com/ipfs/go-ipfs-files v0.0.2/go.mod h1:INEFm0LL2LWXBhNJ2PMIIb2w45hpXgPjNoE7yA8Y1d4=
github.com/ipfs/go-ipfs-files v0.0.3 h1:ME+QnC3uOyla1ciRPezDW0ynQYK2ikOh9OCKAEg4uUA=
github.com/ipfs/go-ipfs-files v0.0.3/go.mod h1:INEFm0LL2LWXBhNJ2PMIIb2w45hpXgPjNoE7yA8Y1d4=
github.com/ipfs/go-ipfs-files v0.0.4 h1:WzRCivcybUQch/Qh6v8LBRhKtRsjnwyiuOV09mK7mrE=
github.com/ipfs/go-ipfs-files v0.0.4/go.mod h1:INEFm0LL2LWXBhNJ2PMIIb2w45hpXgPjNoE7yA8Y1d4=
github.com/ipfs/go-ipfs-files v0.0.6 h1:sMRtPiSmDrTA2FEiFTtk1vWgO2Dkg7bxXKJ+s8/cDAc=
github.com/ipfs/go-ipfs-files v0.0.6/go.mod h1:lVYE6sgAdtZN5825beJjSAHibw7WOBNPDWz5LaJeukg=
github.com/ipfs/go-ipfs-flags v0.0.1/go.mod h1:RnXBb9WV53GSfTrSDVK61NLTFKvWc60n+K9EgCDh+rA=
github.com/ipfs/go-ipfs-pinner v0.0.3 h1:ez/yNYYyH1W7DiCF/L29tmp6L7lBO8eqbJtPi2pHicA=
github.com/ipfs/go-ipfs-pinner v0.0.3/go.mod h1:s4kFZWLWGDudN8Jyd/GTpt222A12C2snA2+OTdy/7p8=
Expand Down
16 changes: 2 additions & 14 deletions ios/GomobileIPFS/Classes/IPFS.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ public class IPFS: NSObject {
self.absSockPath = ""
#endif


// init repo if needed
if !(try Repo.isInitialized(url: absRepoURL)) {
let config = try Config.defaultConfig()
Expand Down Expand Up @@ -76,29 +75,18 @@ public class IPFS: NSObject {
// init node
let node = try Node(repo)

// serve api
var err: NSError?

// Create a shell over UDS on normal devices
#if !targetEnvironment(simulator)
try node.serve(onUDS: self.absSockPath)
if let shell = IpfsNewUDSShell(self.absSockPath, &err) {
self.shell = shell
} else {
throw IPFSError("UDS shell creation failed", err)
}
self.shell = IpfsNewUDSShell(self.absSockPath)
/*
** On iOS simulator, temporary directory's absolute path exceeds
** the length limit for Unix Domain Socket, since simulator is
** only used for debug, we can safely fallback on shell over TCP
*/
#else
let maddr: String = try node.serve(onTCPPort: "0")
if let shell = IpfsNewShell(maddr, &err) {
self.shell = shell
} else {
throw IPFSError("TCP shell creation failed", err)
}
self.shell = IpfsNewShell(maddr)
#endif

self.repo = repo
Expand Down

0 comments on commit efdf0d2

Please sign in to comment.