/
installation.py
74 lines (56 loc) · 2.71 KB
/
installation.py
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
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from parse_rest.connection import API_ROOT
from parse_rest.datatypes import ParseResource
from parse_rest.query import QueryManager
class Installation(ParseResource):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'installations'])
@classmethod
def _get_installation_url(cls, installation_id):
"""
Get the URL for RESTful operations on this particular installation
"""
return '/'.join([cls.ENDPOINT_ROOT, installation_id])
@classmethod
def update_channels(cls, installation_id, channels_to_add=set(),
channels_to_remove=set(), **kw):
"""
Allow an application to manually subscribe or unsubscribe an
installation to a certain push channel in a unified operation.
this is based on:
https://www.parse.com/docs/rest#installations-updating
installation_id: the installation id you'd like to add a channel to
channels_to_add: the name of the channel you'd like to subscribe the user to
channels_to_remove: the name of the channel you'd like to unsubscribe the user from
"""
installation_url = cls._get_installation_url(installation_id)
current_config = cls.GET(installation_url)
new_channels = list(set(current_config['channels']).union(channels_to_add).difference(channels_to_remove))
cls.PUT(installation_url, channels=new_channels)
class Push(ParseResource):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'push'])
@classmethod
def _send(cls, data, where=None, **kw):
if where != None:
kw['where'] = where
# allow channels to be specified even if "where" is as well
if "channels" in kw:
kw['where']["channels"] = {"$in": kw.pop("channels")}
return cls.POST('', data=data, **kw)
@classmethod
def alert(cls, data, where=None, **kw):
cls._send(data, where=where, **kw)
@classmethod
def message(cls, message, where=None, **kw):
cls._send({'alert': message}, where=where, **kw)
Installation.Query = QueryManager(Installation)