Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-dms3-cmds
Commits
f6f6a656
Unverified
Commit
f6f6a656
authored
Jun 28, 2019
by
Steven Allen
Committed by
GitHub
Jun 28, 2019
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #171 from ipfs/feat/fallback
fallback executor support
parents
1adb804f
ca0ae174
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
30 additions
and
28 deletions
+30
-28
examples/adder/remote/client/main.go
examples/adder/remote/client/main.go
+5
-17
http/client.go
http/client.go
+22
-8
http/client_test.go
http/client_test.go
+2
-2
http/http_test.go
http/http_test.go
+1
-1
No files found.
examples/adder/remote/client/main.go
View file @
f6f6a656
...
...
@@ -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
())
}
http/client.go
View file @
f6f6a656
...
...
@@ -22,16 +22,12 @@ 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
ua
string
apiPrefix
string
fallback
cmds
.
Executor
}
type
ClientOpt
func
(
*
client
)
...
...
@@ -48,7 +44,16 @@ func ClientWithAPIPrefix(apiPrefix string) ClientOpt {
}
}
func
NewClient
(
address
string
,
opts
...
ClientOpt
)
Client
{
// ClientWithFallback adds a fallback executor to the client.
//
// Note: This may run the PreRun function twice.
func
ClientWithFallback
(
exe
cmds
.
Executor
)
ClientOpt
{
return
func
(
c
*
client
)
{
c
.
fallback
=
exe
}
}
func
NewClient
(
address
string
,
opts
...
ClientOpt
)
cmds
.
Executor
{
if
!
strings
.
HasPrefix
(
address
,
"http://"
)
{
address
=
"http://"
+
address
}
...
...
@@ -69,6 +74,11 @@ func NewClient(address string, opts ...ClientOpt) Client {
func
(
c
*
client
)
Execute
(
req
*
cmds
.
Request
,
re
cmds
.
ResponseEmitter
,
env
cmds
.
Environment
)
error
{
cmd
:=
req
.
Command
err
:=
cmd
.
CheckArguments
(
req
)
if
err
!=
nil
{
return
err
}
if
cmd
.
PreRun
!=
nil
{
err
:=
cmd
.
PreRun
(
req
,
env
)
if
err
!=
nil
{
...
...
@@ -76,9 +86,13 @@ func (c *client) Execute(req *cmds.Request, re cmds.ResponseEmitter, env cmds.En
}
}
res
,
err
:=
c
.
S
end
(
req
)
res
,
err
:=
c
.
s
end
(
req
)
if
err
!=
nil
{
if
isConnRefused
(
err
)
{
if
c
.
fallback
!=
nil
{
// XXX: this runs the PreRun twice
return
c
.
fallback
.
Execute
(
req
,
re
,
env
)
}
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`"
)
}
return
err
...
...
@@ -146,7 +160,7 @@ func (c *client) toHTTPRequest(req *cmds.Request) (*http.Request, error) {
return
httpReq
,
nil
}
func
(
c
*
client
)
S
end
(
req
*
cmds
.
Request
)
(
cmds
.
Response
,
error
)
{
func
(
c
*
client
)
s
end
(
req
*
cmds
.
Request
)
(
cmds
.
Response
,
error
)
{
if
req
.
Context
==
nil
{
log
.
Warningf
(
"no context set in request"
)
req
.
Context
=
context
.
Background
()
...
...
http/client_test.go
View file @
f6f6a656
...
...
@@ -44,7 +44,7 @@ func TestClientUserAgent(t *testing.T) {
c
:=
NewClient
(
tc
.
host
,
ClientWithUserAgent
(
tc
.
ua
))
.
(
*
client
)
c
.
httpClient
=
testClient
c
.
S
end
(
r
)
c
.
s
end
(
r
)
if
!
called
{
t
.
Error
(
"handler has not been called"
)
...
...
@@ -84,7 +84,7 @@ func TestClientAPIPrefix(t *testing.T) {
c
:=
NewClient
(
tc
.
host
,
ClientWithAPIPrefix
(
tc
.
prefix
))
.
(
*
client
)
c
.
httpClient
=
testClient
c
.
S
end
(
r
)
c
.
s
end
(
r
)
if
!
called
{
t
.
Error
(
"handler has not been called"
)
...
...
http/http_test.go
View file @
f6f6a656
...
...
@@ -99,7 +99,7 @@ func TestHTTP(t *testing.T) {
req
.
Files
=
tc
.
file
}
res
,
err
:=
c
.
S
end
(
req
)
res
,
err
:=
c
.
(
*
client
)
.
s
end
(
req
)
if
tc
.
sendErr
!=
nil
{
if
err
==
nil
{
t
.
Fatalf
(
"expected error %q but got nil"
,
tc
.
sendErr
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment