From acb9c5d087924b77d83a3fc65813c19b65e90298 Mon Sep 17 00:00:00 2001 From: Dimitri Sokolyuk Date: Mon, 21 Dec 2015 01:23:19 +0100 Subject: Return uri to post --- client.go | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) (limited to 'client.go') diff --git a/client.go b/client.go index 4737995..4cb0aa9 100644 --- a/client.go +++ b/client.go @@ -24,8 +24,8 @@ type Client struct { } // NewClient fetches directory and initializes nonce -func NewClient(uri string) (*Client, error) { - resp, err := http.Get(uri) +func NewClient(directory string) (*Client, error) { + resp, err := http.Get(directory) if err != nil { return nil, err } @@ -39,7 +39,7 @@ func NewClient(uri string) (*Client, error) { return c, nil } -var errNoNonces = errors.New("No nonces available") +var errNoNonces = errors.New("out of nonces") // Nonce implements jose nonce provider func (c Client) Nonce() (string, error) { @@ -70,21 +70,21 @@ func (c Client) Nonce() (string, error) { // request is used for // new-reg, new-authz, challenge, new-cert -func (c *Client) post(s Signer, v interface{}) error { +func (c *Client) post(uri string, s Signer, v interface{}) (*http.Response, error) { body, err := json.Marshal(v) if err != nil { - return err + return nil, err } log.Println("POST", string(body)) signed, err := s.Sign(body, c) if err != nil { - return err + return nil, err } - resp, err := http.Post(c.Location, "application/jose+json", signed) + resp, err := http.Post(uri, "application/jose+json", signed) if err != nil { - return err + return nil, err } defer resp.Body.Close() c.parseHeader(resp) @@ -95,21 +95,21 @@ func (c *Client) post(s Signer, v interface{}) error { var p Problem err = json.NewDecoder(resp.Body).Decode(&p) if err != nil { - return err + return resp, err } if err, ok := urnErrors[p.Type]; ok { p.Err = err } - return p + return resp, p } // DEBUG body, err = ioutil.ReadAll(resp.Body) if err != nil { - return err + return resp, err } log.Println("RESPONSE", string(body)) - return json.Unmarshal(body, v) + return resp, json.Unmarshal(body, v) //return json.NewDecoder(resp.Body).Decode(v) } @@ -164,8 +164,8 @@ func (c *Client) Register(a *Account) (*Registration, error) { Resource: ResNewReg, Contact: a.Contact, } - c.Location = c.Dir.NewReg - return r, c.post(a, r) + _, err := c.post(c.Dir.NewReg, a, r) + return r, err } // Agree to TOS @@ -180,7 +180,8 @@ func (c *Client) Agree(a *Account) (*Registration, error) { Agreement: tos, } log.Println("TOS", tos) - return r, c.post(a, r) + _, err := c.post(c.Location, a, r) + return r, err } func (c *Client) Authorize(a *Account, domain string) (*Authorization, error) { @@ -191,8 +192,8 @@ func (c *Client) Authorize(a *Account, domain string) (*Authorization, error) { Value: domain, }, } - c.Location = c.Dir.NewAuthz - return r, c.post(a, r) + _, err := c.post(c.Dir.NewAuthz, a, r) + return r, err } func (c Client) String() string { -- cgit v1.2.3