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-unixfs
Commits
c8ae4b6f
Commit
c8ae4b6f
authored
Nov 04, 2014
by
Matt Bell
Committed by
Juan Batiz-Benet
Nov 04, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
commands/http: Decomposed Client#Send function
parent
e57cd9b8
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
15 deletions
+33
-15
commands/http/client.go
commands/http/client.go
+33
-15
No files found.
commands/http/client.go
View file @
c8ae4b6f
...
...
@@ -41,6 +41,33 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
req
.
SetOption
(
cmds
.
EncLong
,
cmds
.
JSON
)
}
query
,
in
,
err
:=
getQuery
(
req
)
if
err
!=
nil
{
return
nil
,
err
}
path
:=
strings
.
Join
(
req
.
Path
(),
"/"
)
url
:=
fmt
.
Sprintf
(
ApiUrlFormat
,
c
.
serverAddress
,
ApiPath
,
path
,
query
)
httpRes
,
err
:=
http
.
Post
(
url
,
"application/octet-stream"
,
in
)
if
err
!=
nil
{
return
nil
,
err
}
res
,
err
:=
getResponse
(
httpRes
,
req
)
if
err
!=
nil
{
return
nil
,
err
}
if
len
(
userEncoding
)
>
0
{
req
.
SetOption
(
cmds
.
EncShort
,
userEncoding
)
req
.
SetOption
(
cmds
.
EncLong
,
userEncoding
)
}
return
res
,
nil
}
func
getQuery
(
req
cmds
.
Request
)
(
string
,
io
.
Reader
,
error
)
{
// TODO: handle multiple files with multipart
var
in
io
.
Reader
...
...
@@ -64,20 +91,18 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
}
else
{
// TODO: multipart
if
in
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"Currently, only one file stream is possible per request"
)
return
""
,
nil
,
fmt
.
Errorf
(
"Currently, only one file stream is possible per request"
)
}
in
=
arg
.
(
io
.
Reader
)
}
}
path
:=
strings
.
Join
(
req
.
Path
(),
"/"
)
url
:=
fmt
.
Sprintf
(
ApiUrlFormat
,
c
.
serverAddress
,
ApiPath
,
path
,
query
.
Encode
())
httpRes
,
err
:=
http
.
Post
(
url
,
"application/octet-stream"
,
in
)
if
err
!=
nil
{
return
nil
,
err
}
return
query
.
Encode
(),
in
,
nil
}
// getResponse decodes a http.Response to create a cmds.Response
func
getResponse
(
httpRes
*
http
.
Response
,
req
cmds
.
Request
)
(
cmds
.
Response
,
error
)
{
var
err
error
res
:=
cmds
.
NewResponse
(
req
)
contentType
:=
httpRes
.
Header
[
"Content-Type"
][
0
]
...
...
@@ -109,7 +134,6 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
// handle marshalled errors
err
=
dec
.
Decode
(
&
e
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
nil
,
err
}
}
...
...
@@ -120,17 +144,11 @@ func (c *client) Send(req cmds.Request) (cmds.Response, error) {
v
:=
req
.
Command
()
.
Type
err
=
dec
.
Decode
(
&
v
)
if
err
!=
nil
{
fmt
.
Println
(
err
)
return
nil
,
err
}
res
.
SetOutput
(
v
)
}
if
len
(
userEncoding
)
>
0
{
req
.
SetOption
(
cmds
.
EncShort
,
userEncoding
)
req
.
SetOption
(
cmds
.
EncLong
,
userEncoding
)
}
return
res
,
nil
}
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