Skip to content
This repository has been archived by the owner on Feb 20, 2022. It is now read-only.

Commit

Permalink
add file support
Browse files Browse the repository at this point in the history
  • Loading branch information
dankrause committed Nov 8, 2014
1 parent aaca867 commit d97d078
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 11 deletions.
18 changes: 12 additions & 6 deletions parse_rest/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class ParseBase(object):
ENDPOINT_ROOT = API_ROOT

@classmethod
def execute(cls, uri, http_verb, extra_headers=None, batch=False, **kw):
def execute(cls, uri, http_verb, extra_headers=None, batch=False, body=None, **kw):
"""
if batch == False, execute a command with the given parameters and
return the response JSON.
Expand All @@ -70,19 +70,25 @@ def execute(cls, uri, http_verb, extra_headers=None, batch=False, **kw):
rest_key = ACCESS_KEYS.get('rest_key')
master_key = ACCESS_KEYS.get('master_key')

headers = extra_headers or {}
url = uri if uri.startswith(API_ROOT) else cls.ENDPOINT_ROOT + uri
data = kw and json.dumps(kw) or "{}"
if body is None:
data = kw and json.dumps(kw) or "{}"
else:
data = body
if http_verb == 'GET' and data:
url += '?%s' % urlencode(kw)
data = None
else:
data = data.encode('utf-8')

headers = {
'Content-type': 'application/json',
'X-Parse-Application-Id': app_id,
'X-Parse-REST-API-Key': rest_key
}
headers.update(extra_headers or {})

request = Request(url, data, headers)
request.add_header('Content-type', 'application/json')
request.add_header('X-Parse-Application-Id', app_id)
request.add_header('X-Parse-REST-API-Key', rest_key)

if master_key and 'X-Parse-Session-Token' not in headers.keys():
request.add_header('X-Parse-Master-Key', master_key)
Expand Down
41 changes: 36 additions & 5 deletions parse_rest/datatypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import base64
import datetime
import mimetypes
import six

from parse_rest.connection import API_ROOT, ParseBase
Expand Down Expand Up @@ -184,25 +185,55 @@ def _to_native(self):


@complex_type()
class File(ParseType):
class File(ParseType, ParseBase):
ENDPOINT_ROOT = '/'.join([API_ROOT, 'files'])

@classmethod
def from_native(cls, **kw):
return cls(**kw)

def __init__(self, **kw):
name = kw.get('name')
def __init__(self, name, content=None, mimetype=None, url=None):
if isinstance(name, dict):
name = name["name"]
self._name = name
self._file_url = None
self._api_url = '/'.join([API_ROOT, 'files', name])
self._file_url = kw.get('url')
self._content = content
self._mimetype = mimetype or mimetypes.guess_type(name)
if not content and not url:
with open(name) as f:
content = f.read()
self._content = content

def __repr__(self):
return '<File:%s>' % (getattr(self, '_name', None))

def _to_native(self):
return {
'__type': 'File',
'name': self._name,
'url': self._file_url
}
}

def save(self, batch=False):
uri = '/'.join([self.__class__.ENDPOINT_ROOT, self.name])
headers = {'Content-type': self.mimetype}
response = self.__class__.POST(uri, extra_headers=headers, batch=batch, body=self._content)
self._file_url = response['url']
self._name = response['name']
self._api_url = '/'.join([API_ROOT, 'files', self._name])

if batch:
return response, lambda response_dict: None

def delete(self, batch=False):
uri = "/".join(self.__class__.ENDPOINT_ROOT, self.name)
response = self.__class__.DELETE(uri, batch=batch)

if batch:
return response, lambda response_dict: None

mimetype = property(lambda self: self._mimetype)
url = property(lambda self: self._file_url)
name = property(lambda self: self._name)
_absolute_url = property(lambda self: self._api_url)
Expand Down

0 comments on commit d97d078

Please sign in to comment.