Skip to content

Commit

Permalink
http(fix): remove client interface and just return an executor
Browse files Browse the repository at this point in the history
This is a breaking change and will affect Filecoin. However, filecoin is
currently using this incorrectly anyways (breaking PreRun and PostRun).
  • Loading branch information
Stebalien committed Jun 14, 2019
1 parent 4f26386 commit ba094a0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 28 deletions.
22 changes: 5 additions & 17 deletions examples/adder/remote/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,20 @@ func main() {
panic(err)
}

req.Options["encoding"] = cmds.Text

// create http rpc client
client := http.NewClient(":6798")

// send request to server
res, err := client.Send(req)
if err != nil {
panic(err)
}

req.Options["encoding"] = cmds.Text

// create an emitter
re, err := cli.NewResponseEmitter(os.Stdout, os.Stderr, req)
if err != nil {
panic(err)
}

// copy received result into cli emitter
if pr, ok := req.Command.PostRun[cmds.CLI]; ok {
err = pr(res, re)
} else {
err = cmds.Copy(re, res)
}
// send request to server
err = client.Execute(req, re, nil)
if err != nil {
re.CloseWithError(err)
panic(err)
}

os.Exit(re.Status())
}
11 changes: 3 additions & 8 deletions http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ var OptionSkipMap = map[string]bool{
"api": true,
}

// Client is the commands HTTP client interface.
type Client interface {
Send(req *cmds.Request) (cmds.Response, error)
}

type client struct {
serverAddress string
httpClient *http.Client
Expand All @@ -48,7 +43,7 @@ func ClientWithAPIPrefix(apiPrefix string) ClientOpt {
}
}

func NewClient(address string, opts ...ClientOpt) Client {
func NewClient(address string, opts ...ClientOpt) cmds.Executor {
if !strings.HasPrefix(address, "http://") {
address = "http://" + address
}
Expand Down Expand Up @@ -81,7 +76,7 @@ func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.En
}
}

res, err := c.Send(req)
res, err := c.send(req)
if err != nil {
if isConnRefused(err) {
err = fmt.Errorf("cannot connect to the api. Is the daemon running? To run as a standalone CLI command remove the api file in `$IPFS_PATH/api`")
Expand Down Expand Up @@ -151,7 +146,7 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
return httpReq, nil
}

func (c *client) Send(req *cmds.Request) (cmds.Response, error) {
func (c *client) send(req *cmds.Request) (cmds.Response, error) {
if req.Context == nil {
log.Warningf("no context set in request")
req.Context = context.Background()
Expand Down
4 changes: 2 additions & 2 deletions http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestClientUserAgent(t *testing.T) {

c := NewClient(tc.host, ClientWithUserAgent(tc.ua)).(*client)
c.httpClient = testClient
c.Send(r)
c.send(r)

if !called {
t.Error("handler has not been called")
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestClientAPIPrefix(t *testing.T) {

c := NewClient(tc.host, ClientWithAPIPrefix(tc.prefix)).(*client)
c.httpClient = testClient
c.Send(r)
c.send(r)

if !called {
t.Error("handler has not been called")
Expand Down
2 changes: 1 addition & 1 deletion http/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestHTTP(t *testing.T) {
req.Files = tc.file
}

res, err := c.Send(req)
res, err := c.(*client).send(req)
if tc.sendErr != nil {
if err == nil {
t.Fatalf("expected error %q but got nil", tc.sendErr)
Expand Down

0 comments on commit ba094a0

Please sign in to comment.