def post(self): """ Creates an application Method: POST Path: /applications Request Parameters: application JSON object data for the application pretty [true|false] whether to output in human readable format or not Returns: :return: the application key for the new application """ application_data = self.get_from_body('application') try: # Create new application user = users.get_current_user() if user is None: self.write_error(400, 'Unable assign application to user') application_key = uuid4().hex Application(id=application_key, client_secret=uuid4().hex, server_response_key=uuid4().hex, name=application_data['name'], description=application_data.get('description', ''), registrant=user.nickname()).put() self.write_message(201, 'applicationKey', application_key) except KeyError: self.write_error(400, 'Missing attributes for application') except TransactionFailedError: self.write_error(400, 'Unable to store application') except BadValueError: # Thrown when model validations fail self.write_error(400, 'Invalid data')
def post(self, application_key): """ Regenerates a server response key Method: POST Path: /applications/{application_key}/server/key URI Parameters: application_key string the key that identifies the application Request Parameters: pretty [true|false] whether to output in human readable format or not Parameters: :param application_key: the key that identifies the application Returns :return: the new client secret key """ try: application = Application.get_by_id(application_key) application.server_response_key = uuid4().hex application.put() self.write_message(200, 'serverResponseKey', application.server_response_key) except AttributeError: self.write_message(400, 'Invalid application key')
def get(self): """ Obtains a user's data Method: GET Path: /applications Request Parameters: pretty [true|false] whether to output in human readable format or not Returns: :return: all the registered applications """ query = Application.query().order(-Application.registration_timestamp) result = [] for application in query.fetch(): result.append({ 'applicationId': application.key.id(), 'name': application.name, 'apiCalls': application.api_calls, 'registrant': application.registrant, 'registered': application.registration_timestamp.isoformat() }) self.write_message(200, 'applications', result)
def get(self, application_key): """ Obtains the data for an application Method: GET Path: /applications/{application_key} URI Parameters: application_key string the key that identifies the application Request Parameters: pretty [true|false] whether to output in human readable format or not Parameters: :param application_key: the key that identifies the application Returns: :return: the data for the application """ try: application = Application.get_by_id(application_key) json_result = { 'applicationKey': application_key, 'name': application.name, 'description': application.description, 'clientSecret': application.client_secret, 'serverResponseKey': application.server_response_key } self.write_message(200, 'application', json_result) except AttributeError: self.write_error(400, 'Invalid application key')
def get(self, application_key): """ Obtains the data for an application Method: GET Path: /applications/{application_key} URI Parameters: application_key string the key that identifies the application Request Parameters: pretty [true|false] whether to output in human readable format or not Parameters: :param application_key: the key that identifies the application Returns: :return: the data for the application """ try: application = Application.get_by_id(application_key) json_result = {'applicationKey': application_key, 'name': application.name, 'description': application.description, 'clientSecret': application.client_secret, 'serverResponseKey': application.server_response_key} self.write_message(200, 'application', json_result) except AttributeError: self.write_error(400, 'Invalid application key')
def get(self): """ Authenticates the client and sends it an access token Method: GET Path: /auth Request Parameters: applicationKey string key that identifies the client as an application clientSecret string secret key to prove the client's identity pretty [true|false] whether to output in human readable format or not Returns: :return: an access token if the application key and client secret are valid; otherwise it sends the client an error """ application_key = self.request.get("applicationKey") client_secret = self.request.get("clientSecret") if application_key is "": self.write_error(400, "No application key was provided") return if client_secret is "": self.write_error(400, "No client secret was provided") return application = Application.get_by_id(application_key) if application is None: self.write_error(400, "Invalid credentials") return if application.client_secret != client_secret: self.write_error(400, "Invalid credentials") return access_token = create_access_token(application_key) response_key = application.server_response_key self.write_signed_message(200, "accessToken", access_token, response_key)
def __get_application(self): """ Gets the application that made the current request Returns: :return: the application if the access token is valid; None otherwise """ if self.request.method in ['GET', 'DELETE']: access_token = self.request.get('accessToken') else: try: access_token = loads(self.request.body).get('accessToken') except ValueError: access_token = None if access_token is None: return None application_key = get_application_key(access_token) if not application_key: return None return Application.get_by_id(application_key)
def get(self): """ Obtains a user's data Method: GET Path: /applications Request Parameters: pretty [true|false] whether to output in human readable format or not Returns: :return: all the registered applications """ query = Application.query().order(-Application.registration_timestamp) result = [] for application in query.fetch(): result.append({'applicationId': application.key.id(), 'name': application.name, 'apiCalls': application.api_calls, 'registrant': application.registrant, 'registered': application.registration_timestamp.isoformat()}) self.write_message(200, 'applications', result)
def put(self, application_key): """ Updates an application Method: PUT Path: /applications/{application_key} URI Parameters: application_key string the key that identifies the application Request Parameters: application JSON object the new data for the application pretty [true|false] whether to output in human readable format or not Parameters: :param application_key: the key that identifies the application Returns: :return: the application key """ application_data = self.get_from_body('application') application = Application.get_by_id(application_key) try: if application is not None: # Update application if application_data.get('name') is not None: application.name = application_data['name'] if application_data.get('description') is not None: application.description = application_data['description'] application.put() self.write_message(200, 'applicationKey', application_key) else: self.write_error(400, 'Invalid application key') except TransactionFailedError: self.write_error(400, 'Unable to store application') except BadValueError: # Thrown when model validations fail self.write_error(400, 'Invalid data')