Skip to content
Merged

Dev #97

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ To run this project, you will need to add the following environment variables to
| `BRAPI_TOKEN` | Token for accessing Brapi.dev API | Yes | - |
| `CLIENT_API_KEY` | Secret key to authenticate clients accessing this API | Yes | - |
| `CORS_ORIGIN` | Allowed origin for CORS | No | `*` |
| `FLASK_ENV` | Environment mode (`production` enables Redis) | No | `development` |
| `REDIS_HOST` | Redis Hostname (Required if Prod) | No | - |
| `REDIS_PORT` | Redis Port (Required if Prod) | No | - |
| `REDIS_PASSWORD` | Redis Password (Required if Prod) | No | - |

### Caching

This application uses **Redis** for caching in **production** environments (`FLASK_ENV=production`).
In **development** mode (default), caching is disabled (`NullCache`) to facilitate debugging.

### Setting up variables

Expand All @@ -40,6 +49,12 @@ GENAI_API_KEY=your_genai_key
BRAPI_TOKEN=your_brapi_token
CLIENT_API_KEY=your_client_secret_key
CORS_ORIGIN=http://localhost:3000

# Redis (Production only)
FLASK_ENV=production
REDIS_HOST=mx.redis-server.com
REDIS_PORT=6379
REDIS_PASSWORD=secret
```

Or export them in your terminal:
Expand Down
24 changes: 24 additions & 0 deletions api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,31 @@
from dotenv import load_dotenv
import os
from controllers import genai, brapi
from flask_caching import Cache

load_dotenv()
cors_origin = os.getenv('CORS_ORIGIN', '*')
api = Flask(__name__)
cors=CORS(api, resources={r"/*": {"origins": cors_origin}})
api.config['CORS_HEADERS'] = 'Content-Type'

# Cache Configuration
is_prod = os.getenv('FLASK_ENV') == 'production'
if is_prod:
cache_config = {
'CACHE_TYPE': 'RedisCache',
'CACHE_REDIS_HOST': os.getenv('REDIS_HOST'),
'CACHE_REDIS_PORT': os.getenv('REDIS_PORT'),
'CACHE_REDIS_PASSWORD': os.getenv('REDIS_PASSWORD')
}
else:
cache_config = {
'CACHE_TYPE': 'NullCache'
}

api.config.from_mapping(cache_config)
cache = Cache(api)

def require_api_key(f):
@wraps(f)
def decorated_function(*args, **kwargs):
Expand Down Expand Up @@ -63,6 +81,7 @@ def ticket_resume_show(name):
@api.route('/brapi/quote/<string:name>')
@cross_origin()
@require_api_key
@cache.cached(timeout=300)
def brapi_quote_show(name):
result = brapi.Brapi.get_stock_data(name)
if result:
Expand All @@ -72,6 +91,7 @@ def brapi_quote_show(name):
@api.route('/brapi/async_quote/<string:name>')
@cross_origin()
@require_api_key
@cache.cached(timeout=300)
async def brapi_async_quote_show(name):
result = await brapi.Brapi.get_async_stock_data(name)
if result:
Expand All @@ -81,6 +101,7 @@ async def brapi_async_quote_show(name):
@api.route('/brapi/sync_quote/<string:name>')
@cross_origin()
@require_api_key
@cache.cached(timeout=300)
def brapi_sync_quote_show(name):
result = brapi.Brapi.get_sync_stock_data(name)
if result:
Expand All @@ -90,6 +111,7 @@ def brapi_sync_quote_show(name):
@api.route('/brapi/sync_quote_list')
@cross_origin()
@require_api_key
@cache.cached(timeout=300)
def brapi_sync_quote_list_show():
result = brapi.Brapi.get_sync_stock_data_list()
if result:
Expand All @@ -99,6 +121,7 @@ def brapi_sync_quote_list_show():
@api.route('/brapi/sync_quote_by_sector/<string:sector>')
@cross_origin()
@require_api_key
@cache.cached(timeout=300)
def brapi_sync_quote_by_sector_show(sector):
result = brapi.Brapi.get_sync_stock_data_list_by_sector(sector)
if result:
Expand All @@ -109,6 +132,7 @@ def brapi_sync_quote_by_sector_show(sector):
@api.route('/brapi/sync_quote_list_sectors')
@cross_origin()
@require_api_key
@cache.cached(timeout=300)
def brapi_sync_quote_list_sectors_show():
result = brapi.Brapi.get_available_sectors_list()
if result:
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,5 @@ zipp==3.7.0
zope.event==5.0
zope.interface==6.4.post2
brapi==1.0.0
Flask-Caching==2.3.0
redis==5.0.1
Loading