From dbdb1da1416caf6345cc943900315cee17643ee7 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 20 Dec 2015 18:01:18 +0100 Subject: kiss --- client.go | 39 ++++++++++++++++----------------------- 1 file changed, 16 insertions(+), 23 deletions(-) (limited to 'client.go') diff --git a/client.go b/client.go index 6006b86..a83ef1c 100644 --- a/client.go +++ b/client.go @@ -8,6 +8,7 @@ import ( "log" "net/http" "regexp" + "strconv" "time" ) @@ -31,7 +32,7 @@ func NewClient(uri string) (*Client, error) { } defer resp.Body.Close() c := &Client{nonce: make(chan string, 10)} - c.nonce <- replyNonce(resp) + c.parseHeader(resp) err = json.NewDecoder(resp.Body).Decode(&c.Dir) if err != nil { return nil, err @@ -88,9 +89,7 @@ func (c *Client) post(s Signer, v interface{}) error { } defer resp.Body.Close() - c.nonce <- replyNonce(resp) - c.Link = links(resp) - c.Location = location(resp) + c.parseHeader(resp) if resp.StatusCode >= http.StatusBadRequest { return handleError(resp) @@ -106,32 +105,26 @@ func (c *Client) post(s Signer, v interface{}) error { //return json.NewDecoder(resp.Body).Decode(v) } -func location(r *http.Response) string { - return r.Header.Get("Location") -} +var linksRe = regexp.MustCompile(`^<(.*)>;rel="(.*)"`) + +func (c *Client) parseHeader(r *http.Response) { + c.nonce <- r.Header.Get("Replay-Nonce") + + c.Location = r.Header.Get("Location") -func links(r *http.Response) Link { - link := make(Link) - reg := regexp.MustCompile(`^<(.*)>;rel="(.*)"`) + c.Link = make(Link) for _, l := range r.Header["Link"] { - re := reg.FindStringSubmatch(l) + re := linksRe.FindStringSubmatch(l) if len(re) == 3 { - link[re[2]] = re[1] + c.Link[re[2]] = re[1] } } - return link -} -func retryAfter(r *http.Response) time.Duration { - ra := r.Header.Get("Retry-After") - if d, err := time.ParseDuration(ra + "s"); err == nil { - return d + c.RetryAfter = time.Second + ra, err := strconv.Atoi(r.Header.Get("Retry-After")) + if err == nil { + c.RetryAfter *= time.Duration(ra) } - return time.Second -} - -func replyNonce(r *http.Response) string { - return r.Header.Get("Replay-Nonce") } /* -- cgit v1.2.3