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

Add additional publish function for more fine grained control of IPNS record publishing #91

Merged
merged 7 commits into from
Jul 6, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
34 changes: 34 additions & 0 deletions ipns.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package shell

import (
"bytes"
"context"
"encoding/json"
"strconv"
"time"
)

type PublishResponse struct {
Name string `json:"name"`
Value string `json:"value"`
}

// Publish updates a mutable name to point to a given value
func (s *Shell) Publish(node string, value string) error {
args := []string{value}
Expand All @@ -25,6 +33,32 @@ func (s *Shell) Publish(node string, value string) error {
return nil
}

// PublishWithDetails is used for fine grained control over record publishing
func (s *Shell) PublishWithDetails(contentHash, key string, lifetime, ttl time.Duration, resolve bool) (*PublishResponse, error) {
var pubResp PublishResponse
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd put it above/closer to json.Unmarshal line

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! :D

args := []string{contentHash}
req := s.newRequest(context.Background(), "name/publish", args...)
if key == "" {
key = "self"
}
req.Opts["key"] = key
req.Opts["lifetime"] = lifetime.String()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd wrap this and ttl with an if and only set this when duration is > 0

go-ipfs cares whether or not a option is present - https://github.com/ipfs/go-ipfs/blob/master/core/commands/publish.go#L117

req.Opts["ttl"] = ttl.String()
req.Opts["resolve"] = strconv.FormatBool(resolve)
resp, err := req.Send(s.httpcli)
if err != nil {
return nil, err
}
defer resp.Close()
if resp.Error != nil {
return nil, resp.Error
}
buf := new(bytes.Buffer)
buf.ReadFrom(resp.Output)
json.Unmarshal(buf.Bytes(), &pubResp)
return &pubResp, nil
}

// Resolve gets resolves the string provided to an /ipfs/[hash]. If asked to
// resolve an empty string, resolve instead resolves the node's own /ipns value.
func (s *Shell) Resolve(id string) (string, error) {
Expand Down
37 changes: 37 additions & 0 deletions ipns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package shell_test

import (
"fmt"
"testing"
"time"

ipfsapi "github.com/RTradeLtd/go-ipfs-api"
)

var examplesHash = "/ipfs/Qmbu7x6gJbsKDcseQv66pSbUcAA3Au6f7MfTYVXwvBxN2K"

func TestPublishDetailsWithKey(t *testing.T) {
shell := ipfsapi.NewShell("localhost:5001")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

other tests do NewShell(shellUrl)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Note that you'll need to change the package to just shell)


resp, err := shell.PublishWithDetails(examplesHash, "key1", time.Hour, time.Hour, false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This expects key1 to be generated when calling this

if err != nil {
t.Fatal(err)
}

if resp.Value != examplesHash {
t.Fatalf(fmt.Sprintf("Expected to receive %s but got %s", examplesHash, resp.Value))
}
}

func TestPublishDetailsWithoutKey(t *testing.T) {
shell := ipfsapi.NewShell("localhost:5001")

resp, err := shell.PublishWithDetails(examplesHash, "", time.Hour, time.Hour, false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will publish over whatever is published on the machine it's invoked on, which is.. suboptimal

if err != nil {
t.Fatal(err)
}

if resp.Value != examplesHash {
t.Fatalf(fmt.Sprintf("Expected to receive %s but got %s", examplesHash, resp.Value))
}
}
1 change: 0 additions & 1 deletion request.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ func (e *Error) Error() string {

func (r *Request) Send(c *http.Client) (*Response, error) {
url := r.getURL()

req, err := http.NewRequest("POST", url, r.Body)
if err != nil {
return nil, err
Expand Down