From 46e4f83a631af25f0a616c9ffecf3bf715e606e3 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Thu, 24 Dec 2015 00:49:22 +0100 Subject: Bikeshading --- client.go | 39 +++++++++++++++++++-------------------- 1 file changed, 19 insertions(+), 20 deletions(-) (limited to 'client.go') diff --git a/client.go b/client.go index 401fcfb..f10537f 100644 --- a/client.go +++ b/client.go @@ -14,18 +14,22 @@ import ( "github.com/mgutz/ansi" ) -type Links map[string]string - // Client ... type Client struct { Directory nonce chan string } -type nextStep struct { - Link Links - Location *url.URL - RetryAfter time.Duration +var errNoNonces = errors.New("out of nonces") + +// Nonce implements jose nonce provider +func (c Client) Nonce() (string, error) { + select { + case nonce := <-c.nonce: + return nonce, nil + default: + return "", errNoNonces + } } func (c Client) replyNonce(r *http.Response) { @@ -47,18 +51,6 @@ func NewClient(directory string) (*Client, error) { return c, json.NewDecoder(resp.Body).Decode(&c.Directory) } -var errNoNonces = errors.New("out of nonces") - -// Nonce implements jose nonce provider -func (c Client) Nonce() (string, error) { - select { - case nonce := <-c.nonce: - return nonce, nil - default: - return "", errNoNonces - } -} - // Important header fields // // Replay-Nonce each response, required for next request @@ -85,8 +77,7 @@ func (c *Client) post(uri string, s Signer, v interface{}) (*http.Response, erro } log.Println(ansi.Color("POST", "red+b"), uri, string(body)) - s.Init(c) - signed, err := s.Sign(body) + signed, err := s.Sign(body, c) if err != nil { return nil, err } @@ -121,6 +112,14 @@ func (c *Client) post(uri string, s Signer, v interface{}) (*http.Response, erro //return json.NewDecoder(resp.Body).Decode(v) } +type Links map[string]string + +type nextStep struct { + Link Links + Location *url.URL + RetryAfter time.Duration +} + var linksRe = regexp.MustCompile(`^<(.*)>;rel="(.*)"`) func parseHeader(r *http.Response) nextStep { -- cgit v1.2.3