-
Notifications
You must be signed in to change notification settings - Fork 2
/
crud.go
93 lines (87 loc) · 2.54 KB
/
crud.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package parse
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"time"
)
// Create creates a Parse object. On success the new object's ID is returned.
// The provided object is not modified.
func (c *Client) Create(object Object) (objectID string, err error) {
className, err := objectTypeName(object)
if err != nil {
return "", err
}
payload, err := json.Marshal(object)
uri := "/1/classes/" + className
resp, err := c.doWithBody("POST", uri, bytes.NewReader(payload))
if err != nil {
return "", err
}
defer resp.Body.Close()
c.trace("Create", uri)
_, id := objectURIToClassAndID(resp.Header.Get("Location"))
return id, nil
}
// GetClass populates the passed object by looking up based on Class name and objectID.
func (c *Client) GetClass(className string, objectID string, object interface{}) error {
uri := fmt.Sprintf("/1/classes/%s/%s", className, objectID)
resp, err := c.doSimple("GET", uri)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
c.trace("Get", uri, string(body))
// TODO(tmc): warn if not == .Zero() before populating?
return json.Unmarshal(body, object)
}
// Get populates the passed object by looking up based on objectID.
func (c *Client) Get(objectID string, object Object) error {
className, err := objectTypeName(object)
if err != nil {
return err
}
return c.GetClass(className, objectID, object)
}
// Update submits the JSON serialization of object and on success returns the
// updated time. The provided object is not modified.
func (c *Client) Update(object Object) (updateTime time.Time, err error) {
className, err := objectTypeName(object)
if err != nil {
return updateTime, err
}
payload, err := json.Marshal(object)
uri := fmt.Sprintf("/1/classes/%s/%s", className, object.ObjectID())
resp, err := c.doWithBody("PUT", uri, bytes.NewReader(payload))
if err != nil {
return updateTime, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return updateTime, err
}
c.trace("Update", uri, string(body))
updatedAt := &struct {
Time time.Time `json:"updatedAt"`
}{}
err = json.Unmarshal(body, updatedAt)
return updatedAt.Time, err
}
// Delete removes the provided object from the Parse data store.
func (c *Client) Delete(object Object) error {
className, err := objectTypeName(object)
if err != nil {
return err
}
uri := fmt.Sprintf("/1/classes/%s/%s", className, object.ObjectID())
resp, err := c.doSimple("DELETE", uri)
defer resp.Body.Close()
c.trace("Delete", uri)
return err
}