refactor req & reqDo

This commit is contained in:
Christoph Polcin 2014-10-27 16:19:17 +01:00
parent 05b47fd0dc
commit b947e819ed
2 changed files with 36 additions and 54 deletions

View File

@ -130,13 +130,13 @@ func (c *Client) Remove(path string) error {
} }
func (c *Client) RemoveAll(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 { if err != nil {
return newPathError("Remove", path, 400) return newPathError("Remove", path, 400)
} }
defer rs.Body.Close() defer rs.Body.Close()
if rs.StatusCode == 200 { if rs.StatusCode == 200 || rs.StatusCode == 404 {
return nil return nil
} else { } else {
return newPathError("Remove", path, rs.StatusCode) 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) { 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 { if err != nil {
return nil, newPathErrorErr("ReadStream", path, err) return nil, newPathErrorErr("ReadStream", path, err)
} }

View File

@ -8,30 +8,26 @@ import (
"strings" "strings"
) )
func (c *Client) req(method string, path string, body io.Reader) (req *http.Request, err error) { func (c *Client) req(method string, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) {
req, err = http.NewRequest(method, Join(c.root, path), body) r, err := http.NewRequest(method, Join(c.root, path), body)
if err != nil { if err != nil {
return nil, err return nil, err
} }
for k, vals := range c.headers { for k, vals := range c.headers {
for _, v := range vals { 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) { if intercept != nil {
rq, err := c.req(method, path, body) intercept(r)
if err != nil {
return nil, err
} }
return c.c.Do(rq) return c.c.Do(r)
} }
func (c *Client) mkcol(path string) int { 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 { if err != nil {
return 400 return 400
} }
@ -45,65 +41,51 @@ func (c *Client) mkcol(path string) int {
} }
func (c *Client) options(path string) (*http.Response, error) { func (c *Client) options(path string) (*http.Response, error) {
rq, err := c.req("OPTIONS", path, nil) return c.req("OPTIONS", path, nil, func(rq *http.Request) {
if err != nil { rq.Header.Add("Depth", "0")
return nil, err })
}
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 { 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)) rs, err := c.req("PROPFIND", path, strings.NewReader(body), func(rq *http.Request) {
if err != nil { if self {
return err rq.Header.Add("Depth", "0")
} } else {
rq.Header.Add("Depth", "1")
if self { }
rq.Header.Add("Depth", "0") rq.Header.Add("Content-Type", "text/xml;charset=UTF-8")
} else { rq.Header.Add("Accept", "application/xml,text/xml")
rq.Header.Add("Depth", "1") rq.Header.Add("Accept-Charset", "utf-8")
} // TODO add support for 'gzip,deflate;q=0.8,q=0.7'
rq.Header.Add("Content-Type", "text/xml;charset=UTF-8") rq.Header.Add("Accept-Encoding", "")
rq.Header.Add("Accept", "application/xml,text/xml") })
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 { if err != nil {
return err return err
} }
defer rs.Body.Close() defer rs.Body.Close()
if rs.StatusCode != 207 { 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) return parseXML(rs.Body, resp, parse)
} }
func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error { func (c *Client) copymove(method string, oldpath string, newpath string, overwrite bool) error {
rq, err := c.req(method, oldpath, nil) rs, err := c.req(method, oldpath, nil, func(rq *http.Request) {
if err != nil { rq.Header.Add("Destination", Join(c.root, newpath))
return newPathErrorErr(method, oldpath, err) if overwrite {
} rq.Header.Add("Overwrite", "T")
} else {
rq.Header.Add("Destination", Join(c.root, newpath)) rq.Header.Add("Overwrite", "F")
if overwrite { }
rq.Header.Add("Overwrite", "T") })
} else {
rq.Header.Add("Overwrite", "F")
}
rs, err := c.c.Do(rq)
if err != nil { if err != nil {
return newPathErrorErr(method, oldpath, err) return newPathErrorErr(method, oldpath, err)
} }
defer rs.Body.Close() defer rs.Body.Close()
// TODO handle result outside ...
switch rs.StatusCode { switch rs.StatusCode {
case 201, 204: case 201, 204:
return nil 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 { 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 { if err != nil {
return 400 return 400
} }