Skip to content

Commit

Permalink
Merge pull request #176 from ipfs/fix/ipfsconn-with-POST
Browse files Browse the repository at this point in the history
Fix/ipfsconn with post
  • Loading branch information
hsanjuan committed Oct 13, 2017
2 parents 180807b + 598aaf2 commit da0cb5e
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
26 changes: 13 additions & 13 deletions ipfsconn/ipfshttp/ipfshttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ func (ipfs *Connector) Shutdown() error {
// contains the error message.
func (ipfs *Connector) ID() (api.IPFSID, error) {
id := api.IPFSID{}
body, err := ipfs.get("id")
body, err := ipfs.post("id")
if err != nil {
id.Error = err.Error()
return id, err
Expand Down Expand Up @@ -605,7 +605,7 @@ func (ipfs *Connector) Pin(hash *cid.Cid) error {
}
if !pinStatus.IsPinned() {
path := fmt.Sprintf("pin/add?arg=%s", hash)
_, err = ipfs.get(path)
_, err = ipfs.post(path)
if err == nil {
logger.Info("IPFS Pin request succeeded: ", hash)
}
Expand All @@ -624,7 +624,7 @@ func (ipfs *Connector) Unpin(hash *cid.Cid) error {
}
if pinStatus.IsPinned() {
path := fmt.Sprintf("pin/rm?arg=%s", hash)
_, err := ipfs.get(path)
_, err := ipfs.post(path)
if err == nil {
logger.Info("IPFS Unpin request succeeded:", hash)
}
Expand All @@ -638,7 +638,7 @@ func (ipfs *Connector) Unpin(hash *cid.Cid) error {
// PinLs performs a "pin ls --type typeFilter" request against the configured
// IPFS daemon and returns a map of cid strings and their status.
func (ipfs *Connector) PinLs(typeFilter string) (map[string]api.IPFSPinStatus, error) {
body, err := ipfs.get("pin/ls?type=" + typeFilter)
body, err := ipfs.post("pin/ls?type=" + typeFilter)

// Some error talking to the daemon
if err != nil {
Expand All @@ -664,7 +664,7 @@ func (ipfs *Connector) PinLs(typeFilter string) (map[string]api.IPFSPinStatus, e
// an api.IPFSPinStatus for that hash.
func (ipfs *Connector) PinLsCid(hash *cid.Cid) (api.IPFSPinStatus, error) {
lsPath := fmt.Sprintf("pin/ls?arg=%s&type=recursive", hash)
body, err := ipfs.get(lsPath)
body, err := ipfs.post(lsPath)

// Network error, daemon down
if body == nil && err != nil {
Expand All @@ -691,15 +691,15 @@ func (ipfs *Connector) PinLsCid(hash *cid.Cid) (api.IPFSPinStatus, error) {
return api.IPFSPinStatusFromString(pinObj.Type), nil
}

// get performs the heavy lifting of a get request against
// post performs the heavy lifting of a post request against
// the IPFS daemon.
func (ipfs *Connector) get(path string) ([]byte, error) {
logger.Debugf("getting %s", path)
func (ipfs *Connector) post(path string) ([]byte, error) {
logger.Debugf("posting %s", path)
url := fmt.Sprintf("%s/%s",
ipfs.apiURL(),
path)

res, err := http.Get(url)
res, err := http.Post(url, "", nil)
if err != nil {
logger.Error("error getting:", err)
return nil, err
Expand Down Expand Up @@ -756,7 +756,7 @@ func (ipfs *Connector) ConnectSwarms() error {
// This is a best effort attempt
// We ignore errors which happens
// when passing in a bunch of addresses
_, err := ipfs.get(
_, err := ipfs.post(
fmt.Sprintf("swarm/connect?arg=%s", addr))
if err != nil {
logger.Debug(err)
Expand All @@ -772,7 +772,7 @@ func (ipfs *Connector) ConnectSwarms() error {
// a given configuration key. For example, "Datastore/StorageMax" will return
// the value for StorageMax in the Datastore configuration object.
func (ipfs *Connector) ConfigKey(keypath string) (interface{}, error) {
res, err := ipfs.get("config/show")
res, err := ipfs.post("config/show")
if err != nil {
logger.Error(err)
return nil, err
Expand Down Expand Up @@ -816,7 +816,7 @@ func getConfigValue(path []string, cfg map[string]interface{}) (interface{}, err
// value is derived from the RepoSize and StorageMax values given by "repo
// stats". The value is in bytes.
func (ipfs *Connector) FreeSpace() (int, error) {
res, err := ipfs.get("repo/stat")
res, err := ipfs.post("repo/stat")
if err != nil {
logger.Error(err)
return 0, err
Expand All @@ -834,7 +834,7 @@ func (ipfs *Connector) FreeSpace() (int, error) {
// RepoSize returns the current repository size of the ipfs daemon as
// provided by "repo stats". The value is in bytes.
func (ipfs *Connector) RepoSize() (int, error) {
res, err := ipfs.get("repo/stat")
res, err := ipfs.post("repo/stat")
if err != nil {
logger.Error(err)
return 0, err
Expand Down
20 changes: 10 additions & 10 deletions ipfsconn/ipfshttp/ipfshttp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func TestIPFSProxyVersion(t *testing.T) {
defer mock.Close()
defer ipfs.Shutdown()

res, err := http.Get(fmt.Sprintf("%s/version", proxyURL(ipfs)))
res, err := http.Post(fmt.Sprintf("%s/version", proxyURL(ipfs)), "", nil)
if err != nil {
t.Fatal("should forward requests to ipfs host: ", err)
}
Expand Down Expand Up @@ -187,7 +187,7 @@ func TestIPFSProxyPin(t *testing.T) {
defer mock.Close()
defer ipfs.Shutdown()

res, err := http.Get(fmt.Sprintf("%s/pin/add?arg=%s", proxyURL(ipfs), test.TestCid1))
res, err := http.Post(fmt.Sprintf("%s/pin/add?arg=%s", proxyURL(ipfs), test.TestCid1), "", nil)
if err != nil {
t.Fatal("should have succeeded: ", err)
}
Expand All @@ -209,7 +209,7 @@ func TestIPFSProxyPin(t *testing.T) {
}

// Try with a bad cid
res2, err := http.Get(fmt.Sprintf("%s/pin/add?arg=%s", proxyURL(ipfs), test.ErrorCid))
res2, err := http.Post(fmt.Sprintf("%s/pin/add?arg=%s", proxyURL(ipfs), test.ErrorCid), "", nil)
if err != nil {
t.Fatal("request should work: ", err)
}
Expand Down Expand Up @@ -237,7 +237,7 @@ func TestIPFSProxyUnpin(t *testing.T) {
defer mock.Close()
defer ipfs.Shutdown()

res, err := http.Get(fmt.Sprintf("%s/pin/rm?arg=%s", proxyURL(ipfs), test.TestCid1))
res, err := http.Post(fmt.Sprintf("%s/pin/rm?arg=%s", proxyURL(ipfs), test.TestCid1), "", nil)
if err != nil {
t.Fatal("should have succeeded: ", err)
}
Expand All @@ -260,7 +260,7 @@ func TestIPFSProxyUnpin(t *testing.T) {
}

// Try with a bad cid
res2, err := http.Get(fmt.Sprintf("%s/pin/rm?arg=%s", proxyURL(ipfs), test.ErrorCid))
res2, err := http.Post(fmt.Sprintf("%s/pin/rm?arg=%s", proxyURL(ipfs), test.ErrorCid), "", nil)
if err != nil {
t.Fatal("request should work: ", err)
}
Expand All @@ -287,7 +287,7 @@ func TestIPFSProxyPinLs(t *testing.T) {
defer mock.Close()
defer ipfs.Shutdown()

res, err := http.Get(fmt.Sprintf("%s/pin/ls?arg=%s", proxyURL(ipfs), test.TestCid1))
res, err := http.Post(fmt.Sprintf("%s/pin/ls?arg=%s", proxyURL(ipfs), test.TestCid1), "", nil)
if err != nil {
t.Fatal("should have succeeded: ", err)
}
Expand All @@ -309,7 +309,7 @@ func TestIPFSProxyPinLs(t *testing.T) {
t.Error("wrong response")
}

res2, err := http.Get(fmt.Sprintf("%s/pin/ls", proxyURL(ipfs)))
res2, err := http.Post(fmt.Sprintf("%s/pin/ls", proxyURL(ipfs)), "", nil)
if err != nil {
t.Fatal("should have succeeded: ", err)
}
Expand All @@ -328,7 +328,7 @@ func TestIPFSProxyPinLs(t *testing.T) {
t.Error("wrong response")
}

res3, err := http.Get(fmt.Sprintf("%s/pin/ls?arg=%s", proxyURL(ipfs), test.ErrorCid))
res3, err := http.Post(fmt.Sprintf("%s/pin/ls?arg=%s", proxyURL(ipfs), test.ErrorCid), "", nil)
if err != nil {
t.Fatal("should have succeeded: ", err)
}
Expand Down Expand Up @@ -406,7 +406,7 @@ func TestProxyAddError(t *testing.T) {
ipfs, mock := testIPFSConnector(t)
defer mock.Close()
defer ipfs.Shutdown()
res, err := http.Get(fmt.Sprintf("%s/add?recursive=true", proxyURL(ipfs)))
res, err := http.Post(fmt.Sprintf("%s/add?recursive=true", proxyURL(ipfs)), "", nil)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -489,7 +489,7 @@ func TestProxyError(t *testing.T) {
defer mock.Close()
defer ipfs.Shutdown()

res, err := http.Get(fmt.Sprintf("%s/bad/command", proxyURL(ipfs)))
res, err := http.Post(fmt.Sprintf("%s/bad/command", proxyURL(ipfs)), "", nil)
if err != nil {
t.Fatal("should have succeeded: ", err)
}
Expand Down

0 comments on commit da0cb5e

Please sign in to comment.