Ejemplo n.º 1
0
    def patch(self, user_id: str):
        """ updates the details of a user.

        Args:
            user_id: id of the user.

        Returns:
            a response object (either directly or implicitly by the framework)

        """

        patch = request.get_json()

        logger.debug('update user: details recieved: {}'.format(patch))

        try:
            user = controller.update_user_details(user_id, **patch)
        except KeyError:
            message = "no record found for user: {}".format(user_id)
            logger.error(message)
            return utils.format_error(message), 404

        resp_dict = {
            '_data': self._json_mapper(user),
            '_description': None,
            '_links': self._generate_hateoas_links(user_id)
        }

        status = 200

        headers = {'Location': api.url_for(User, user_id=user.id)}

        return resp_dict, status, headers
Ejemplo n.º 2
0
    def post(self):
        """ adds a user to the system.

        Args:
            None

        Returns:
            a response object (either directly or implicitly by the framework)

        """

        payload = request.get_json()

        logger.debug('create user: details recieved: {}'.format(payload))

        try:
            user = controller.add_user(email=payload['email'],
                                       name=payload['name'],
                                       college=payload['college'])
        except KeyError:
            message = "unable to parse one of the following: email, name, college"
            logger.error(message)
            return utils.format_error(message), 400

        resp_dict = {
            '_data': User._json_mapper(user),
            '_description': None,
            '_links': User._generate_hateoas_links(user.id)
        }

        status = 201

        headers = {'Location': api.url_for(User, user_id=user.id)}

        return resp_dict, status, headers
Ejemplo n.º 3
0
    def get(self, user_id: str):
        """ fetches the recommendations of a user.

        Paginated for optimum performance across users.

        Args:
            user_id: id of the user.

        Returns:
            a response object (either directly or implicitly by the framework)

        """

        user = controller.get_user(user_id)
        if user is None:
            return utils.format_error("the user ID was not found"), 404

        offset = int(request.args.get('offset', 0))

        limit = int(request.args.get('limit', 50))

        logger.debug(
            'recieved a request to get the recommendations for user {} with offset {} and limit {}'
            .format(user_id, offset, limit))

        recommended_users = controller.get_recommendations(
            user_id, offset, limit)

        link_for_next_page = {
            'rel':
            'next',
            'href':
            api.url_for(Recommendation,
                        user_id=user_id,
                        offset=offset + len(recommended_users),
                        limit=limit),
            'action':
            'GET',
            'types': ['application/json']
        }

        resp_dict = {
            '_data': [self._json_mapper(user) for user in recommended_users],
            '_description': None,
            '_links':
            [link_for_next_page] + self._generate_hateoas_links(user_id)
        }

        return resp_dict
Ejemplo n.º 4
0
    def _generate_hateoas_links(user_id: str) -> List[Dict]:
        """  This method collects and returns all related resources as links.

        A link is the description of a resource. Each link contains sufficient information for a client
        to be able to fully navigate to the resource.

        This enables HATEOAS for our REST API.

        Args:
            user_id: the user id defining the resource

        Returns:
            a list of the links

        """

        return [{
            'rel': 'self',
            'href': api.url_for(User, user_id=user_id),
            'action': 'GET',
            'types': ['application/json']
        }]