Browse Source

api: Refactoring into shared write logic

pull/711/head
Armon Dadgar 10 years ago
parent
commit
5f627fca1b
  1. 21
      api/api.go
  2. 6
      api/raw.go
  3. 34
      api/session.go

21
api/api.go

@ -311,6 +311,27 @@ func (c *Client) query(endpoint string, out interface{}, q *QueryOptions) (*Quer
return qm, nil return qm, nil
} }
// write is used to do a PUT request against an endpoint
// and serialize/deserialized using the standard Consul conventions.
func (c *Client) write(endpoint string, in, out interface{}, q *WriteOptions) (*WriteMeta, error) {
r := c.newRequest("PUT", endpoint)
r.setWriteOptions(q)
r.obj = in
rtt, resp, err := requireOK(c.doRequest(r))
if err != nil {
return nil, err
}
defer resp.Body.Close()
wm := &WriteMeta{RequestTime: rtt}
if out != nil {
if err := decodeBody(resp, &out); err != nil {
return nil, err
}
}
return wm, nil
}
// parseQueryMeta is used to help parse query meta-data // parseQueryMeta is used to help parse query meta-data
func parseQueryMeta(resp *http.Response, q *QueryMeta) error { func parseQueryMeta(resp *http.Response, q *QueryMeta) error {
header := resp.Header header := resp.Header

6
api/raw.go

@ -16,3 +16,9 @@ func (c *Client) Raw() *Raw {
func (raw *Raw) Query(endpoint string, out interface{}, q *QueryOptions) (*QueryMeta, error) { func (raw *Raw) Query(endpoint string, out interface{}, q *QueryOptions) (*QueryMeta, error) {
return raw.c.query(endpoint, out, q) return raw.c.query(endpoint, out, q)
} }
// Write is used to do a PUT request against an endpoint
// and serialize/deserialized using the standard Consul conventions.
func (raw *Raw) Write(endpoint string, in, out interface{}, q *WriteOptions) (*WriteMeta, error) {
return raw.c.write(endpoint, in, out, q)
}

34
api/session.go

@ -93,18 +93,9 @@ func (s *Session) Create(se *SessionEntry, q *WriteOptions) (string, *WriteMeta,
} }
func (s *Session) create(obj interface{}, q *WriteOptions) (string, *WriteMeta, error) { func (s *Session) create(obj interface{}, q *WriteOptions) (string, *WriteMeta, error) {
r := s.c.newRequest("PUT", "/v1/session/create")
r.setWriteOptions(q)
r.obj = obj
rtt, resp, err := requireOK(s.c.doRequest(r))
if err != nil {
return "", nil, err
}
defer resp.Body.Close()
wm := &WriteMeta{RequestTime: rtt}
var out struct{ ID string } var out struct{ ID string }
if err := decodeBody(resp, &out); err != nil { wm, err := s.c.write("/v1/session/create", obj, &out, q)
if err != nil {
return "", nil, err return "", nil, err
} }
return out.ID, wm, nil return out.ID, wm, nil
@ -112,35 +103,20 @@ func (s *Session) create(obj interface{}, q *WriteOptions) (string, *WriteMeta,
// Destroy invalides a given session // Destroy invalides a given session
func (s *Session) Destroy(id string, q *WriteOptions) (*WriteMeta, error) { func (s *Session) Destroy(id string, q *WriteOptions) (*WriteMeta, error) {
r := s.c.newRequest("PUT", "/v1/session/destroy/"+id) wm, err := s.c.write("/v1/session/destroy/"+id, nil, nil, q)
r.setWriteOptions(q)
rtt, resp, err := requireOK(s.c.doRequest(r))
if err != nil { if err != nil {
return nil, err return nil, err
} }
resp.Body.Close()
wm := &WriteMeta{RequestTime: rtt}
return wm, nil return wm, nil
} }
// Renew renews the TTL on a given session // Renew renews the TTL on a given session
func (s *Session) Renew(id string, q *WriteOptions) (*SessionEntry, *WriteMeta, error) { func (s *Session) Renew(id string, q *WriteOptions) (*SessionEntry, *WriteMeta, error) {
r := s.c.newRequest("PUT", "/v1/session/renew/"+id) var entries []*SessionEntry
r.setWriteOptions(q) wm, err := s.c.write("/v1/session/renew/"+id, nil, &entries, q)
rtt, resp, err := requireOK(s.c.doRequest(r))
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
defer resp.Body.Close()
wm := &WriteMeta{RequestTime: rtt}
var entries []*SessionEntry
if err := decodeBody(resp, &entries); err != nil {
return nil, wm, err
}
if len(entries) > 0 { if len(entries) > 0 {
return entries[0], wm, nil return entries[0], wm, nil
} }

Loading…
Cancel
Save