-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsession.go
248 lines (207 loc) · 7.18 KB
/
session.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
package goparse
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strings"
"github.com/parnurzeal/gorequest"
)
const (
headerAppID = "X-Parse-Application-Id" // Parse Application ID
headerMasterKey = "X-Parse-Master-Key" // Parse Master Key
headerAPIKey = "X-Parse-REST-API-Key" // Parse REST API Key
headerSessionToken = "X-Parse-Session-Token" // Parse Session Token
headerRevocableSession = "X-Parse-Revocable-Session" // Parse Session Token
pathMe = "/users/me"
)
// ParseSession is the client which has SessionToken as user authentication.
type ParseSession struct {
client *ParseClient
SessionToken string
}
const (
// Error code referrense at parse.com
// http://www.parse.com/docs/dotnet/api/html/T_Parse_ParseException_ErrorCode.htm
// errCodeObjectNotFound is object not found
errCodeObjectNotFound = 101
)
var (
// ErrObjectNotFound Error code indicating the specified object doesn't exist.
ErrObjectNotFound = errors.New("object not found")
)
// Create a request which is set headers for Parse API
func (s *ParseSession) initRequest(req *gorequest.SuperAgent, useMaster bool) {
if useMaster {
req.
Set(headerAppID, s.client.ApplicationID).
Set(headerMasterKey, s.client.MasterKey).
Timeout(s.client.TimeOut)
} else {
req.
Set(headerAppID, s.client.ApplicationID).
Set(headerAPIKey, s.client.RESTAPIKey).
Timeout(s.client.TimeOut)
}
if s.client.RevocableSession {
req.Set(headerRevocableSession, "1")
}
if s.SessionToken != "" {
req.Set(headerSessionToken, s.SessionToken)
}
}
func (s *ParseSession) get(path string, useMaster bool) *gorequest.SuperAgent {
req := gorequest.New().Get(s.client.URL + path)
s.initRequest(req, useMaster)
return req
}
func (s *ParseSession) post(path string, useMaster bool) *gorequest.SuperAgent {
req := gorequest.New().Post(s.client.URL + path)
s.initRequest(req, useMaster)
return req
}
func (s *ParseSession) put(path string, useMaster bool) *gorequest.SuperAgent {
req := gorequest.New().Put(s.client.URL + path)
s.initRequest(req, useMaster)
return req
}
func (s *ParseSession) del(path string, useMaster bool) *gorequest.SuperAgent {
req := gorequest.New().Delete(s.client.URL + path)
s.initRequest(req, useMaster)
return req
}
// Signup new user
func (s *ParseSession) Signup(data interface{}) (user User, err error) {
return user, do(s.post("/users", false).Send(data), &user)
}
// Login with data
func (s *ParseSession) Login(username string, password string) (user User, err error) {
// Query values
vals := url.Values{
"username": []string{username},
"password": []string{password},
}
// Create a user
err = do(s.get("/login", false).Query(vals.Encode()), &user)
if user.SessionToken != "" {
s.SessionToken = user.SessionToken
}
return user, err
}
// Logout deletes session from parse
func (s *ParseSession) Logout() (err error) {
return do(s.post("/logout", false), nil)
}
// RequestPasswordReset let parse server to send a password reset mail
func (s *ParseSession) RequestPasswordReset(email string) (err error) {
return do(s.post("/requestPasswordReset", false).Send(User{
Email: email,
}), nil)
}
// GetUser gets user information
func (s *ParseSession) GetUser(userObjectID string) (user User, err error) {
return user, s.getUser(userObjectID, &user, false)
}
// GetUserByMaster gets user information by use master key
func (s *ParseSession) GetUserByMaster(userObjectID string) (user User, err error) {
return user, s.getUser(userObjectID, &user, true)
}
// GetUserInto gets user information into provided object
func (s *ParseSession) GetUserInto(userObjectID string, user interface{}) (err error) {
return s.getUser(userObjectID, user, false)
}
// GetUserIntoByMaster gets user information into provided object by use master key
func (s *ParseSession) GetUserIntoByMaster(userObjectID string, user interface{}) (err error) {
return s.getUser(userObjectID, user, true)
}
// GetUserByMaster gets user information by private
func (s *ParseSession) getUser(userObjectID string, user interface{}, useMaster bool) (err error) {
if userObjectID == "" {
return errors.New("userObjectID must not be empty")
}
if useMaster && s.client.MasterKey == "" {
return errors.New("request is requires PARSE_REST_API_KEY")
}
return do(s.get("/users/"+userObjectID, useMaster), &user)
}
// UpdateUser update user information
func (s *ParseSession) UpdateUser(userObjectID string, data interface{}) (*ObjectResponse, error) {
return s.updateUser(userObjectID, data, false)
}
// UpdateUserByMaster update user information by use master key
func (s *ParseSession) UpdateUserByMaster(userObjectID string, data interface{}) (*ObjectResponse, error) {
return s.updateUser(userObjectID, data, true)
}
// UpdateUser update user information by private
func (s *ParseSession) updateUser(userObjectID string, data interface{}, useMaster bool) (*ObjectResponse, error) {
if userObjectID == "" {
return nil, errors.New("userObjectID must not be empty")
}
if useMaster && s.client.MasterKey == "" {
return nil, errors.New("request is requires PARSE_REST_API_KEY")
}
var resp ObjectResponse
return &resp, do(s.put("/users/"+userObjectID, useMaster).Send(data), &resp)
}
// GetMe gets self user information
func (s *ParseSession) GetMe() (user User, err error) {
err = s.GetMeInto(&user)
return user, err
}
// GetMeInto gets self user information into provided object
func (s *ParseSession) GetMeInto(user interface{}) error {
if user == nil {
return errors.New("user must not be nil")
}
return do(s.get("/users/me", false), user)
}
// DeleteUser deletes user by ID
func (s *ParseSession) DeleteUser(userID string) error {
return do(s.del("/users/"+userID, false), nil)
}
// UploadInstallation stores the subscription data for installations
func (s *ParseSession) UploadInstallation(data Installation, result interface{}) error {
return do(s.post("/installations", false).Send(data), result)
}
// PushNotification sends push-notifiaction each device via parse
func (s *ParseSession) PushNotification(body PushNotificationQuery) error {
return do(s.post("/push", false).Send(body), nil)
}
// PushNotificationByMaster sends push-notifiaction as Master to each device via parse
func (s *ParseSession) PushNotificationByMaster(body PushNotificationQuery) error {
return do(s.post("/push", true).Send(body), nil)
}
// Execute a parse request
func do(req *gorequest.SuperAgent, data interface{}) error {
res, body, errs := req.End()
if errs != nil {
return fmt.Errorf("%v", errs)
}
if res.StatusCode < 200 || res.StatusCode >= 300 {
// parse as error model
reserr := new(Error)
err := json.NewDecoder(strings.NewReader(body)).Decode(reserr)
if err != nil {
return err
}
return reserr
}
if data == nil {
return nil
}
return json.NewDecoder(strings.NewReader(body)).Decode(data)
}
// NewClass creates a new class from the session
func (s *ParseSession) NewClass(className string) *ParseClass {
return &ParseClass{
Session: s,
Name: className,
ClassURL: "/classes/" + className,
UseMaster: false,
}
}
// IsObjectNotFound check the error "not found" or not
func IsObjectNotFound(err error) bool {
v, ok := err.(*Error)
return ok && v.Code == errCodeObjectNotFound
}