Commits

mickael9 committed f05e87e

Improved API wrapper

- Added some fields to User
- User.files should be dict, not list
- Added refresh method to User
- Added destroy method to File
- API errors are now also printed when DEBUG = True

  • Participants
  • Parent commits cedd61c

Comments (0)

Files changed (1)

 
     result = json.loads(resp.decode('utf-8'))
 
-    if "error" in result:
-        raise APIError(result["error"])
-
     if DEBUG:
         print(req.full_url + " -->")
         pprint.pprint(result)
         print()
 
+    if "error" in result:
+        raise APIError(result["error"])
+
     return result
 
 class User(object):
     def _load(self, result):
         self.atoken = result['accesstoken']
         self.rtoken = result['refreshtoken']
+        self.userid = result['user']['userid']
+        self.email = result['user']['email']
+        self.full_name = result['user']['fullname']
         self.storage_used = result['user']['storage']['used']
         self.storage_limit = result['user']['storage']['limit']
 
+    def refresh(self):
+        self.login_token(self.rtoken)
+
     def login_auth(self, email, password):
         result = _post_request('users/login', apikey=API_KEY, email=email, password=password)
         self._load(result)
         self.title = result.get('title')
         self.created = datetime.datetime.fromtimestamp(result['created'])
         self.url = result.get('getturl')
-        self.files = []
+        self.files = {}
 
         for file_result in result['files']:
             f = File(self)
             f._load(file_result)
 
-            self.files.append(f)
+            self.files[f.id] = f
 
     def refresh(self):
         result = _get_request('shares/' + self.name)
 class UserShare(Share):
     def __init__(self, user):
         self.user = user
-    
+
     def update(self, **fields):
         result = _post_request('shares/' + self.name + '/update', _accesstoken=self.user.atoken, **fields)
         self._load(result)
         else:
             self.put_url = None
 
+    def destroy(self):
+        _post_request('files/' + self.share.name + '/' + str(self.id) + '/destroy', _accesstoken=self.share.user.atoken)
+
     def refresh(self):
         result = _get_request('files/' + self.share.name + '/' + str(self.id))
         self._load(result)