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 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
39 changes: 39 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,37 @@ 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) {

args := []string{contentHash}
req := s.newRequest(context.Background(), "name/publish", args...)
if key == "" {
key = "self"
}
req.Opts["key"] = key
if lifetime.Seconds() > 0 {
req.Opts["lifetime"] = lifetime.String()
}
if ttl.Seconds() > 0 {
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)
var pubResp PublishResponse
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
38 changes: 38 additions & 0 deletions ipns_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package shell

import (
"fmt"
"testing"
"time"
)

var examplesHashForIPNS = "/ipfs/Qmbu7x6gJbsKDcseQv66pSbUcAA3Au6f7MfTYVXwvBxN2K"
var testKey = "self" // feel free to change to whatever key you have locally

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

resp, err := shell.PublishWithDetails(examplesHashForIPNS, testKey, time.Second, time.Second, false)
if err != nil {
t.Fatal(err)
}

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

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

resp, err := shell.PublishWithDetails(examplesHashForIPNS, "", time.Second, time.Second, false)
if err != nil {
t.Fatal(err)
}

if resp.Value != examplesHashForIPNS {
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