From 4c01872714a54959d32cce1a651756019d185f32 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 6 Dec 2015 11:04:50 +0100 Subject: Parse Retry-After --- client.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'client.go') diff --git a/client.go b/client.go index a78e437..a64f0d5 100644 --- a/client.go +++ b/client.go @@ -7,8 +7,14 @@ import ( "net/http" "net/textproto" "regexp" + "strconv" + "time" ) +type Solver interface { + Solve() +} + type NonceSigner interface { Sign([]byte) ([]byte, error) parseNonce(*http.Response) @@ -30,7 +36,7 @@ func Get(s NonceSigner, uri string, v interface{}) error { // // Replay-Nonce each response, required for next request // Link links to next stage -// Retry-After pooling interval +// Retry-After polling interval // Action Request Response // @@ -66,16 +72,16 @@ func Post(s NonceSigner, uri string, v interface{}) (*http.Response, error) { return nil, handleError(resp) } - log.Printf("%+v\n", parseLinks(resp)) + log.Printf("%+v\n", link(resp)) return resp, nil } type Links map[string]string -func parseLinks(r *http.Response) Links { +func link(r *http.Response) Links { links := make(Links) - key := textproto.CanonicalMIMEHeaderKey("link") + key := textproto.CanonicalMIMEHeaderKey("Link") reg := regexp.MustCompile(`^<(.*)>;rel="(.*)"`) for _, l := range r.Header[key] { re := reg.FindStringSubmatch(l) @@ -85,3 +91,16 @@ func parseLinks(r *http.Response) Links { } return links } + +func retryAfter(r *http.Response) time.Duration { + if ra := r.Header.Get("Retry-After"); ra != "" { + if i, err := strconv.Atoi(ra); err == nil { + return time.Duration(i) * time.Second + } + } + return time.Second +} + +func replyNonce(r *http.Response) string { + return r.Header.Get("Replay-Nonce") +} -- cgit v1.2.3