aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-20 18:01:18 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-20 18:01:18 +0100
commitdbdb1da1416caf6345cc943900315cee17643ee7 (patch)
tree81b5fbeda39b03945f90ba881421b0f0f2ea9302 /client.go
parent0a7fe63f7f8f844f7a9b150202b309894cc9477f (diff)
kiss
Diffstat (limited to 'client.go')
-rw-r--r--client.go39
1 files changed, 16 insertions, 23 deletions
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")
}
/*