-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
839 additions
and
35 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[tool.poetry] | ||
name = "fugle-market-data" | ||
version = "0.1.0" | ||
description = "Fetch market data from Fugle API" | ||
authors = ["wildfootw <[email protected]>", "soyccan <[email protected]>"] | ||
|
||
[tool.poetry.dependencies] | ||
python = "^3.11" | ||
fugle-marketdata = "^1.0.2" | ||
fastapi = "^0.104.1" | ||
|
||
[build-system] | ||
requires = ["poetry-core"] | ||
build-backend = "poetry.core.masonry.api" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,21 +13,40 @@ | |
# Copyright (c) 2023 by wildfootw <[email protected]> | ||
# | ||
|
||
import logging | ||
import os | ||
|
||
from fastapi import FastAPI | ||
from fugle_marketdata import RestClient | ||
|
||
fugle_marketdata_api_key = os.getenv('FUGLE_MARKET_DATA_API_KEY') | ||
|
||
def get_env_or_raise(name: str) -> str: | ||
value = os.getenv(name) | ||
if value is None: | ||
raise ValueError(f'Environment variable "{name}" not set') | ||
return value | ||
|
||
fugle_marketdata_api_key = get_env_or_raise('FUGLE_MARKET_DATA_API_KEY') | ||
|
||
app = FastAPI() | ||
|
||
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) | ||
|
||
def get_stock_price(symbol: str): | ||
client = RestClient(api_key=fugle_marketdata_api_key) | ||
data = client.stock.intraday.quote(symbol=symbol) | ||
|
||
if not isinstance(data, dict): | ||
logging.error(f"Unexpected response: {data}") | ||
return {'error': repr(data)} | ||
|
||
if (status_code := data.get("statusCode")) and status_code != 200: | ||
logging.error(f"Failed to get stock price: {data.get('message')}") | ||
return {'error': data.get('message')} | ||
|
||
# Extracting specific data from the response | ||
price_data = { | ||
"symbol": symbol, | ||
"symbol": symbol, | ||
"name": data['name'], | ||
"lastPrice": data['lastPrice'], | ||
"openPrice": data['openPrice'], | ||
|
Oops, something went wrong.