Skip to content

Commit ba094a0

Browse files
committed
http(fix): remove client interface and just return an executor
This is a breaking change and will affect Filecoin. However, filecoin is currently using this incorrectly anyways (breaking PreRun and PostRun).
1 parent 4f26386 commit ba094a0

File tree

4 files changed

+11
-28
lines changed

4 files changed

+11
-28
lines changed

examples/adder/remote/client/main.go

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,20 @@ func main() {
1919
panic(err)
2020
}
2121

22+
req.Options["encoding"] = cmds.Text
23+
2224
// create http rpc client
2325
client := http.NewClient(":6798")
2426

25-
// send request to server
26-
res, err := client.Send(req)
27-
if err != nil {
28-
panic(err)
29-
}
30-
31-
req.Options["encoding"] = cmds.Text
32-
3327
// create an emitter
3428
re, err := cli.NewResponseEmitter(os.Stdout, os.Stderr, req)
3529
if err != nil {
3630
panic(err)
3731
}
3832

39-
// copy received result into cli emitter
40-
if pr, ok := req.Command.PostRun[cmds.CLI]; ok {
41-
err = pr(res, re)
42-
} else {
43-
err = cmds.Copy(re, res)
44-
}
33+
// send request to server
34+
err = client.Execute(req, re, nil)
4535
if err != nil {
46-
re.CloseWithError(err)
36+
panic(err)
4737
}
48-
49-
os.Exit(re.Status())
5038
}

http/client.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ var OptionSkipMap = map[string]bool{
2222
"api": true,
2323
}
2424

25-
// Client is the commands HTTP client interface.
26-
type Client interface {
27-
Send(req *cmds.Request) (cmds.Response, error)
28-
}
29-
3025
type client struct {
3126
serverAddress string
3227
httpClient *http.Client
@@ -48,7 +43,7 @@ func ClientWithAPIPrefix(apiPrefix string) ClientOpt {
4843
}
4944
}
5045

51-
func NewClient(address string, opts ...ClientOpt) Client {
46+
func NewClient(address string, opts ...ClientOpt) cmds.Executor {
5247
if !strings.HasPrefix(address, "http://") {
5348
address = "http://" + address
5449
}
@@ -81,7 +76,7 @@ func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.En
8176
}
8277
}
8378

84-
res, err := c.Send(req)
79+
res, err := c.send(req)
8580
if err != nil {
8681
if isConnRefused(err) {
8782
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`")
@@ -151,7 +146,7 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
151146
return httpReq, nil
152147
}
153148

154-
func (c *client) Send(req *cmds.Request) (cmds.Response, error) {
149+
func (c *client) send(req *cmds.Request) (cmds.Response, error) {
155150
if req.Context == nil {
156151
log.Warningf("no context set in request")
157152
req.Context = context.Background()

http/client_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func TestClientUserAgent(t *testing.T) {
4444

4545
c := NewClient(tc.host, ClientWithUserAgent(tc.ua)).(*client)
4646
c.httpClient = testClient
47-
c.Send(r)
47+
c.send(r)
4848

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

8585
c := NewClient(tc.host, ClientWithAPIPrefix(tc.prefix)).(*client)
8686
c.httpClient = testClient
87-
c.Send(r)
87+
c.send(r)
8888

8989
if !called {
9090
t.Error("handler has not been called")

http/http_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestHTTP(t *testing.T) {
9999
req.Files = tc.file
100100
}
101101

102-
res, err := c.Send(req)
102+
res, err := c.(*client).send(req)
103103
if tc.sendErr != nil {
104104
if err == nil {
105105
t.Fatalf("expected error %q but got nil", tc.sendErr)

0 commit comments

Comments
 (0)