This project provides a simple, serverless proxy that uses a Google Cloud Function to expose two endpoints from the Yahoo Finance API. The endpoints allow you to search for stocks and ETFs and retrieve their latest price data.
- Search Endpoint (
/search): Search for financial instruments by name, ticker, ISIN, or description. - Price Endpoint (
/price): Get the latest price and recent performance data for a specific ticker. - Serverless: Deployed as a Google Cloud Function for easy scaling and management.
- Validated: Includes a full suite of unit tests and linting to ensure code quality.
- Method:
GET - Query Parameters:
query(required): The search term to look for.
- Success Response (
200 OK):- A JSON array of matching stocks, sorted by trading volume.
- Example:
GET /search?query=tesla[ { "name": "Tesla, Inc.", "isin": "US88160R1014", "ticker": "TSLA", "description": "Tesla, Inc. designs, develops, manufactures, and sells electric vehicles...", "type": "EQUITY", "volume": 150000000 } ]
- Error Responses:
400 Bad Request: If thequeryparameter is missing.502 Bad Gateway: If the Yahoo Finance API is unavailable.
- Method:
GET - Query Parameters:
ticker(required): The stock ticker to look up.
- Success Response (
200 OK):- A JSON object with the latest price and percentage changes.
- Example:
GET /price?ticker=AAPL{ "ticker": "AAPL", "last_price": 170.0, "change_1d_percent": "6.25%", "change_7d_percent": "13.33%", "change_30d_percent": "13.33%", "change_1y_percent": "70.00%" }
- Error Responses:
400 Bad Request: If thetickerparameter is missing.404 Not Found: If the ticker is invalid or has no data.500 Internal Server Error: If an unexpected error occurs.
- Google Cloud SDK: Make sure you have the
gcloudCLI installed and authenticated. - Project ID: Have your Google Cloud Project ID ready.
- Permissions: Ensure your account has the necessary permissions to deploy Cloud Functions.
-
Clone the Repository:
git clone <repository-url> cd <repository-directory>
-
Configure the Deployment Script:
- Open the
deploy.shscript. - Set the
PROJECT_IDvariable to your Google Cloud Project ID. - (Optional) Customize the
FUNCTION_NAMEandREGIONif needed.
- Open the
-
Run the Deployment Script:
chmod +x deploy.sh ./deploy.sh
-
Verify the Deployment:
- After the script finishes, it will output the URL of your deployed function.
- You can test the endpoints using a tool like
curlor your browser:# Test the search endpoint curl "<your-function-url>/search?query=google" # Test the price endpoint curl "<your-function-url>/price?ticker=GOOGL"
To run the function locally or run the tests, follow these steps:
-
Install Dependencies:
pip install -r requirements.txt -r tests/test_requirements.txt
-
Run the Linter:
flake8 main.py tests/test_main.py
-
Run the Unit Tests:
PYTHONPATH=. pytest
To run the function on your local machine for development and testing, you can use the Google Cloud Functions Framework.
-
Install the Functions Framework:
pip install functions-framework
-
Run the Local Server: The entry point to the application is the
handlerfunction inmain.py. To start a local server, run the following command from the root of the repository:functions-framework --target=handler
The server will start on
http://localhost:8080. -
Test the Endpoints: You can now send requests to the local server using
curlor any other HTTP client.-
Search Endpoint:
curl "http://localhost:8080/search?query=Apple" -
Price Endpoint:
curl "http://localhost:8080/price?ticker=AAPL"
-