12 type object interface {
14 attrs() map[string]interface{}
18 func (c *Client) lookupObject(objpath string) (object, error) {
19 resp, err := c.get(objpath)
23 defer resp.Body.Close()
24 if resp.StatusCode == http.StatusNotFound {
25 return nil, ErrNotExist
27 iresp, err := parseResponse(resp.Body)
29 return nil, fmt.Errorf("parse response: %v", err)
30 } else if iresp.Error != nil {
31 return nil, iresp.Error
32 } else if resp.StatusCode != http.StatusOK {
33 return nil, errors.New(resp.Status)
35 return objectFromLookup(iresp)
38 func (c *Client) allObjects(objpath string) ([]object, error) {
39 resp, err := c.get(objpath)
43 defer resp.Body.Close()
44 iresp, err := parseResponse(resp.Body)
47 } else if iresp.Error != nil {
48 return nil, iresp.Error
49 } else if resp.StatusCode != http.StatusOK {
50 return nil, errors.New(resp.Status)
52 return iresp.Results, nil
55 func (c *Client) filterObjects(objpath, expr string) ([]object, error) {
56 resp, err := c.getFilter(objpath, expr)
60 defer resp.Body.Close()
61 if resp.StatusCode == http.StatusNotFound {
62 return nil, ErrNoMatch
64 iresp, err := parseResponse(resp.Body)
67 } else if iresp.Error != nil {
68 return nil, iresp.Error
69 } else if resp.StatusCode != http.StatusOK {
70 return nil, errors.New(resp.Status)
72 return iresp.Results, nil
75 func (c *Client) createObject(obj object) error {
76 buf := &bytes.Buffer{}
77 switch v := obj.(type) {
79 if err := json.NewEncoder(buf).Encode(v); err != nil {
83 if err := json.NewEncoder(buf).Encode(v); err != nil {
87 if err := json.NewEncoder(buf).Encode(v); err != nil {
91 return fmt.Errorf("create type %T unsupported", v)
93 resp, err := c.put(obj.path(), buf)
97 if resp.StatusCode == http.StatusOK {
100 defer resp.Body.Close()
101 iresp, err := parseResponse(resp.Body)
103 return fmt.Errorf("parse response: %v", err)
105 if strings.Contains(iresp.Error.Error(), "already exists") {
111 func (c *Client) deleteObject(objpath string) error {
112 resp, err := c.delete(objpath)
116 defer resp.Body.Close()
117 if resp.StatusCode == http.StatusOK {
119 } else if resp.StatusCode == http.StatusNotFound {
122 iresp, err := parseResponse(resp.Body)
124 return fmt.Errorf("parse response: %v", err)