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

[WIP] Implement CoreAPI #76

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package shell

import (
"context"
"errors"
"os"
"strings"
"path/filepath"
"io/ioutil"
"net/http"

homedir "github.com/mitchellh/go-homedir"
iface "github.com/ipfs/go-ipfs/core/coreapi/interface"
ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
)

const (
DefaultPathName = ".ipfs"
DefaultPathRoot = "~/" + DefaultPathName
DefaultApiFile = "api"
EnvDir = "IPFS_PATH"
)

type httpApi struct{
url string
client *http.Client
}

func NewLocalApi() (iface.CoreAPI, error) {
baseDir := os.Getenv(EnvDir)
if baseDir == "" {
baseDir = DefaultPathRoot
}

baseDir, err := homedir.Expand(baseDir)
if err != nil {
return nil, err
}

apiFile := filepath.Join(baseDir, DefaultApiFile)

if _, err := os.Stat(apiFile); err != nil {
return nil, err
}

api, err := ioutil.ReadFile(apiFile)
if err != nil {
return nil, err
}

return NewApi(strings.TrimSpace(string(api)))
}

func NewApi(url string) (iface.CoreAPI, error) {
if a, err := ma.NewMultiaddr(url); err == nil {
_, host, err := manet.DialArgs(a)
if err == nil {
url = host
}
}

return &httpApi{
url: url,
client: &http.Client{
Transport: &http.Transport{
DisableKeepAlives: true,
},
},
}, nil
}

// Unixfs returns the UnixfsAPI interface backed by the go-ipfs node
func (api *httpApi) Unixfs() iface.UnixfsAPI {
return nil
}

func (api *httpApi) Block() iface.BlockAPI {
return nil
}

// Dag returns the DagAPI interface backed by the go-ipfs node
func (api *httpApi) Dag() iface.DagAPI {
return nil
}

// Name returns the NameAPI interface backed by the go-ipfs node
func (api *httpApi) Name() iface.NameAPI {
return nil
}

// Key returns the KeyAPI interface backed by the go-ipfs node
func (api *httpApi) Key() iface.KeyAPI {
return (*httpKeyApi)(api)
}

//Object returns the ObjectAPI interface backed by the go-ipfs node
func (api *httpApi) Object() iface.ObjectAPI {
return nil
}

func (api *httpApi) Pin() iface.PinAPI {
return nil
}

// ResolveNode resolves the path `p` using Unixfs resolver, gets and returns the
// resolved Node.
func (api *httpApi) ResolveNode(ctx context.Context, p iface.Path) (iface.Node, error) {
return nil, errors.New("TODO")
}

// ResolvePath resolves the path `p` using Unixfs resolver, returns the
// resolved path.
func (api *httpApi) ResolvePath(ctx context.Context, p iface.Path) (iface.Path, error) {
return nil, errors.New("TODO")
}
76 changes: 76 additions & 0 deletions key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package shell

import (
"context"
"errors"
"encoding/json"

"github.com/ipfs/go-ipfs/core/coreapi/interface"
"github.com/ipfs/go-ipfs/core/coreapi/interface/options"
)

type httpKeyApi httpApi

type key struct {
KeyName string `json:"Name"`
Id string `json:"Id"`
}

func (k *key) Name() string {
return k.KeyName
}

func (k *key) Path() iface.Path {
return nil //TODO
}


func (api *httpKeyApi) Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (iface.Key, error) {
return nil, errors.New("TODO")
}

func (api *httpKeyApi) WithType(algorithm string) options.KeyGenerateOption {
return nil
}

func (api *httpKeyApi) WithSize(size int) options.KeyGenerateOption {
return nil
}

func (api *httpKeyApi) Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (iface.Key, bool, error) {
return nil, false, errors.New("TODO")
}

func (api *httpKeyApi) WithForce(force bool) options.KeyRenameOption {
return nil
}

func (api *httpKeyApi) List(ctx context.Context) ([]iface.Key, error) {
resp, err := api.core().newRequest(ctx, "key/list").Send(api.client)
if err != nil {
return nil, err
}

var res = struct{
Keys []*key
}{}

if err := json.NewDecoder(resp.Output).Decode(&res); err != nil {
return nil, err
}

out := make([]iface.Key, len(res.Keys))
for i, e := range res.Keys {
out[i] = e
}

return out, nil
}

func (api *httpKeyApi) Remove(ctx context.Context, name string) (iface.Path, error) {
return nil, errors.New("TODO")
}

func (api *httpKeyApi) core() *httpApi {
return (*httpApi)(api)
}
24 changes: 24 additions & 0 deletions key_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package shell

import (
"testing"
"context"
)

//TODO: create utils to run tests on a proper test node
func TestListSelf(t *testing.T) {
ctx := context.Background()
api, err := NewLocalApi()
if err != nil {
t.Fatal(err)
}

keys, err := api.Key().List(ctx)
if err != nil {
t.Fatalf("failed to list keys: %s", err)
}

if keys[0].Name() != "self" {
t.Errorf("expected the key to be called 'self', got '%s'", keys[0].Name())
}
}
19 changes: 19 additions & 0 deletions legacy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package shell

import (
gohttp "net/http"

"github.com/ipfs/go-ipfs-api/legacy"
)

func NewLocalShell() *legacy.Shell {
return legacy.NewLocalShell()
}

func NewShell(url string) *legacy.Shell {
return legacy.NewShell(url)
}

func NewShellWithClient(url string, c *gohttp.Client) *legacy.Shell {
return legacy.NewShellWithClient(url, c)
}
2 changes: 1 addition & 1 deletion dag.go → legacy/dag.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package shell
package legacy

import (
"bytes"
Expand Down
2 changes: 1 addition & 1 deletion ipns.go → legacy/ipns.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package shell
package legacy

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion pubsub.go → legacy/pubsub.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package shell
package legacy

import (
"encoding/binary"
Expand Down
Loading