aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-06 11:04:50 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-06 11:04:50 +0100
commit4c01872714a54959d32cce1a651756019d185f32 (patch)
tree31334c1cb53cd98e8c92fe9b9733ab4d84be03b1
parent56eb87944ec5e64daff1d7d0f80e9a60eb12bf96 (diff)
Parse Retry-After
-rw-r--r--account.go6
-rw-r--r--client.go27
2 files changed, 28 insertions, 5 deletions
diff --git a/account.go b/account.go
index 20dc7c7..3d01e1d 100644
--- a/account.go
+++ b/account.go
@@ -34,6 +34,10 @@ func NewAccount(email string, bits int) (*Account, error) {
}, nil
}
+func LoadAccount(email string) (*Account, error) {
+ return nil, nil
+}
+
func (a *Account) Sign(msg []byte) ([]byte, error) {
if a.signer == nil {
var err error
@@ -67,7 +71,7 @@ func (a Account) Nonce() (string, error) {
}
func (a Account) parseNonce(r *http.Response) {
- if nonce := r.Header.Get("Replay-Nonce"); nonce != "" {
+ if nonce := replyNonce(r); nonce != "" {
a.nonce <- nonce
}
}
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")
+}