Exemple #1
0
    def post(self):
        """ Creates a match

            This operation is transactional

            Method: POST
            Path: /matches

            URI Parameters:
            pretty          [true|false]        whether to output in human readable format or not

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            match           JSON object         data for the match

            :return: the key for the created match
        """
        match_data = self.get_from_body('match')
        try:
            guest_id = match_data['guest']
            guest_points = match_data['guestPoints']
            host_id = match_data['host']
            host_points = match_data['hostPoints']
            if host_id is guest_id:
                self.write_signed_error(400, 'Host and guest id cannot be equal')
            match = Match(guest=ndb.Key(User, guest_id),
                          guest_points=guest_points,
                          host=ndb.Key(User, host_id),
                          host_points=host_points)
            # Asynchronously update player data
            guest_future = User.get_by_id_async(guest_id)
            host_future = User.get_by_id_async(host_id)
            guest = guest_future.get_result()
            host = host_future.get_result()
            guest.experience += guest_points
            host.experience += host_points
            # Update entities
            results = _update_entities(match, host, guest)
            # Prepare message
            match_key = results[0].urlsafe()
            self.write_signed_message(201, 'id', match_key)
        except (KeyError, TypeError):
            self.write_signed_error(400, 'Missing attributes for match')
        except TransactionFailedError:
            self.write_signed_error(507, 'Unable to store match')
        except AttributeError:
            self.write_signed_error(400, 'Invalid id')
        except BadValueError:
            # Thrown when model validations fail
            self.write_signed_error(400, 'Invalid data')
Exemple #2
0
    def get(self):
        """ Obtains user data for a list of user id's

            Method: GET
            Path: /users

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            users           JSON array          id's of the users
            pretty          [true|false]        whether to output in human readable format or not

            Returns:
            :return: a list with the data of the requested users
        """
        users_data = self.request.get('users')
        if users_data is '':
            self.write_signed_error(400, 'No users data was provided')
        try:
            user_keys = []
            for user_id in loads(users_data):
                user_keys.append(Key(User, user_id))
            query = User.query(User.key.IN(user_keys))
            result = []
            for user in query.fetch():
                result.append({
                    'id': user.key.id(),
                    'countryCode': user.country_code,
                    'rank': user.rank
                })
            self.write_signed_message(200, 'users', result)
        except ValueError:
            self.write_signed_error(400, 'Malformed JSON')
        except AttributeError:
            self.write_signed_error(400, 'Invalid data')
Exemple #3
0
    def get(self, user_id):
        """ Obtains a user's data

            Method: GET
            Path: /users/{user_id}

            URI Parameters:
            user_id         string              id of the user

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            pretty          [true|false]        whether to output in human readable format or not

            Parameters:
            :param user_id: id of the user

            Returns:
            :return: user data with id, countryCode, rank and experience
        """
        try:
            user = User.get_by_id(user_id)
            json_result = {
                'id': user_id,
                'countryCode': user.country_code,
                'rank': user.rank,
                'experience': user.experience
            }
            self.write_signed_message(200, 'user', json_result)
        except AttributeError:
            self.write_signed_error(400, {'error': 'Invalid user id'})
Exemple #4
0
    def get(self):
        """ Obtains user data for a list of user id's

            Method: GET
            Path: /users

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            users           JSON array          id's of the users
            pretty          [true|false]        whether to output in human readable format or not

            Returns:
            :return: a list with the data of the requested users
        """
        users_data = self.request.get('users')
        if users_data is '':
            self.write_signed_error(400, 'No users data was provided')
        try:
            user_keys = []
            for user_id in loads(users_data):
                user_keys.append(Key(User, user_id))
            query = User.query(User.key.IN(user_keys))
            result = []
            for user in query.fetch():
                result.append({'id': user.key.id(),
                               'countryCode': user.country_code,
                               'rank': user.rank})
            self.write_signed_message(200, 'users', result)
        except ValueError:
            self.write_signed_error(400, 'Malformed JSON')
        except AttributeError:
            self.write_signed_error(400, 'Invalid data')
Exemple #5
0
    def get(self, user_id):
        """ Obtains a user's data

            Method: GET
            Path: /users/{user_id}

            URI Parameters:
            user_id         string              id of the user

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            pretty          [true|false]        whether to output in human readable format or not

            Parameters:
            :param user_id: id of the user

            Returns:
            :return: user data with id, countryCode, rank and experience
        """
        try:
            user = User.get_by_id(user_id)
            json_result = {'id': user_id,
                           'countryCode': user.country_code,
                           'rank': user.rank,
                           'experience': user.experience}
            self.write_signed_message(200, 'user', json_result)
        except AttributeError:
            self.write_signed_error(400, {'error': 'Invalid user id'})
Exemple #6
0
    def get(self):
        """ Obtains the global leaderboard

            Method: GET
            Path: /leaderboards

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            offset          int                 number of entries to skip
            limit           int                 maximum number of entries to return
            pretty          [true|false]        whether to output in human readable format or not

            Returns:
            :return: an array of user id's
        """
        query = User.query().order(-User.experience)
        self.query_leaderboard(query)
    def get(self):
        """ Obtains the global leaderboard

            Method: GET
            Path: /leaderboards

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            offset          int                 number of entries to skip
            limit           int                 maximum number of entries to return
            pretty          [true|false]        whether to output in human readable format or not

            Returns:
            :return: an array of user id's
        """
        query = User.query().order(-User.experience)
        self.query_leaderboard(query)
Exemple #8
0
    def post(self):
        """ Creates or updates a user

            Method: POST
            Path: /users

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            user            JSON object         data for the user
            pretty          [true|false]        whether to output in human readable format or not

            Returns:
            :return: a JSON object with the id of the user
        """
        user_id = self.get_from_body('id')
        if user_id is None or user_id is '':
            self.write_message(400, 'error', 'No user id was provided')
            return
        User(id=user_id,
             country_code=self.request.headers['X-Appengine-Country']).put()
        self.write_signed_message(201, 'id', user_id)
    def get(self, country_code):
        """ Obtains the leaderboard for a country

            Method: GET
            Path: /leaderboards/{country_code}

            URI Parameters:
            country_code    sting               code for the country of the leaderboard

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            offset          int                 number of entries to skip
            limit           int                 maximum number of entries to return
            pretty          [true|false]        whether to output in human readable format or not

            Parameters:
            :param country_code: code for the country of the leaderboard

            Returns:
            :return: an array of users id's
        """
        query = User.query().order(-User.experience).filter(User.country_code == country_code)
        self.query_leaderboard(query)
Exemple #10
0
    def get(self, country_code):
        """ Obtains the leaderboard for a country

            Method: GET
            Path: /leaderboards/{country_code}

            URI Parameters:
            country_code    sting               code for the country of the leaderboard

            Request Parameters:
            accessToken     string              token required to gain access to the resource
            offset          int                 number of entries to skip
            limit           int                 maximum number of entries to return
            pretty          [true|false]        whether to output in human readable format or not

            Parameters:
            :param country_code: code for the country of the leaderboard

            Returns:
            :return: an array of users id's
        """
        query = User.query().order(-User.experience).filter(
            User.country_code == country_code)
        self.query_leaderboard(query)