def update_wishlist_item(wishlist_id, item_id): """ The route for modifying the description of an item in a specific wishlist. Example: curl -i -H 'Content-Type: application/json' -X PUT -d '{"description":"update product!"}' http://127.0.0.1:5000/wishlists/1/items/i123 H is for headers, X is used to specify HTTP Method, d is used to pass a message. """ try: data = request.get_json() data['id'] = item_id except TypeError: (jsonify("Invalid input data type"), status.HTTP_400_BAD_REQUEST) if is_valid(data, 'item'): try: wl = Wishlist.find_or_404(wishlist_id) wl.update_item(data) wl.save_wishlist() new_wl = wl.find(wishlist_id) return make_response(jsonify(new_wl.serialize_wishlist()), status.HTTP_200_OK) except WishlistException: message = {'error': 'Wishlist %s was not found' % wishlist_id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND) except ItemException: message = {'error': 'Item %s was not found' % item_id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND) else: message = {'error': 'Item data was not valid'} return make_response(jsonify(message), status.HTTP_400_BAD_REQUEST)
def read_wishlist(wishlist_id): """ The route for reading wishlists, whether one specifically by id or all wishlists when no id is specified. Example: curl http://127.0.0.1:5000/wishlists/1 """ try: wl = Wishlist.find_or_404(wishlist_id) return make_response(jsonify(wl.serialize_wishlist()), status.HTTP_200_OK) except WishlistException: return make_response( jsonify(message='Cannot retrieve wishlist with id %s' % wishlist_id), status.HTTP_404_NOT_FOUND)
def item(wishlist_id): """ The route for getting all items associated with a wishlist or making a new item for a wishlist via a POST. Example: curl http://127.0.0.1:5000/wishlists/1/items """ try: wl = Wishlist.find_or_404(wishlist_id) items = wl.all_items() return make_response(jsonify(items), status.HTTP_200_OK) except WishlistException: return make_response( jsonify(message='Cannot retrieve wishlist with id %s' % wishlist_id), status.HTTP_404_NOT_FOUND)
def read_wishlist_item(wishlist_id, item_id): """ Retrieve a single Wishlist item This endpoint will return a Wishlist item based on it's ID --- tags: - Wishlist Items produces: - application/json parameters: - name: wishlist_id in: path description: ID of wishlist to retrieve from type: integer required: true - name: item_id in: path description: ID of item to be retrieved type: string required: true responses: 200: description: Wishlist items matching with the query schema: id: Wishlist properties: id: type: string description: ID of the item matching description: type: string description: Description of the item 404: description: Wishlist not found """ try: wl = Wishlist.find_or_404(wishlist_id) item = wl.find_item(item_id) return make_response(jsonify(item), status.HTTP_200_OK) except ItemException: return make_response( jsonify(message='Item with id %s could not be found' % item_id), status.HTTP_404_NOT_FOUND) except WishlistException: return make_response( jsonify(message='Wishlist with id %d could not be found' % wishlist_id), status.HTTP_404_NOT_FOUND)
def clear_wishlist(wishlist_id): """ The route for clearing a wishlist specified by wishlist_id without deleting the wishlist itself. Example: curl -X PUT http://127.0.0.1:5000/wishlists/1/items/clear """ try: wl = Wishlist.find_or_404(wishlist_id) wl.remove_item(None) wl.save_wishlist() new_wl = wl.find(wishlist_id) return make_response(jsonify(new_wl.serialize_wishlist()), status.HTTP_200_OK) except WishlistException: message = {'error': 'Wishlist %s was not found' % wishlist_id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND)
def read_wishlist_item(wishlist_id, item_id): """ The route for retrieving a specific item in a wishlist. Example: curl http://127.0.0.1:5000/wishlists/1/items/i123 """ try: wl = Wishlist.find_or_404(wishlist_id) item = wl.find_item(item_id) return make_response(jsonify(item), status.HTTP_200_OK) except ItemException: return make_response( jsonify(message='Item with id %s could not be found' % item_id), status.HTTP_404_NOT_FOUND) except WishlistException: return make_response( jsonify(message='Wishlist with id %d could not be found' % wishlist_id), status.HTTP_404_NOT_FOUND)
def update_wishlist(id): """ The route for modifying a wishlist's user_id or name. Example: curl -i -H 'Content-Type: application/json' -X PUT -d '{"name":"new_name","user_id":110}' http://127.0.0.1:5000/wishlists/1 H is for headers, X is used to specify HTTP Method, d is used to pass a message. """ data = request.get_json() if is_valid(data, 'wishlist'): try: wl = Wishlist.find_or_404(id) wl.deserialize_wishlist(data) wl.save_wishlist() return make_response(jsonify(wl.serialize_wishlist()), status.HTTP_200_OK) except WishlistException: message = {'error': 'Wishlist %s was not found' % id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND) else: message = {'error': 'Wishlist data was not valid'} return make_response(jsonify(message), status.HTTP_400_BAD_REQUEST)
def item(wishlist_id): """ Retrieve a list of items in the wishlist This endpoint will return all items --- tags: - Wishlist Items parameters: - name: wishlist_id in: path description: ID of the wishlist from which items have to be retrieved required: true type: integer responses: 200: description: Wishlist items belonging to the wishlist ID schema: id: Wishlist properties: wishlist_item_id: type: object properties: item_id: type: string description: ID of the item item_description: type: string description: Description of the item 404: description: Wishlist not found """ try: wl = Wishlist.find_or_404(wishlist_id) items = wl.all_items() return make_response(jsonify(items), status.HTTP_200_OK) except WishlistException: return make_response( jsonify(message='Cannot retrieve wishlist with id %s' % wishlist_id), status.HTTP_404_NOT_FOUND)
def add_item_to_wishlist(wishlist_id): """ The route for adding new items to the wishlist. This method can also be checked using CURL. Pre-requisite: Create a wishlist to add an item. Example: curl -i -H 'Content-Type: application/json' -X POST -d '{"id":"i123","description":"Awesome product!"}' http://127.0.0.1:5000/wishlists/1/items curl -i -H 'Content-Type: application/json' -X POST -d '{"id":"i12","description":"Apple product!"}' http://127.0.0.1:5000/wishlists/1/items """ data = request.get_json() if is_valid(data, 'item'): try: wl = Wishlist.find_or_404(wishlist_id) wl.deserialize_wishlist_items(data) wl.save_item() message = wl.serialize_wishlist() return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': wl.self_url()}) except WishlistException: message = {'error': 'Wishlist %s was not found' % wishlist_id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND) else: message = {'error': 'Item data was not valid'} return make_response(jsonify(message), status.HTTP_400_BAD_REQUEST)
def data_load_wishlist_items(data): #data_to_be_sent = {"id":data['id'], "description":data['description']} wl = Wishlist.find_or_404(data['wishlist_id']) wl.deserialize_wishlist_items(data).save_item()
def clear_wishlist(wishlist_id): """ Clears a Wishlist This endpoint will clear a Wishlist based on the wishlist_id --- tags: - Wishlists consumes: - application/json produces: - application/json parameters: - name: wishlist_id in: path description: ID of the wishlist to be cleared type: integer required: true responses: 200: description: Wishlist cleared schema: id: Wishlist properties: user_id: type: string description: Unique ID of the user(created by the user) name: type: string description: Wishlist Name(created by the user) created: type: string format: date-time description: The time at which the wishlist was created deleted: type: boolean description: Flag to be set when a wishlist is deleted items: type: object properties: wishlist_item_id: type: object properties: item_id: type: string description: Original ID of the item item_description: type: string description: Description of the item description: Dictionary to store objects in a wishlist id: type: integer description: Unique ID of the wishlist assigned internally by the server 404: description: Wishlist not found """ try: wl = Wishlist.find_or_404(wishlist_id) wl.remove_item(None) wl.save_wishlist() new_wl = wl.find(wishlist_id) return make_response(jsonify(new_wl.serialize_wishlist()), status.HTTP_200_OK) except WishlistException: message = {'error': 'Wishlist %s was not found' % wishlist_id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND)
def update_wishlist_item(wishlist_id, item_id): """ Update a Wishlist Item This endpoint will update a Wishlist Item based the body that is posted --- tags: - Wishlist Items consumes: - application/json produces: - application/json parameters: - name: wishlist_id in: path description: ID of wishlist to which the item belongs type: integer required: true - name: item_id in: path description: ID of Item to be updated type: String required: true - in: body name: body schema: id: data required: - description properties: description: type: string description: Updated description of the item responses: 200: description: Wishlist item updated schema: id: Wishlist properties: user_id: type: string description: Unique ID of the user(created by the user) name: type: string description: Wishlist Name(created by the user) created: type: string format: date-time description: The time at which the wishlist was created deleted: type: boolean description: Flag to be set when a wishlist is deleted items: type: object properties: wishlist_item_id: type: object properties: item_id: type: string description: Original ID of the item item_description: type: string description: Description of the item description: Dictionary to store objects in a wishlist id: type: integer description: Unique ID of the wishlist assigned internally by the server 404: description: Wishlist/Item not found. 400: description: Bad Request (the posted data was not valid) """ try: data = request.get_json() data['id'] = item_id except TypeError: (jsonify("Invalid input data type"), status.HTTP_400_BAD_REQUEST) if is_valid(data, 'item'): try: wl = Wishlist.find_or_404(wishlist_id) wl.update_item(data) wl.save_wishlist() new_wl = wl.find(wishlist_id) return make_response(jsonify(new_wl.serialize_wishlist()), status.HTTP_200_OK) except WishlistException: message = {'error': 'Wishlist %s was not found' % wishlist_id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND) except ItemException: message = {'error': 'Item %s was not found' % item_id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND) else: message = {'error': 'Item data was not valid'} return make_response(jsonify(message), status.HTTP_400_BAD_REQUEST)
def update_wishlist(id): """ Update a Wishlist This endpoint will update a Wishlist based on the body that is put --- tags: - Wishlists consumes: - application/json produces: - application/json parameters: - name: id in: path description: ID of wishlist to update type: integer required: true - in: body name: body schema: id: data required: - name - user_id properties: name: type: string description: New name for the Wishlist user_id: type: string description: User ID of the user owning the wishlist responses: 200: description: Wishlist updated schema: id: Wishlist properties: user_id: type: string description: Unique ID of the user(created by the user) name: type: string description: Wishlist Name(created by the user) created: type: string format: date-time description: The time at which the wishlist was created deleted: type: boolean description: Flag to be set when a wishlist is deleted items: type: object properties: wishlist_item_id: type: object properties: item_id: type: string description: Original ID of the item item_description: type: string description: Description of the item description: Dictionary to store objects in a wishlist id: type: integer description: Unique ID of the wishlist assigned internally by the server 404: description: Wishlist not found 400: description: Bad Request (the posted data was not valid) """ data = request.get_json() if is_valid(data, 'wishlist'): try: wl = Wishlist.find_or_404(id) wl.deserialize_wishlist(data) wl.save_wishlist() return make_response(jsonify(wl.serialize_wishlist()), status.HTTP_200_OK) except WishlistException: message = {'error': 'Wishlist %s was not found' % id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND) else: message = {'error': 'Wishlist data was not valid'} return make_response(jsonify(message), status.HTTP_400_BAD_REQUEST)
def read_wishlist(wishlist_id): """ Retrieve a single Wishlist This endpoint will return a Wishlist based on it's ID --- tags: - Wishlists produces: - application/json parameters: - name: wishlist_id in: path description: ID of wishlist to retrieve type: integer required: true responses: 200: description: Wishlist retrieved schema: id: Wishlist properties: user_id: type: string description: Unique ID of the user(created by the user) name: type: string description: Wishlist Name(created by the user) created: type: string format: date-time description: The time at which the wishlist was created deleted: type: boolean description: Flag to be set when a wishlist is deleted items: type: object properties: wishlist_item_id: type: object properties: item_id: type: string description: Original ID of the item item_description: type: string description: Description of the item description: Dictionary to store objects in a wishlist id: type: integer description: Unique ID of the wishlist assigned internally by the server 404: description: Wishlist not found """ try: wl = Wishlist.find_or_404(wishlist_id) return make_response(jsonify(wl.serialize_wishlist()), status.HTTP_200_OK) except WishlistException: return make_response( jsonify(message='Cannot retrieve wishlist with id %s' % wishlist_id), status.HTTP_404_NOT_FOUND)
def add_item_to_wishlist(wishlist_id): """ Add a Wishlist Item to an existing wishlist This endpoint will add a wishlist item based on the data in the body that is posted --- tags: - Wishlist Items consumes: - application/json produces: - application/json parameters: - name: wishlist_id in: path description: ID of wishlist to which the item has to be added to type: integer required: true - in: body name: body required: true schema: id: data required: - id - description properties: id: type: string description: ID of the wishlist item description: type: string description: Description of the item to be added to the wishlist responses: 201: description: Wishlist item created schema: id: Wishlist properties: user_id: type: string description: Unique ID of the user(created by the user) name: type: string description: Wishlist Name(created by the user) created: type: string format: date-time description: The time at which the wishlist was created deleted: type: boolean description: Flag to be set when a wishlist is deleted items: type: object properties: wishlist_item_id: type: object properties: item_id: type: string description: Original ID of the item item_description: type: string description: Description of the item description: Dictionary to store objects in a wishlist id: type: integer description: Unique ID of the wishlist assigned internally by the server 400: description: Bad Request (the posted data was not valid) """ data = request.get_json() if is_valid(data, 'item'): try: wl = Wishlist.find_or_404(wishlist_id) wl.deserialize_wishlist_items(data) wl.save_item() message = wl.serialize_wishlist() return make_response(jsonify(message), status.HTTP_201_CREATED, {'Location': wl.self_url()}) except WishlistException: message = {'error': 'Wishlist %s was not found' % wishlist_id} return make_response(jsonify(message), status.HTTP_404_NOT_FOUND) else: message = {'error': 'Item data was not valid'} return make_response(jsonify(message), status.HTTP_400_BAD_REQUEST)