aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-12-10 13:15:55 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-12-10 13:15:55 +0100
commit54f6c23a9670700e8a7d14858173e19221e16550 (patch)
tree61a971cdcb551c2b666cf35519fce2692adeb18d /client.go
parentf576e74ce6891ce05882d536428f9bf9979910a2 (diff)
Move nonce into client
Diffstat (limited to 'client.go')
-rw-r--r--client.go34
1 files changed, 34 insertions, 0 deletions
diff --git a/client.go b/client.go
index 8e92127..5b178a8 100644
--- a/client.go
+++ b/client.go
@@ -3,6 +3,7 @@ package acme
import (
"bytes"
"encoding/json"
+ "errors"
"log"
"net/http"
"net/textproto"
@@ -19,6 +20,39 @@ type NonceSigner interface {
parseNonce(*http.Response)
}
+type Client struct {
+ directory Directory
+ nonce chan string
+}
+
+// NewClient fetches directory and initializes nonce
+func NewClient(uri string) (*Client, error) {
+ resp, err := http.Get(uri)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+ c := &Client{nonce: make(chan string, 10)}
+ c.nonce <- replyNonce(resp)
+ err = json.NewDecoder(resp.Body).Decode(&c.directory)
+ if err != nil {
+ return nil, err
+ }
+ return c, nil
+}
+
+var errNoNonces = errors.New("No nonces available")
+
+// Nonce implements jose nonce provider
+func (c Client) Nonce() (string, error) {
+ select {
+ case nonce := <-c.nonce:
+ return nonce, nil
+ default:
+ return "", errNoNonces
+ }
+}
+
// Get is used for
// directory, authz, cert
func Get(s NonceSigner, uri string, v interface{}) error {