From 5dd267af24d709292ed7c48c213b7bfd0d7b9269 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Sun, 13 Dec 2015 03:18:59 +0100 Subject: Combine messages --- client.go | 46 ++++++++++++++++++++++++++++------------------ 1 file changed, 28 insertions(+), 18 deletions(-) (limited to 'client.go') diff --git a/client.go b/client.go index e2d8492..a97279b 100644 --- a/client.go +++ b/client.go @@ -4,6 +4,8 @@ import ( "bytes" "encoding/json" "errors" + "fmt" + "io/ioutil" "log" "net/http" "net/textproto" @@ -68,8 +70,8 @@ func (c Client) Nonce() (string, error) { // request is used for // new-reg, new-authz, challenge, new-cert -func (c *Client) request(url string, s Signer, rq, re interface{}) error { - body, err := json.Marshal(rq) +func (c *Client) post(url string, s Signer, v interface{}) error { + body, err := json.Marshal(v) if err != nil { return err } @@ -95,7 +97,14 @@ func (c *Client) request(url string, s Signer, rq, re interface{}) error { c.Link = links(resp) c.Location = location(resp) - return json.NewDecoder(resp.Body).Decode(re) + body, err = ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + // DEBUG + log.Println("RESPONSE", string(body)) + return json.Unmarshal(body, v) + //return json.NewDecoder(resp.Body).Decode(v) } func location(r *http.Response) string { @@ -146,37 +155,38 @@ func replyNonce(r *http.Response) string { challenge cert-chain */ -func (c *Client) Register(a *Account) (RegistrationResp, error) { - rq := Registration{ +func (c *Client) Register(a *Account) (*Registration, error) { + r := &Registration{ Resource: ResNewReg, Contact: a.Contact, } - re := RegistrationResp{} - err := c.request(c.Dir.NewReg, a, rq, &re) - return re, err + err := c.post(c.Dir.NewReg, a, r) + return r, err } // Agree to TOS -func (c *Client) Agree(a *Account) (RegistrationResp, error) { - rq := Registration{ +func (c *Client) Agree(a *Account) (*Registration, error) { + r := &Registration{ Resource: ResRegister, Contact: a.Contact, Agreement: c.Link["terms-of-service"], } - re := RegistrationResp{} - err := c.request(c.Location, a, rq, &re) - return re, err + err := c.post(c.Location, a, r) + return r, err } -func (c *Client) Authorize(a *Account, domain string) (AuthorizationResp, error) { - rq := Authorization{ +func (c *Client) Authorize(a *Account, domain string) (*Authorization, error) { + r := &Authorization{ Resource: ResNewAuthz, Identifier: Identifier{ Type: IdentDNS, Value: domain, }, } - re := AuthorizationResp{} - err := c.request(c.Dir.NewAuthz, a, rq, &re) - return re, err + err := c.post(c.Dir.NewAuthz, a, r) + return r, err +} + +func (c Client) String() string { + return fmt.Sprintf("Link: %v, Location: %v", c.Link, c.Location) } -- cgit v1.2.3