aboutsummaryrefslogtreecommitdiff
path: root/client.go
diff options
context:
space:
mode:
authorDimitri Sokolyuk <demon@dim13.org>2015-11-28 18:52:49 +0100
committerDimitri Sokolyuk <demon@dim13.org>2015-11-28 18:52:49 +0100
commitc6261c5ce9537296479b621e91a90e4f57a21734 (patch)
treea81f7dfc613bec42437890fedeb9af8b01adf5a5 /client.go
parentf280d0d011a8dd1fbc582b54a248c9e8459311b0 (diff)
WIP error handler
Diffstat (limited to 'client.go')
-rw-r--r--client.go40
1 files changed, 21 insertions, 19 deletions
diff --git a/client.go b/client.go
index 6e83b5a..06bb907 100644
--- a/client.go
+++ b/client.go
@@ -3,13 +3,16 @@ package acme
import (
"bytes"
"encoding/json"
- "log"
"net/http"
"github.com/square/go-jose"
)
-var nonces = make(nonce, 100) // buffered channel for nonces
+type Signer interface {
+ Sign([]byte) ([]byte, error)
+}
+
+var nonces = newNonce()
func Get(uri string, v interface{}) error {
resp, err := http.Get(uri)
@@ -21,20 +24,28 @@ func Get(uri string, v interface{}) error {
return json.NewDecoder(resp.Body).Decode(v)
}
-func Post(uri string, v interface{}) error {
+func Post(s Signer, uri string, v interface{}) (*http.Response, error) {
body, err := json.Marshal(v)
if err != nil {
- return err
+ return nil, err
}
- log.Println(string(body))
- return nil
- // premature debug abort
- _, err = http.Post(uri, "application/jose+json", bytes.NewReader(body))
+ signed, err := s.Sign(body)
if err != nil {
- return err
+ return nil, err
+ }
+
+ resp, err := http.Post(uri, "application/jose+json", bytes.NewReader(signed))
+ if err != nil {
+ return nil, err
+ }
+ nonces.parse(resp)
+
+ if resp.StatusCode >= http.StatusBadRequest {
+ return nil, handleError(resp)
}
- return nil
+
+ return resp, nil
}
func Sign(acc Account, body []byte) (string, error) {
@@ -49,12 +60,3 @@ func Sign(acc Account, body []byte) (string, error) {
}
return obj.FullSerialize(), nil
}
-
-func ParseSigned(body string) error {
- obj, err := jose.ParseSigned(body)
- if err != nil {
- return err
- }
- log.Printf("%+v\n", obj)
- return nil
-}