from flask import Flask, request, jsonify, send_from_directory import os app = Flask(__name__) @app.route('/') def help(): help_text = ''' API Help

API Help

This API demonstrates various aspects of API design, including:

  1. GET /greet?name=<name> - Query parameter example. Replace <name> with your name to get a greeting.
  2. GET /math/<int:num1>/<int:num2> - URL parameter example. Replace <num1> and <num2> with integers for a simple addition result.
  3. POST /reverse - JSON body parameter example. Send a JSON object with a "text" field to get the text reversed.
  4. GET /dir/subdir - Directory and subdirectory example. An example of nested directories in API design.
  5. GET /headers - Header parameter example. Send custom header 'X-Api-Key' to see the header value in response.
  6. PUT /update - PUT method example. Send a JSON object with 'id' and 'value' fields to update an item in a simple in-memory store.
  7. GET /search?query=<query>&offset=<offset>&limit=<limit> - Optional query parameters example. Replace <query> with your search term, <offset> with the number of items to skip, and <limit> with the number of items to return.
  8. POST /upload - File upload example. Send a file with the field name 'file' to upload it to the server.
  9. GET /files/<filename> - File download example. Replace <filename> with the name of the file you uploaded to download it.
''' return help_text @app.route('/greet') def greet(): name = request.args.get('name', 'Stranger') return f'Hello, {name}!' @app.route('/math//') def math(num1, num2): result = num1 + num2 return jsonify({"result": result}) @app.route('/reverse', methods=['POST']) def reverse(): data = request.get_json() text = data.get('text', '') reversed_text = text[::-1] return jsonify({"reversed_text": reversed_text}) @app.route('/dir/subdir') def dir_subdir(): return "Welcome to the /dir/subdir endpoint!" @app.route('/headers') def headers(): api_key = request.headers.get('X-Api-Key', 'Not provided') return jsonify({"X-Api-Key": api_key}) store = {} @app.route('/update', methods=['PUT']) def update(): data = request.get_json() item_id = data.get('id') value = data.get('value') if item_id and value: store[item_id] = value return jsonify({"message": "Item updated successfully", "item": {item_id: value}}) else: return jsonify({"message": "Invalid request data"}), 400 @app.route('/search') def search(): query = request.args.get('query', '') offset = int(request.args.get('offset', 0)) limit = int(request.args.get('limit', 10)) # Simulate a list of search results all_results = [f'Result {i} for {query}' for i in range(1, 101)] paginated_results = all_results[offset:offset + limit] return jsonify({"results": paginated_results, "offset": offset, "limit": limit}) UPLOAD_FOLDER = 'uploaded_files' app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) @app.route('/upload', methods=['POST']) def upload(): if 'file' not in request.files: return jsonify({"message": "No file part"}), 400 file = request.files['file'] if file.filename == '': return jsonify({"message": "No file selected"}), 400['UPLOAD_FOLDER'], file.filename)) return jsonify({"message": "File uploaded successfully", "filename": file.filename}) @app.route('/files/') def download(filename): return send_from_directory(app.config['UPLOAD_FOLDER'], filename) if __name__ == '__main__':, host='', port=5023)