Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
97 changes: 80 additions & 17 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import collections

API_ROOT = 'https://api.parse.com/1/classes'
API_ROOT_FILES = 'https://api.parse.com/1/files'
API_ROOT_PUSH = 'https://api.parse.com/1/push'

APPLICATION_ID = ''
MASTER_KEY = ''
Expand All @@ -28,17 +30,31 @@ class ParseBinaryDataWrapper(str):


class ParseBase(object):
def _executeCall(self, uri, http_verb, data=None):
url = API_ROOT + uri

def _executeCall(self, uri, http_verb, incomingClass, data=None):

if type(incomingClass) == ParseFile:
url = API_ROOT_FILES + uri
elif type(incomingClass) == ParseObject:
url = API_ROOT + uri
elif type(incomingClass) == ParsePush:
url = API_ROOT_PUSH
else:
url = API_ROOT + uri

request = urllib2.Request(url, data)

request.add_header('Content-type', 'application/json')

# we could use urllib2's authentication system, but it seems like overkill for this
auth_header = "Basic %s" % base64.b64encode('%s:%s' % (APPLICATION_ID, MASTER_KEY))
request.add_header("Authorization", auth_header)
if type(incomingClass) == ParseFile:
request.add_header('Content-type', incomingClass.content_type())
elif type(incomingClass) == ParseObject:
request.add_header('Content-type', 'application/json')

# we could use urllib2's authentication system, but it seems like overkill for this
#auth_header = "Basic %s" % base64.b64encode('%s:%s' % (APPLICATION_ID, MASTER_KEY))
request.add_header("X-Parse-Application-Id", APPLICATION_ID)
request.add_header("X-Parse-REST-API-Key", MASTER_KEY)

request.get_method = lambda: http_verb

# TODO: add error handling for server response
Expand All @@ -54,14 +70,44 @@ def _ISO8601ToDatetime(self, date_string):
date = datetime.datetime.strptime(date_string, "%Y-%m-%dT%H:%M:%S.%f%Z")
return date

class ParseFile(ParseBase):
def __init__(self,class_name,input_file,content_type, attrs_file=None):
self._class_name = class_name
self._file = input_file
self._content_type=content_type
#these 3 below are returned by the REST API
self.url = None
self.name = None
self.__type = None

if attrs_file:
self._populateFromDict(attrs_file)

def content_type(self):
return self._content_type

def save(self):
# URL: /1/classes/<className>
# HTTP Verb: POST

uri = '/%s' % self._class_name

data = open(self._file,'rb').read()

response_dict = self._executeCall(uri, 'POST', self, data)
self.url=response_dict['url']
self.name=response_dict['name']

def _populateFromDict(self, attrs_dict):
self.__dict__.update(attrs_dict)

class ParseObject(ParseBase):
def __init__(self, class_name, attrs_dict=None):
self._class_name = class_name
self._object_id = None
self._updated_at = None
self._created_at = None

if attrs_dict:
self._populateFromDict(attrs_dict)

Expand All @@ -86,7 +132,7 @@ def delete(self):

uri = '/%s/%s' % (self._class_name, self._object_id)

self._executeCall(uri, 'DELETE')
self._executeCall(uri, 'DELETE', self)

self = self.__init__(None)

Expand Down Expand Up @@ -129,18 +175,19 @@ def _convertFromParseType(self, prop):
value = self._ISO8601ToDatetime(value['iso'])
elif value['__type'] == 'Bytes':
value = ParseBinaryDataWrapper(base64.b64decode(value['base64']))
elif value['__type'] == 'File':
value = ParseFile('','','',value)
else:
raise Exception('Invalid __type.')

return (key, value)

def _getJSONProperties(self):

properties_list = self.__dict__.items()

# filter properties that start with an underscore
properties_list = filter(lambda prop: prop[0][0] != '_', properties_list)

#properties_list = [(key, value) for key, value in self.__dict__.items() if key[0] != '_']

properties_list = map(self._convertToParseType, properties_list)
Expand All @@ -158,7 +205,7 @@ def _create(self):

data = self._getJSONProperties()

response_dict = self._executeCall(uri, 'POST', data)
response_dict = self._executeCall(uri, 'POST',self, data)

self._created_at = self._updated_at = response_dict['createdAt']
self._object_id = response_dict['objectId']
Expand All @@ -171,18 +218,35 @@ def _update(self):

data = self._getJSONProperties()

response_dict = self._executeCall(uri, 'PUT', data)
response_dict = self._executeCall(uri, 'PUT',self, data)

self._updated_at = response_dict['updatedAt']

class ParsePush(ParseObject):
def __init__(self,channel,data):
self.channel = channel
self.data = data
self.result = ''
#TODO: type, expiration_interval

def save(self):
# URL: /1/classes/<className>
# HTTP Verb: POST
data = self._getJSONProperties()

response_dict = self._executeCall('', 'POST', self, data)
self.result = response_dict['result']

class ParseQuery(ParseBase):
def __init__(self, class_name):
self._class_name = class_name
self._where = collections.defaultdict(dict)
self._options = {}
self._object_id = ''


#by default set the limit, somehow parse only returns 100 records
self._options['limit'] = 999999999999999

def eq(self, name, value):
self._where[name] = value
return self
Expand Down Expand Up @@ -246,10 +310,9 @@ def _fetch(self, single_result=False):

uri = '/%s?%s' % (self._class_name, urllib.urlencode(options))

response_dict = self._executeCall(uri, 'GET')
response_dict = self._executeCall(uri, 'GET',self)

if single_result:
return ParseObject(self._class_name, response_dict)
else:
return [ParseObject(self._class_name, result) for result in response_dict['results']]

return [ParseObject(self._class_name, result) for result in response_dict['results']]
Binary file added __init__.pyc
Binary file not shown.