Commits

Guillermo Szeliga committed 8704c53 Draft

- Interactive authorization process for box.net works.
- Some refactoring needs to be done.

Comments (0)

Files changed (5)

api/shelf/configuration/accounts.py

         return self.__account_type_class(json.dumps(account_config_values))
 
     def authorize(self, account_data):
-        print 'Connects using account info and expected adapter in order to authorize our application and return new account data'
+        client = self.__client_class(account_data)
+        client.grant_client_access()
+
+        return account_data
 
     def load(self, id=None):
         pass

api/shelf/console/shell.py

 			account_data = account_manager.create(field_values)
 
 			#3- If account needs authorization
-			#account_data = account_manager.authorize(account_data)
+			account_data = account_manager.authorize(account_data)
 
 			#4- Save data into a file
 			account_manager.save(account_data)

api/storages/adapters/box/adapter.py

+import webbrowser
+
 __author__ = 'guillermo'
 
 from api.storages.adapters.box import events
 
 		self.__account_data = account_data
 
-	def obtainAccessToken(self, sess):
+	def obtain_access_token(self, session_handler):
 
-		auth_page = sess.get_authorization_page()
+		auth_page = session_handler.get_authorization_page()
 
-		print 'You should redirect user to ' + auth_page
+		print 'You\'re being redirect to ' + auth_page + ' for further authorization'
+
+		webbrowser.open_new_tab(auth_page)
 
 		raw_input()
 
-		return sess.get_auth_token()
+		return session_handler.get_auth_token()
+
+	def __create_session_handler(self):
+		return session.BoxSession(self.__account_data.appKey())
+
+
+	def grant_client_access(self, session_handler=None):
+
+		if session_handler is None:
+			handler = self.__create_session_handler()
+		else:
+			handler = session_handler
+
+		access_token = self.obtain_access_token(handler)
+
+		self.__account_data.assignAccessToken(access_token)
+
+		return access_token
 
 	def connect(self):
 
-		boxSession = session.BoxSession(self.__account_data.appKey())
+		boxSession = self.__create_session_handler()
 
 		if not self.__account_data.isAccessGranted():
-			self.__account_data.assignAccessToken(self.obtainAccessToken(boxSession))
+			self.grant_client_access(boxSession)
 		else:
 			boxSession.set_auth_token(self.__account_data.oauthTokenSecret())
 
 
 		return events.BoxEventsBrowser(iterator, translator)
 
+	def move(self):
+		pass
+
+	def about(self):
+		pass
+
 
 class BoxAccountData(skeleton.StorageAccountData):
 
 		return skeleton.StorageAccountData._list_account_field('box')
 
 	def jsonize(self):
-		return dict({'appKey': self._appKey, 'OAuthToken' : self._oauth_token, 'OAuthTokenSecret': self._oauth_token_secret, 'unique_id': self.unique_id()})
+		return dict({'appKey': self._appKey, 'OAuthToken' : self._oauth_token, 'OAuthTokenSecret': self._oauth_token_secret, 'unique_id': self.unique_id()})

api/storages/adapters/dropbox/adapter.py

 		skeleton.StorageAdapter.__init__(self)
 		self._account_data = account_data
 
-	def obtainAccessToken(self, sess):
+	def obtain_access_token(self, sess):
 
 		request_token =  sess.obtain_request_token()
 
 		sess = session.DropboxSession(self._account_data.appKey(), self._account_data.appSecret(), self._account_data.accessType())		
 
 		if not self._account_data.isAccessGranted():
-			self._account_data.assignAccessToken(self.obtainAccessToken(sess))
+			self._account_data.assignAccessToken(self.obtain_access_token(sess))
 		else:
 			sess.set_token(self._account_data.oauthToken(), self._account_data.oauthTokenSecret())
 			print 'We are already granted ' + str(sess.token)

api/storages/adapters/skeleton.py

 		pass
 
 	@abc.abstractmethod
+	def obtain_access_token(self, session_handler):
+		pass
+
+	@abc.abstractmethod
+	def grant_client_access(self,session_handler=None):
+		pass
+
+	@abc.abstractmethod
 	def about(self):
 		pass
 
 
 		return fields_registry
 
+	def __str__(self):
+		return self.toJSON()
+
 
 class AccountFieldDescriptor():