def upload_file(self, **kwargs): """ Upload a file to the Gett service. Takes keyword arguments. Input: * ``filename`` the filename to use in the Gett service (required) * ``data`` the file contents to store in the Gett service (required) - must be a string * ``sharename`` the name of the share in which to store the data (optional); if not given, a new share will be created. * ``title`` the share title to use if a new share is created (optional) Output: * A :py:mod:`pygett.files.GettFile` object Example:: file = client.upload_file(filaname="foo", data=open("foo.txt").read()) """ params = None if 'filename' not in kwargs: raise AttributeError("Parameter 'filename' must be given") else: params = {"filename": kwargs['filename']} if 'data' not in kwargs: raise AttributeError("Parameter 'data' must be given") sharename = None if 'sharename' not in kwargs: share = None if 'title' in kwargs: share = self.create_share(title=kwargs['title']) else: share = self.create_share() sharename = share.sharename else: sharename = kwargs['sharename'] response = GettRequest().post( "/files/%s/create?accesstoken=%s" % (sharename, self.user.access_token()), params) f = None if response.http_status == 200: if 'sharename' not in response.response: response.response['sharename'] = sharename f = GettFile(self.user, **response.response) if f.send_data(data=kwargs['data']): return f
def upload_file(self, **kwargs): """ Upload a file to the Gett service. Takes keyword arguments. Input: * ``filename`` the filename to use in the Gett service (required) * ``data`` the file contents to store in the Gett service (required) - must be a string * ``sharename`` the name of the share in which to store the data (optional); if not given, a new share will be created. * ``title`` the share title to use if a new share is created (optional) Output: * A :py:mod:`pygett.files.GettFile` object Example:: file = client.upload_file(filaname="foo", data=open("foo.txt").read()) """ params = None if 'filename' not in kwargs: raise AttributeError("Parameter 'filename' must be given") else: params = { "filename": kwargs['filename'] } if 'data' not in kwargs: raise AttributeError("Parameter 'data' must be given") sharename = None if 'sharename' not in kwargs: share = None if 'title' in kwargs: share = self.create_share(title=kwargs['title']) else: share = self.create_share() sharename = share.sharename else: sharename = kwargs['sharename'] response = GettRequest().post("/files/%s/create?accesstoken=%s" % (sharename, self.user.access_token()), params) f = None if response.http_status == 200: if 'sharename' not in response.response: response.response['sharename'] = sharename f = GettFile(self.user, **response.response) if f.send_data(data=kwargs['data']): return f
def refresh(self): """ **refresh** Refresh this user object with data from the Gett service Input: * None Output: * ``True`` Example:: if client.user.refresh(): print "User data refreshed!" print "You have %s bytes of storage remaining." % ( client.user.storage_limit - client_user.storage_used ) """ response = GettRequest().get("/users/me?accesstoken=%s" % self.access_token()) if response.http_status == 200: self.userid = response.response['userid'] self.fullname = response.response['fullname'] self.storage_used = response.response['storage']['used'] self.storage_limit = response.response['storage']['limit'] return True
def update(self, **kwargs): """ Add, remove or modify a share's title. Input: * ``title`` The share title, if any (optional) **NOTE**: Passing ``None`` or calling this method with an empty argument list will remove the share's title. Output: * None Example:: share = client.get_share("4ddfds") share.update(title="Example") # Set title to Example share.update() # Remove title """ if 'title' in kwargs: params = {"title": kwargs['title']} else: params = {"title": None} response = GettRequest().post( "/shares/%s/update?accesstoken=%s" % (self.sharename, self.user.access_token()), params) if response.http_status == 200: self.__init__(self.user, **response.response)
def send_data(self, **kwargs): """ This method transmits data to the Gett service. Input: * ``put_url`` A PUT url to use when transmitting the data (required) * ``data`` A byte stream (required) Output: * ``True`` Example:: if file.send_data(put_url=file.upload_url, data=open("example.txt", "rb").read()): print "Your file has been uploaded." """ put_url = None if 'put_url' in kwargs: put_url = kwargs['put_url'] else: put_url = self.put_upload_url if 'data' not in kwargs: raise AttributeError("'data' parameter is required") if not put_url: raise AttributeError("'put_url' cannot be None") if not isinstance(kwargs['data'], str): raise TypeError("'data' parameter must be of type 'str'") response = GettRequest().put(put_url, kwargs['data']) if response.http_status == 200: return True
def upload_url(self): """ This method generates URLs which allow overwriting a file's content with new content. The output is suitable for use in the ``send_data()`` method below. Input: * None Output: * A URL (string) Example:: file = client.get_file("4ddfds", 0) file.send_data(put_url=file.upload_url, data=open("example.txt", "rb").read()) """ if self.put_upload_url: return self.put_upload_url else: response = GettRequest().get( "/files/%s/%s/upload?accesstoken=%s" % (self.sharename, self.fileid, self.user.access_token())) if response.http_status == 200: return response.response['puturl']
def create_share(self, **kwargs): """ Create a new share. Takes a keyword argument. Input: * ``title`` optional share title (optional) Output: * A :py:mod:`pygett.shares.GettShare` object Example:: new_share = client.create_share( title="Example Title" ) """ params = None if 'title' in kwargs: params = {"title": kwargs['title']} response = GettRequest().post( ("/shares/create?accesstoken=%s" % self.user.access_token()), params) if response.http_status == 200: return GettShare(self.user, **response.response)
def _get_shares(self, **kwargs): endpoint = "/shares?accesstoken=%s" % self.user.access_token() if 'limit' in kwargs and isinstance(kwargs['limit'], int) and kwargs['limit'] > 0: endpoint = endpoint + "&limit=%d" % kwargs['limit'] if 'skip' in kwargs and isinstance(kwargs['skip'], int) and kwargs['skip'] > 0: endpoint = endpoint + "&skip=%d" % kwargs['skip'] return GettRequest().get(endpoint)
def thumbnail(self): """ This method returns a thumbnail representation of the file if the data is a supported graphics format. Input: * None Output: * A byte stream representing a thumbnail of a support graphics file Example:: file = client.get_file("4ddfds", 0) open("thumbnail.jpg", "wb").write(file.thumbnail()) """ response = GettRequest().get("/files/%s/%s/blob/thumb" % (self.sharename, self.fileid)) return response.response
def get_share(self, sharename): """ Get a specific share. Does not require authentication. Input: * A sharename Output: * A :py:mod:`pygett.shares.GettShare` object Example:: share = client.get_share("4ddfds") """ response = GettRequest().get("/shares/%s" % sharename) if response.http_status == 200: return GettShare(self.user, **response.response)
def destroy(self): """ This method removes the file's content and metadata from the Gett service. There is no way to recover the data once this method has successfully completed. Input: * None Output: * ``True`` Example:: client.get_file("4ddfds", 0).destroy() """ response = GettRequest().post( ("/files/%s/%s/destroy?accesstoken=%s" % self.user.access_token()), None) if response.http_status == 200: return True
def contents(self): """ This method downloads the contents of the file represented by a `GettFile` object's metadata. Input: * None Output: * A byte stream **NOTE**: You are responsible for handling any encoding/decoding which may be necessary. Example:: file = client.get_file("4ddfds", 0) print file.contents() """ response = GettRequest().get("/files/%s/%s/blob" % (self.sharename, self.fileid)) return response.response
def destroy(self): """ This method removes this share and all of its associated files. There is no way to recover a share or its contents once this method has been called. Input: * None Output: * ``True`` Example:: client.get_share("4ddfds").destroy() """ response = GettRequest().post( "/shares/%s/destroy?accesstoken=%s" % (self.sharename, self.user.access_token()), None) if response.http_status == 200: return True
def refresh(self): """ This method refreshes the object with current metadata from the Gett service. Input: * None Output: * None Example:: share = client.get_share("4ddfds") print share.files[0].filename # prints 'foobar' if share.files[0].destroy(): share.refresh() print share.files[0].filename # now prints 'barbaz' """ response = GettRequest().get("/shares/%s" % self.sharename) if response.http_status == 200: self.__init__(self.user, **response.response)
def login(self, **params): """ **login** Use the current credentials to get a valid Gett access token. Input: * A dict of parameters to use for the login attempt (optional) Output: * ``True`` Example:: if client.user.login(): print "You have %s bytes of storage remaining." % ( client.user.storage_limit - client_user.storage_used ) """ if not params: params = { "apikey": self.apikey, "email": self.email, "password": self.password } response = GettRequest().post("/users/login", params) if response.http_status == 200: self._access_token = response.response['accesstoken'] self.refresh_token = response.response['refreshtoken'] self.access_token_expires = int( time()) + response.response['expires'] self.userid = response.response['user']['userid'] self.fullname = response.response['user']['fullname'] self.storage_used = response.response['user']['storage']['used'] self.storage_limit = response.response['user']['storage']['limit'] return True
def refresh(self): """ Retrieve current file metadata from the Gett service. Input: * None Output: * None Example:: file = client.get_file("4ddfds", 0) print "File size: %s" % file.size # File size: 96 file.send_data(put_url=file.upload_url, data=open("example.txt", "rb").read()) file.refresh() print "File size: %s" % file.size # File size: 109 """ response = GettRequest().get("/files/%s/%s" % (self.sharename, self.fileid)) if response.http_status == 200: self.__init__(self.user, response.response)
def get_file(self, sharename, fileid): """ Get a specific file. Does not require authentication. Input: * A sharename * A fileid - must be an integer Output: * A :py:mod:`pygett.files.GettFile` object Example:: file = client.get_file("4ddfds", 0) """ if not isinstance(fileid, int): raise TypeError("'fileid' must be an integer") response = GettRequest().get("/files/%s/%d" % (sharename, fileid)) if response.http_status == 200: return GettFile(self.user, **response.response)