Skip to content

Commit f6f6a65

Browse files
authored
Merge pull request #171 from ipfs/feat/fallback
fallback executor support
2 parents 1adb804 + ca0ae17 commit f6f6a65

File tree

4 files changed

+30
-28
lines changed

4 files changed

+30
-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: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,12 @@ 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
3328
ua string
3429
apiPrefix string
30+
fallback cmds.Executor
3531
}
3632

3733
type ClientOpt func(*client)
@@ -48,7 +44,16 @@ func ClientWithAPIPrefix(apiPrefix string) ClientOpt {
4844
}
4945
}
5046

51-
func NewClient(address string, opts ...ClientOpt) Client {
47+
// ClientWithFallback adds a fallback executor to the client.
48+
//
49+
// Note: This may run the PreRun function twice.
50+
func ClientWithFallback(exe cmds.Executor) ClientOpt {
51+
return func(c *client) {
52+
c.fallback = exe
53+
}
54+
}
55+
56+
func NewClient(address string, opts ...ClientOpt) cmds.Executor {
5257
if !strings.HasPrefix(address, "http://") {
5358
address = "http://" + address
5459
}
@@ -69,16 +74,25 @@ func NewClient(address string, opts ...ClientOpt) Client {
6974
func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment) error {
7075
cmd := req.Command
7176

77+
err := cmd.CheckArguments(req)
78+
if err != nil {
79+
return err
80+
}
81+
7282
if cmd.PreRun != nil {
7383
err := cmd.PreRun(req, env)
7484
if err != nil {
7585
return err
7686
}
7787
}
7888

79-
res, err := c.Send(req)
89+
res, err := c.send(req)
8090
if err != nil {
8191
if isConnRefused(err) {
92+
if c.fallback != nil {
93+
// XXX: this runs the PreRun twice
94+
return c.fallback.Execute(req, re, env)
95+
}
8296
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`")
8397
}
8498
return err
@@ -146,7 +160,7 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
146160
return httpReq, nil
147161
}
148162

149-
func (c *client) Send(req *cmds.Request) (cmds.Response, error) {
163+
func (c *client) send(req *cmds.Request) (cmds.Response, error) {
150164
if req.Context == nil {
151165
log.Warningf("no context set in request")
152166
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)