diff --git a/ipns.go b/ipns.go index 90715b6cf..609ceec50 100644 --- a/ipns.go +++ b/ipns.go @@ -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} @@ -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) { diff --git a/ipns_test.go b/ipns_test.go new file mode 100644 index 000000000..7137f1d8d --- /dev/null +++ b/ipns_test.go @@ -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)) + } +} diff --git a/request.go b/request.go index 8b6d4e0df..5ca4675f9 100644 --- a/request.go +++ b/request.go @@ -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