Get Stock Price with Basic Auth

parmarjatin4911@gmail.com - Jan 28 - - Dev Community

Get Stock Price with Basic Auth

from flask import Flask, request, Response
import json
import yfinance as yf

app = Flask(name)

API_KEY = "My_API_Key" # Replace with your actual API key

def validate_api_key(request):
auth_header = request.headers.get('Authorization')
if not auth_header:
return False

try:
    auth_type, provided_api_key = auth_header.split(None, 1)
    if auth_type.lower() != 'basic':
        return False

    return provided_api_key == API_KEY
except Exception:
    return False
Enter fullscreen mode Exit fullscreen mode

@app.route("/")
def index():
return "Hello world!"

@app.route('/stock', methods=['GET'])
def get_stock_data():
if not validate_api_key(request):
return Response(json.dumps({"error": "Invalid API key"}),
status=401,
mimetype='application/json')

symbol = request.args.get('symbol')
if not symbol:
    return Response(json.dumps({"error": "Symbol parameter is required"}),
                    status=400,
                    mimetype='application/json')

stock = yf.Ticker(symbol)
stock_price = stock.history(period="1d")['Close'].iloc[-1]

if stock_price is None:
    return Response(json.dumps({"error": "No current price data available for the given symbol"}),
                    status=404,
                    mimetype='application/json')

return Response(json.dumps({"symbol": symbol, "stock_price": stock_price}),
                mimetype='application/json')
Enter fullscreen mode Exit fullscreen mode

if name == "main":
app.run(host='0.0.0.0', port=81)

requirements.txt

yfinance

curl -H "Authorization: Basic My_API_Key" "https://pasttruevolume.mervinpraison.repl.co/stock?symbol=AAPL"

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player