API Endpoints
/products
Description: Fetch detailed product information based on various criteria.
Parameteres:
keyword (string): Search term to find products.
category (string): Filter by specific product categories.
sort (string): Sorting options (e.g., price, popularity).
page (int): Pagination for large datasets.
Response: JSON object containing product name, ID, price, rating, and description.
/product/{product_id}
Description: Retrieve detailed information for a specific product using its unique ID.
Parameteres:
product_id (string): Unique ID for the product.
Response: JSON object with product details including title, price, features, and customer reviews.
/reviews
Description: Fetch customer reviews and ratings for a specific product.
Parameteres:
product_id (string): Unique ID for the product.
Response: JSON object containing reviewer names, ratings, review texts, and helpfulness votes.
/offers
Description: Retrieve current offers, discounts, and availability for a specific product.
Parameteres:
product_id (string): Unique ID for the product.
Response: JSON object with offer details, including price, discount percentages, and stock availability.
/related
Description: Get recommendations for related products based on a specific product.
Parameteres:
product_id (string): Unique ID for the product.
Response: JSON object containing a list of related product IDs and names.
/categories
Description: Retrieve a list of available product categories on buymie.
Response: JSON object containing category names and IDs for filtering products.
/search
Description: Search for products based on various criteria like keywords and filters.
Parameteres:
query (string): Search term to find products.
sort (string): Sorting options (e.g., price, rating).
page (int): Pagination for large datasets.
Response: JSON object with a list of products matching the search criteria.
/price-history
Description: Retrieve historical pricing data for a specific product.
Parameteres:
product_id (string): Unique ID for the product.
Response: JSON object containing price changes over time, including dates and corresponding prices.
/store-locations
Description: Fetch store locations that offer delivery or pickup options.
Parameteres:
zipcode (string): ZIP code for location-based searching.
Response: JSON object containing store names, addresses, and operating hours.
/cart
Description: Manage the shopping cart for a user session.
Parameteres:
user_id (string): Unique identifier for the user.
action (string): Action to perform (add, remove, update).
product_id (string): Unique ID of the product.
Response: JSON object confirming cart status and updated totals.
/checkout
Description: Process the checkout for a user’s cart.
Parameteres:
user_id (string): Unique identifier for the user.
payment_info (object): Payment details for transaction processing.
Response: JSON object containing order confirmation details and estimated delivery time.
API Response Format
All responses are returned in JSON format for easy integration into your application.
from flask import Flask, jsonify, request
app = Flask(__name__)
# Sample data for demonstration purposes
products = [
{"id": "1", "name": "Apple", "price": 1.00, "rating": 4.5, "description": "Fresh apples"},
{"id": "2", "name": "Banana", "price": 0.50, "rating": 4.0, "description": "Ripe bananas"},
# Add more products as needed
]
# Sample data for reviews
reviews = {
"1": [
{"reviewer": "Alice", "rating": 5, "text": "Great apples!", "helpfulness": 10},
{"reviewer": "Bob", "rating": 4, "text": "Very fresh.", "helpfulness": 5}
],
"2": [
{"reviewer": "Charlie", "rating": 4, "text": "Tasty bananas.", "helpfulness": 3}
]
}
@app.route('/products', methods=['GET'])
def get_products():
keyword = request.args.get('keyword', '')
category = request.args.get('category', '')
sort = request.args.get('sort', 'name')
page = int(request.args.get('page', 1))
# Filter and sort products (basic implementation)
filtered_products = [p for p in products if keyword.lower() in p['name'].lower()]
sorted_products = sorted(filtered_products, key=lambda x: x[sort])
return jsonify(sorted_products)
@app.route('/product/', methods=['GET'])
def get_product(product_id):
product = next((p for p in products if p['id'] == product_id), None)
if product:
return jsonify(product)
return jsonify({"error": "Product not found"}), 404
@app.route('/reviews', methods=['GET'])
def get_reviews():
product_id = request.args.get('product_id')
product_reviews = reviews.get(product_id, [])
return jsonify(product_reviews)
@app.route('/offers', methods=['GET'])
def get_offers():
product_id = request.args.get('product_id')
# This is a placeholder; implement offer logic as needed
offers = {"product_id": product_id, "offers": [{"discount": "10%", "availability": "In Stock"}]}
return jsonify(offers)
@app.route('/related', methods=['GET'])
def get_related():
product_id = request.args.get('product_id')
# This is a placeholder; implement related product logic as needed
related_products = [{"id": "3", "name": "Orange"}, {"id": "4", "name": "Grapes"}]
return jsonify(related_products)
@app.route('/categories', methods=['GET'])
def get_categories():
categories = [{"id": "1", "name": "Fruits"}, {"id": "2", "name": "Vegetables"}]
return jsonify(categories)
@app.route('/search', methods=['GET'])
def search_products():
query = request.args.get('query', '')
sort = request.args.get('sort', 'name')
page = int(request.args.get('page', 1))
# Implement search logic (basic implementation)
searched_products = [p for p in products if query.lower() in p['name'].lower()]
sorted_products = sorted(searched_products, key=lambda x: x[sort])
return jsonify(sorted_products)
@app.route('/price-history', methods=['GET'])
def get_price_history():
product_id = request.args.get('product_id')
# This is a placeholder; implement price history logic as needed
price_history = [{"date": "2023-10-01", "price": 1.00}, {"date": "2023-10-10", "price": 1.20}]
return jsonify(price_history)
@app.route('/store-locations', methods=['GET'])
def get_store_locations():
zipcode = request.args.get('zipcode')
# This is a placeholder; implement location logic as needed
locations = [{"name": "Store A", "address": "123 Main St", "hours": "9am-9pm"}]
return jsonify(locations)
@app.route('/cart', methods=['POST'])
def manage_cart():
user_id = request.json.get('user_id')
action = request.json.get('action')
product_id = request.json.get('product_id')
# Implement cart management logic here
return jsonify({"status": "success", "message": f"Product {action} to cart for user {user_id}."})
@app.route('/checkout', methods=['POST'])
def checkout():
user_id = request.json.get('user_id')
payment_info = request.json.get('payment_info')
# Implement checkout logic here
return jsonify({"status": "success", "message": "Checkout completed successfully."})
if __name__ == '__main__':
app.run(debug=True)