from flask import Flask, jsonify, request import random import datetime import platform import os import math app = Flask(__name__) @app.route('/') def home(): return jsonify({ 'api': 'Python Flask Tools API', 'version': '1.0.0', 'description': 'A practical API with useful endpoints for testing and development', 'endpoints': { '/': 'API information', '/time': 'Current timestamp', '/random': 'Random data and quotes', '/health': 'Health check', '/calc': 'Mathematical calculations', '/info': 'System information' }, 'examples': { 'time': '/time', 'random': '/random', 'calc': '/calc?operation=add&a=5&b=3', 'health': '/health' } }) @app.route('/time') def get_time(): return jsonify({ 'current_time': datetime.datetime.now().isoformat(), 'timestamp': datetime.datetime.now().timestamp(), 'timezone': 'UTC', 'date': datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S') }) @app.route('/random') def get_random(): quotes = [ 'The only way to do great work is to love what you do. - Steve Jobs', 'Life is what happens to you while you are busy making other plans. - John Lennon', 'The future belongs to those who believe in the beauty of their dreams. - Eleanor Roosevelt', 'It is during our darkest moments that we must focus to see the light. - Aristotle', 'The only impossible journey is the one you never begin. - Tony Robbins', 'In the middle of difficulty lies opportunity. - Albert Einstein', 'Success is not final, failure is not fatal: it is the courage to continue that counts. - Winston Churchill', 'The way to get started is to quit talking and begin doing. - Walt Disney' ] return jsonify({ 'quote': random.choice(quotes), 'number': random.randint(1, 100), 'boolean': random.choice([True, False]), 'list_item': random.choice(['apple', 'banana', 'cherry', 'date', 'elderberry']), 'uuid': str(random.randint(1000, 9999)) + '-' + str(random.randint(100, 999)), 'color': random.choice(['#FF6B6B', '#4ECDC4', '#45B7D1', '#96CEB4', '#FFEAA7']) }) @app.route('/health') def health_check(): return jsonify({ 'status': 'healthy', 'uptime': 'running', 'service': 'Python Flask Tools API', 'timestamp': datetime.datetime.now().isoformat() }), 200 @app.route('/calc') def calculate(): try: operation = request.args.get('operation', 'add') a = float(request.args.get('a', 0)) b = float(request.args.get('b', 0)) if request.args.get('b') is not None else None operations = { 'add': lambda x, y: x + y, 'subtract': lambda x, y: x - y, 'multiply': lambda x, y: x * y, 'divide': lambda x, y: x / y if y != 0 else 'Cannot divide by zero', 'power': lambda x, y: x ** y, 'sqrt': lambda x, y: math.sqrt(x) if b is None else 'Only one parameter needed for sqrt', 'modulo': lambda x, y: x % y } if operation not in operations: return jsonify({ 'error': f'Operation {operation} not supported', 'available_operations': list(operations.keys()) }), 400 result = operations[operation](a, b) return jsonify({ 'operation': operation, 'a': a, 'b': b, 'result': result, 'calculation': f"{a} {operation} {b} = {result}" }) except ValueError: return jsonify({'error': 'Invalid numbers provided. Use /calc?operation=add&a=5&b=3'}), 400 except Exception as e: return jsonify({'error': str(e)}), 500 @app.route('/info') def system_info(): return jsonify({ 'python_version': platform.python_version(), 'platform': platform.platform(), 'processor': platform.processor(), 'hostname': platform.node(), 'machine': platform.machine(), 'current_directory': os.getcwd(), 'environment': { 'HOSTNAME': os.getenv('HOSTNAME', 'N/A'), 'POD_NAME': os.getenv('POD_NAME', 'N/A') } }) if __name__ == '__main__': app.run(host='0.0.0.0', port=5000, debug=False)