refactor req & reqDo
This commit is contained in:
parent
05b47fd0dc
commit
b947e819ed
@ -130,13 +130,13 @@ func (c *Client) Remove(path string) error {
|
||||
}
|
||||
|
||||
func (c *Client) RemoveAll(path string) error {
|
||||
rs, err := c.reqDo("DELETE", path, nil)
|
||||
rs, err := c.req("DELETE", path, nil, nil)
|
||||
if err != nil {
|
||||
return newPathError("Remove", path, 400)
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
if rs.StatusCode == 200 {
|
||||
if rs.StatusCode == 200 || rs.StatusCode == 404 {
|
||||
return nil
|
||||
} else {
|
||||
return newPathError("Remove", path, rs.StatusCode)
|
||||
@ -197,7 +197,7 @@ func (c *Client) Read(path string) ([]byte, error) {
|
||||
}
|
||||
|
||||
func (c *Client) ReadStream(path string) (io.ReadCloser, error) {
|
||||
rs, err := c.reqDo("GET", path, nil)
|
||||
rs, err := c.req("GET", path, nil, nil)
|
||||
if err != nil {
|
||||
return nil, newPathErrorErr("ReadStream", path, err)
|
||||
}
|
||||
|
50
requests.go
50
requests.go
@ -8,30 +8,26 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (c *Client) req(method string, path string, body io.Reader) (req *http.Request, err error) {
|
||||
req, err = http.NewRequest(method, Join(c.root, path), body)
|
||||
func (c *Client) req(method string, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
|
||||
r, err := http.NewRequest(method, Join(c.root, path), body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for k, vals := range c.headers {
|
||||
for _, v := range vals {
|
||||
req.Header.Add(k, v)
|
||||
r.Header.Add(k, v)
|
||||
}
|
||||
}
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *Client) reqDo(method string, path string, body io.Reader) (*http.Response, error) {
|
||||
rq, err := c.req(method, path, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if intercept != nil {
|
||||
intercept(r)
|
||||
}
|
||||
|
||||
return c.c.Do(rq)
|
||||
return c.c.Do(r)
|
||||
}
|
||||
|
||||
func (c *Client) mkcol(path string) int {
|
||||
rs, err := c.reqDo("MKCOL", path, nil)
|
||||
rs, err := c.req("MKCOL", path, nil, nil)
|
||||
if err != nil {
|
||||
return 400
|
||||
}
|
||||
@ -45,22 +41,13 @@ func (c *Client) mkcol(path string) int {
|
||||
}
|
||||
|
||||
func (c *Client) options(path string) (*http.Response, error) {
|
||||
rq, err := c.req("OPTIONS", path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return c.req("OPTIONS", path, nil, func(rq *http.Request) {
|
||||
rq.Header.Add("Depth", "0")
|
||||
|
||||
return c.c.Do(rq)
|
||||
})
|
||||
}
|
||||
|
||||
func (c *Client) propfind(path string, self bool, body string, resp interface{}, parse func(resp interface{}) error) error {
|
||||
rq, err := c.req("PROPFIND", path, strings.NewReader(body))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
rs, err := c.req("PROPFIND", path, strings.NewReader(body), func(rq *http.Request) {
|
||||
if self {
|
||||
rq.Header.Add("Depth", "0")
|
||||
} else {
|
||||
@ -71,39 +58,34 @@ func (c *Client) propfind(path string, self bool, body string, resp interface{},
|
||||
rq.Header.Add("Accept-Charset", "utf-8")
|
||||
// TODO add support for 'gzip,deflate;q=0.8,q=0.7'
|
||||
rq.Header.Add("Accept-Encoding", "")
|
||||
|
||||
rs, err := c.c.Do(rq)
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
if rs.StatusCode != 207 {
|
||||
return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, rq.Method, rq.URL.String()))
|
||||
return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, "PROPFIND", path))
|
||||
}
|
||||
|
||||
return parseXML(rs.Body, resp, parse)
|
||||
}
|
||||
|
||||
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
|
||||
rq, err := c.req(method, oldpath, nil)
|
||||
if err != nil {
|
||||
return newPathErrorErr(method, oldpath, err)
|
||||
}
|
||||
|
||||
rs, err := c.req(method, oldpath, nil, func(rq *http.Request) {
|
||||
rq.Header.Add("Destination", Join(c.root, newpath))
|
||||
if overwrite {
|
||||
rq.Header.Add("Overwrite", "T")
|
||||
} else {
|
||||
rq.Header.Add("Overwrite", "F")
|
||||
}
|
||||
|
||||
rs, err := c.c.Do(rq)
|
||||
})
|
||||
if err != nil {
|
||||
return newPathErrorErr(method, oldpath, err)
|
||||
}
|
||||
defer rs.Body.Close()
|
||||
|
||||
// TODO handle result outside ...
|
||||
switch rs.StatusCode {
|
||||
case 201, 204:
|
||||
return nil
|
||||
@ -120,7 +102,7 @@ func (c *Client) copymove(method string, oldpath string, newpath string, overwri
|
||||
}
|
||||
|
||||
func (c *Client) put(path string, stream io.Reader) int {
|
||||
rs, err := c.reqDo("PUT", path, stream)
|
||||
rs, err := c.req("PUT", path, stream, nil)
|
||||
if err != nil {
|
||||
return 400
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user