mickael9 avatar mickael9 committed 6d7f01b

Implemented more of the API (still no upload though)

Comments (0)

Files changed (1)

 import json
-from urllib.request import urlopen
+import pprint
+import datetime
+from urllib.request import urlopen, Request
 from urllib.parse import urlencode
 from urllib.error import HTTPError
 
     pass
 
 def _post_request(path, **kwargs):
+    qskeys = {}
+    url = API_BASE + path
+
+    for key, value in kwargs.items():
+        if key.startswith('_'):
+            qskeys[key[1:]] = value
+            del kwargs[key]
+
+    if qskeys:
+        url += '?' + urlencode(qskeys)
+
     input_data = json.dumps(kwargs).encode('utf-8')
-    
+    request = Request(url, input_data)
+
+    return _request(request)
+
+def _get_request(path, **kwargs):
+    url = API_BASE + path
+
+    if kwargs:
+        url += '?' + urlencode(kwargs)
+
+    request = Request(url)
+    return _request(request)
+
+def _request(req):
     try:
-        resp = urlopen(API_BASE + path, input_data).read()
+        resp = urlopen(req).read()
     except HTTPError as ex:
         resp = ex.read()
     
     if "error" in result:
         raise APIError(result["error"])
 
+
+    print(req.full_url + " -->")
+    pprint.pprint(result)
+    print()
+
+    return result
+
 class User(object):
-    def _load(result):
-        self.accesstoken = result['accesstoken']
-        self.storage_used = result['storage']['used']
-        self.storage_limit = result['storage']['limit']
+    def _load(self, result):
+        self.atoken = result['accesstoken']
+        self.rtoken = result['refreshtoken']
+        self.storage_used = result['user']['storage']['used']
+        self.storage_limit = result['user']['storage']['limit']
 
     def login_auth(self, email, password):
         result = _post_request('users/login', apikey=API_KEY, email=email, password=password)
+        self._load(result)
 
-    def login_token(self, refreshtoken):
-        result = _post_request('users/login', refreshtoken=refreshtoken)
+    def login_token(self, rtoken):
+        result = _post_request('users/login', refreshtoken=rtoken)
+        self._load(result)
 
+    def list_shares(self, skip=None, limit=None):
+        if skip is not None and limit is not None:
+            results = _get_request('shares', accesstoken=self.atoken, skip=str(skip), limit=str(limit))
+        else:
+            results = _get_request('shares', accesstoken=self.atoken)
+
+        for share_result in results:
+             share = UserShare(self)
+             share._load(share_result)
+             yield share
+
+    def create_share(self, title=None):
+        if title is not None:
+            result = _post_request('shares/create', _accesstoken=self.atoken, title=title)
+        else:
+            result = _post_request('shares/create', _accesstoken=self.atoken)
+
+        share = UserShare(self)
+        share._load(result)
+
+        return share
 
 class Share(object):
-    pass
+    def _load(self, result):
+        self.name = result['sharename']
+        self.title = result.get('title')
+        self.created = datetime.datetime.fromtimestamp(result['created'])
+        self.url = result.get('getturl')
+        self.files = []
+        
+        for file_result in result['files']:
+            f = File(self)
+            f._load(file_result)
+
+            self.files.append(f)
+
+    def refresh(self):
+        result = _get_request('shares/' + self.name)
+        self._load(result)
+
+class UserShare(Share):
+    def __init__(self, user):
+        self.user = user
+
+    def update(self, **fields):
+        result = _post_request('shares/' + self.name + '/update', _accesstoken=self.atoken, **fields)
+        self._load(result)
+
+    def destroy(self):
+        _post_request('shares/' + self.name + '/destroy', _accesstoken=self.atoken)
+
+    def create_file(self, filename):
+        result = _post_request('files/' + self.name + '/create', _accesstoken=self.atoken, filename=filename)
+        file = File(self)
+        file._load(result)
+        return file
 
 class File(object):
-    pass
+    def __init__(self, share):
+        self.share = share
 
+    def _load(self, result):
+        self.name = result['filename']
+        self.id = result['fileid']
+        self.downloads = result['downloads']
+        self.readystate = result['readystate']
+        self.created = datetime.datetime.fromtimestamp(result['created'])
+        self.url = result.get('getturl')
+
+        if 'upload' in result:
+            self.put_url = result['upload']['puturl']
+        else:
+            self.put_url = None
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.