Initial implementation of cryptocurrency data API service using CoinCap API
* Created FastAPI application structure * Added database models for assets, exchanges, markets, and rates * Integrated with CoinCap API * Implemented REST API endpoints * Setup SQLite persistence with Alembic migrations * Added comprehensive documentation Generated with BackendIM... (backend.im)
This commit is contained in:
parent
d6a7d83e88
commit
3a29a346a9
99
README.md
99
README.md
@ -1,3 +1,98 @@
|
|||||||
# FastAPI Application
|
# Cryptocurrency Data API Service
|
||||||
|
|
||||||
This is a FastAPI application bootstrapped by BackendIM, the AI-powered backend generation platform.
|
A FastAPI backend service that provides cryptocurrency data via CoinCap API. This service acts as a wrapper around the CoinCap API, providing additional features like data caching, persistence, and a clean REST API.
|
||||||
|
|
||||||
|
## Features
|
||||||
|
|
||||||
|
- RESTful API for accessing cryptocurrency data
|
||||||
|
- Proxies CoinCap API to provide cryptocurrency market data
|
||||||
|
- Data models for assets, exchanges, markets, and conversion rates
|
||||||
|
- Persistent storage using SQLite
|
||||||
|
- Database migrations using Alembic
|
||||||
|
- OpenAPI documentation
|
||||||
|
|
||||||
|
## API Endpoints
|
||||||
|
|
||||||
|
The API provides the following endpoints:
|
||||||
|
|
||||||
|
### Health Check
|
||||||
|
- `GET /health` - Check if the service is up and running
|
||||||
|
|
||||||
|
### Assets
|
||||||
|
- `GET /api/v1/assets` - Get a list of assets with optional filtering
|
||||||
|
- `GET /api/v1/assets/{asset_id}` - Get details for a specific asset
|
||||||
|
- `GET /api/v1/assets/{asset_id}/markets` - Get markets for a specific asset
|
||||||
|
- `GET /api/v1/assets/{asset_id}/history` - Get historical data for a specific asset
|
||||||
|
|
||||||
|
### Exchanges
|
||||||
|
- `GET /api/v1/exchanges` - Get a list of exchanges
|
||||||
|
- `GET /api/v1/exchanges/{exchange_id}` - Get details for a specific exchange
|
||||||
|
|
||||||
|
### Markets
|
||||||
|
- `GET /api/v1/markets` - Get a list of markets with optional filtering
|
||||||
|
|
||||||
|
### Rates
|
||||||
|
- `GET /api/v1/rates` - Get conversion rates
|
||||||
|
- `GET /api/v1/rates/{rate_id}` - Get a specific conversion rate
|
||||||
|
|
||||||
|
## Prerequisites
|
||||||
|
|
||||||
|
- Python 3.8 or higher
|
||||||
|
- SQLite
|
||||||
|
|
||||||
|
## Installation
|
||||||
|
|
||||||
|
1. Clone the repository:
|
||||||
|
```
|
||||||
|
git clone <repository-url>
|
||||||
|
cd cryptocurrency-data-api-service
|
||||||
|
```
|
||||||
|
|
||||||
|
2. Create a virtual environment and activate it:
|
||||||
|
```
|
||||||
|
python -m venv venv
|
||||||
|
source venv/bin/activate # On Windows: venv\Scripts\activate
|
||||||
|
```
|
||||||
|
|
||||||
|
3. Install dependencies:
|
||||||
|
```
|
||||||
|
pip install -r requirements.txt
|
||||||
|
```
|
||||||
|
|
||||||
|
4. Apply migrations:
|
||||||
|
```
|
||||||
|
alembic upgrade head
|
||||||
|
```
|
||||||
|
|
||||||
|
## Usage
|
||||||
|
|
||||||
|
Start the server:
|
||||||
|
|
||||||
|
```
|
||||||
|
uvicorn main:app --reload
|
||||||
|
```
|
||||||
|
|
||||||
|
The API will be available at http://localhost:8000
|
||||||
|
|
||||||
|
### API Documentation
|
||||||
|
|
||||||
|
After starting the server, you can access the interactive API documentation:
|
||||||
|
|
||||||
|
- Swagger UI: http://localhost:8000/docs
|
||||||
|
- ReDoc: http://localhost:8000/redoc
|
||||||
|
|
||||||
|
## Environment Variables
|
||||||
|
|
||||||
|
The following environment variables can be configured:
|
||||||
|
|
||||||
|
- `COINCAP_API_KEY` - Your CoinCap API key (default is provided for development)
|
||||||
|
- `DB_PATH` - Path to SQLite database file (default: `/app/storage/db/db.sqlite`)
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
[MIT License](LICENSE)
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
- [FastAPI](https://fastapi.tiangolo.com/) - Web framework for building APIs
|
||||||
|
- [CoinCap API](https://docs.coincap.io/) - Cryptocurrency data provider
|
117
alembic.ini
Normal file
117
alembic.ini
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
# A generic, single database configuration.
|
||||||
|
|
||||||
|
[alembic]
|
||||||
|
# path to migration scripts
|
||||||
|
script_location = alembic
|
||||||
|
|
||||||
|
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
|
||||||
|
# Uncomment the line below if you want the files to be prepended with date and time
|
||||||
|
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
|
||||||
|
# for all available tokens
|
||||||
|
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
|
||||||
|
|
||||||
|
# sys.path path, will be prepended to sys.path if present.
|
||||||
|
# defaults to the current working directory.
|
||||||
|
prepend_sys_path = .
|
||||||
|
|
||||||
|
# timezone to use when rendering the date within the migration file
|
||||||
|
# as well as the filename.
|
||||||
|
# If specified, requires the python-dateutil library that can be
|
||||||
|
# installed by adding `alembic[tz]` to the pip requirements
|
||||||
|
# string value is passed to dateutil.tz.gettz()
|
||||||
|
# leave blank for localtime
|
||||||
|
# timezone =
|
||||||
|
|
||||||
|
# max length of characters to apply to the
|
||||||
|
# "slug" field
|
||||||
|
# truncate_slug_length = 40
|
||||||
|
|
||||||
|
# set to 'true' to run the environment during
|
||||||
|
# the 'revision' command, regardless of autogenerate
|
||||||
|
# revision_environment = false
|
||||||
|
|
||||||
|
# set to 'true' to allow .pyc and .pyo files without
|
||||||
|
# a source .py file to be detected as revisions in the
|
||||||
|
# versions/ directory
|
||||||
|
# sourceless = false
|
||||||
|
|
||||||
|
# version location specification; This defaults
|
||||||
|
# to alembic/versions. When using multiple version
|
||||||
|
# directories, initial revisions must be specified with --version-path.
|
||||||
|
# The path separator used here should be the separator specified by "version_path_separator" below.
|
||||||
|
# version_locations = %(here)s/bar:%(here)s/bat:alembic/versions
|
||||||
|
|
||||||
|
# version path separator; As mentioned above, this is the character used to split
|
||||||
|
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
|
||||||
|
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
|
||||||
|
# Valid values for version_path_separator are:
|
||||||
|
#
|
||||||
|
# version_path_separator = :
|
||||||
|
# version_path_separator = ;
|
||||||
|
# version_path_separator = space
|
||||||
|
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
|
||||||
|
|
||||||
|
# set to 'true' to search source files recursively
|
||||||
|
# in each "version_locations" directory
|
||||||
|
# new in Alembic version 1.10
|
||||||
|
# recursive_version_locations = false
|
||||||
|
|
||||||
|
# the output encoding used when revision files
|
||||||
|
# are written from script.py.mako
|
||||||
|
# output_encoding = utf-8
|
||||||
|
|
||||||
|
# SQLite URL example
|
||||||
|
sqlalchemy.url = sqlite:////app/storage/db/db.sqlite
|
||||||
|
|
||||||
|
|
||||||
|
[post_write_hooks]
|
||||||
|
# post_write_hooks defines scripts or Python functions that are run
|
||||||
|
# on newly generated revision scripts. See the documentation for further
|
||||||
|
# detail and examples
|
||||||
|
|
||||||
|
# format using "black" - use the console_scripts runner, against the "black" entrypoint
|
||||||
|
# hooks = black
|
||||||
|
# black.type = console_scripts
|
||||||
|
# black.entrypoint = black
|
||||||
|
# black.options = -l 79 REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
|
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
|
||||||
|
# hooks = ruff
|
||||||
|
# ruff.type = exec
|
||||||
|
# ruff.executable = %(here)s/.venv/bin/ruff
|
||||||
|
# ruff.options = --fix REVISION_SCRIPT_FILENAME
|
||||||
|
|
||||||
|
# Logging configuration
|
||||||
|
[loggers]
|
||||||
|
keys = root,sqlalchemy,alembic
|
||||||
|
|
||||||
|
[handlers]
|
||||||
|
keys = console
|
||||||
|
|
||||||
|
[formatters]
|
||||||
|
keys = generic
|
||||||
|
|
||||||
|
[logger_root]
|
||||||
|
level = WARN
|
||||||
|
handlers = console
|
||||||
|
qualname =
|
||||||
|
|
||||||
|
[logger_sqlalchemy]
|
||||||
|
level = WARN
|
||||||
|
handlers =
|
||||||
|
qualname = sqlalchemy.engine
|
||||||
|
|
||||||
|
[logger_alembic]
|
||||||
|
level = INFO
|
||||||
|
handlers =
|
||||||
|
qualname = alembic
|
||||||
|
|
||||||
|
[handler_console]
|
||||||
|
class = StreamHandler
|
||||||
|
args = (sys.stderr,)
|
||||||
|
level = NOTSET
|
||||||
|
formatter = generic
|
||||||
|
|
||||||
|
[formatter_generic]
|
||||||
|
format = %(levelname)-5.5s [%(name)s] %(message)s
|
||||||
|
datefmt = %H:%M:%S
|
79
alembic/env.py
Normal file
79
alembic/env.py
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
from logging.config import fileConfig
|
||||||
|
|
||||||
|
from sqlalchemy import engine_from_config
|
||||||
|
from sqlalchemy import pool
|
||||||
|
|
||||||
|
from alembic import context
|
||||||
|
|
||||||
|
# This is the Alembic Config object, which provides
|
||||||
|
# access to the values within the .ini file in use.
|
||||||
|
config = context.config
|
||||||
|
|
||||||
|
# Interpret the config file for Python logging.
|
||||||
|
# This line sets up loggers basically.
|
||||||
|
if config.config_file_name is not None:
|
||||||
|
fileConfig(config.config_file_name)
|
||||||
|
|
||||||
|
# Import all models here
|
||||||
|
from app.models import Asset, AssetPriceHistory, Exchange, Market, Rate
|
||||||
|
from app.core.database import Base
|
||||||
|
|
||||||
|
# Target metadata
|
||||||
|
target_metadata = Base.metadata
|
||||||
|
|
||||||
|
# Other values from the config, defined by the needs of env.py,
|
||||||
|
# can be acquired:
|
||||||
|
# my_important_option = config.get_main_option("my_important_option")
|
||||||
|
# ... etc.
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_offline() -> None:
|
||||||
|
"""Run migrations in 'offline' mode.
|
||||||
|
|
||||||
|
This configures the context with just a URL
|
||||||
|
and not an Engine, though an Engine is acceptable
|
||||||
|
here as well. By skipping the Engine creation
|
||||||
|
we don't even need a DBAPI to be available.
|
||||||
|
|
||||||
|
Calls to context.execute() here emit the given string to the
|
||||||
|
script output.
|
||||||
|
|
||||||
|
"""
|
||||||
|
url = config.get_main_option("sqlalchemy.url")
|
||||||
|
context.configure(
|
||||||
|
url=url,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
literal_binds=True,
|
||||||
|
dialect_opts={"paramstyle": "named"},
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
def run_migrations_online() -> None:
|
||||||
|
"""Run migrations in 'online' mode.
|
||||||
|
|
||||||
|
In this scenario we need to create an Engine
|
||||||
|
and associate a connection with the context.
|
||||||
|
|
||||||
|
"""
|
||||||
|
connectable = engine_from_config(
|
||||||
|
config.get_section(config.config_ini_section, {}),
|
||||||
|
prefix="sqlalchemy.",
|
||||||
|
poolclass=pool.NullPool,
|
||||||
|
)
|
||||||
|
|
||||||
|
with connectable.connect() as connection:
|
||||||
|
context.configure(
|
||||||
|
connection=connection, target_metadata=target_metadata
|
||||||
|
)
|
||||||
|
|
||||||
|
with context.begin_transaction():
|
||||||
|
context.run_migrations()
|
||||||
|
|
||||||
|
|
||||||
|
if context.is_offline_mode():
|
||||||
|
run_migrations_offline()
|
||||||
|
else:
|
||||||
|
run_migrations_online()
|
26
alembic/script.py.mako
Normal file
26
alembic/script.py.mako
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
"""${message}
|
||||||
|
|
||||||
|
Revision ID: ${up_revision}
|
||||||
|
Revises: ${down_revision | comma,n}
|
||||||
|
Create Date: ${create_date}
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
${imports if imports else ""}
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = ${repr(up_revision)}
|
||||||
|
down_revision: Union[str, None] = ${repr(down_revision)}
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
|
||||||
|
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
${upgrades if upgrades else "pass"}
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
${downgrades if downgrades else "pass"}
|
140
alembic/versions/01_initial_migration.py
Normal file
140
alembic/versions/01_initial_migration.py
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
"""Initial migration
|
||||||
|
|
||||||
|
Revision ID: 01_initial_migration
|
||||||
|
Revises:
|
||||||
|
Create Date: 2025-05-14
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = '01_initial_migration'
|
||||||
|
down_revision: Union[str, None] = None
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
# Create Asset table
|
||||||
|
op.create_table(
|
||||||
|
'assets',
|
||||||
|
sa.Column('id', sa.String(), nullable=False),
|
||||||
|
sa.Column('rank', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('symbol', sa.String(), nullable=True),
|
||||||
|
sa.Column('name', sa.String(), nullable=True),
|
||||||
|
sa.Column('supply', sa.Float(), nullable=True),
|
||||||
|
sa.Column('max_supply', sa.Float(), nullable=True),
|
||||||
|
sa.Column('market_cap_usd', sa.Float(), nullable=True),
|
||||||
|
sa.Column('volume_usd_24hr', sa.Float(), nullable=True),
|
||||||
|
sa.Column('price_usd', sa.Float(), nullable=True),
|
||||||
|
sa.Column('change_percent_24hr', sa.Float(), nullable=True),
|
||||||
|
sa.Column('vwap_24hr', sa.Float(), nullable=True),
|
||||||
|
sa.Column('explorer', sa.String(), nullable=True),
|
||||||
|
sa.Column('last_updated', sa.DateTime(), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_assets_id'), 'assets', ['id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_assets_name'), 'assets', ['name'], unique=False)
|
||||||
|
op.create_index(op.f('ix_assets_symbol'), 'assets', ['symbol'], unique=False)
|
||||||
|
|
||||||
|
# Create AssetPriceHistory table
|
||||||
|
op.create_table(
|
||||||
|
'asset_price_history',
|
||||||
|
sa.Column('id', sa.Integer(), nullable=False),
|
||||||
|
sa.Column('asset_id', sa.String(), nullable=True),
|
||||||
|
sa.Column('price_usd', sa.Float(), nullable=True),
|
||||||
|
sa.Column('timestamp', sa.DateTime(), nullable=True),
|
||||||
|
sa.ForeignKeyConstraint(['asset_id'], ['assets.id'], ondelete='CASCADE'),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_asset_price_history_asset_id'), 'asset_price_history', ['asset_id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_asset_price_history_id'), 'asset_price_history', ['id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_asset_price_history_timestamp'), 'asset_price_history', ['timestamp'], unique=False)
|
||||||
|
|
||||||
|
# Create Exchange table
|
||||||
|
op.create_table(
|
||||||
|
'exchanges',
|
||||||
|
sa.Column('exchange_id', sa.String(), nullable=False),
|
||||||
|
sa.Column('name', sa.String(), nullable=True),
|
||||||
|
sa.Column('rank', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('percent_total_volume', sa.Float(), nullable=True),
|
||||||
|
sa.Column('volume_usd', sa.Float(), nullable=True),
|
||||||
|
sa.Column('trading_pairs', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('socket', sa.Boolean(), nullable=True),
|
||||||
|
sa.Column('exchange_url', sa.String(), nullable=True),
|
||||||
|
sa.Column('updated_timestamp', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('last_updated', sa.DateTime(), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('exchange_id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_exchanges_exchange_id'), 'exchanges', ['exchange_id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_exchanges_name'), 'exchanges', ['name'], unique=False)
|
||||||
|
|
||||||
|
# Create Market table
|
||||||
|
op.create_table(
|
||||||
|
'markets',
|
||||||
|
sa.Column('id', sa.Integer(), autoincrement=True, nullable=False),
|
||||||
|
sa.Column('exchange_id', sa.String(), nullable=True),
|
||||||
|
sa.Column('rank', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('base_symbol', sa.String(), nullable=True),
|
||||||
|
sa.Column('base_id', sa.String(), nullable=True),
|
||||||
|
sa.Column('quote_symbol', sa.String(), nullable=True),
|
||||||
|
sa.Column('quote_id', sa.String(), nullable=True),
|
||||||
|
sa.Column('price_quote', sa.Float(), nullable=True),
|
||||||
|
sa.Column('price_usd', sa.Float(), nullable=True),
|
||||||
|
sa.Column('volume_usd_24hr', sa.Float(), nullable=True),
|
||||||
|
sa.Column('percent_exchange_volume', sa.Float(), nullable=True),
|
||||||
|
sa.Column('trades_count_24hr', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('updated_timestamp', sa.Integer(), nullable=True),
|
||||||
|
sa.Column('last_updated', sa.DateTime(), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_markets_base_id'), 'markets', ['base_id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_markets_base_symbol'), 'markets', ['base_symbol'], unique=False)
|
||||||
|
op.create_index(op.f('ix_markets_exchange_id'), 'markets', ['exchange_id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_markets_quote_id'), 'markets', ['quote_id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_markets_quote_symbol'), 'markets', ['quote_symbol'], unique=False)
|
||||||
|
|
||||||
|
# Create Rate table
|
||||||
|
op.create_table(
|
||||||
|
'rates',
|
||||||
|
sa.Column('id', sa.String(), nullable=False),
|
||||||
|
sa.Column('symbol', sa.String(), nullable=True),
|
||||||
|
sa.Column('currency_symbol', sa.String(), nullable=True),
|
||||||
|
sa.Column('type', sa.String(), nullable=True),
|
||||||
|
sa.Column('rate_usd', sa.Float(), nullable=True),
|
||||||
|
sa.Column('last_updated', sa.DateTime(), nullable=True),
|
||||||
|
sa.PrimaryKeyConstraint('id')
|
||||||
|
)
|
||||||
|
op.create_index(op.f('ix_rates_id'), 'rates', ['id'], unique=False)
|
||||||
|
op.create_index(op.f('ix_rates_symbol'), 'rates', ['symbol'], unique=False)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
op.drop_index(op.f('ix_rates_symbol'), table_name='rates')
|
||||||
|
op.drop_index(op.f('ix_rates_id'), table_name='rates')
|
||||||
|
op.drop_table('rates')
|
||||||
|
|
||||||
|
op.drop_index(op.f('ix_markets_quote_symbol'), table_name='markets')
|
||||||
|
op.drop_index(op.f('ix_markets_quote_id'), table_name='markets')
|
||||||
|
op.drop_index(op.f('ix_markets_exchange_id'), table_name='markets')
|
||||||
|
op.drop_index(op.f('ix_markets_base_symbol'), table_name='markets')
|
||||||
|
op.drop_index(op.f('ix_markets_base_id'), table_name='markets')
|
||||||
|
op.drop_table('markets')
|
||||||
|
|
||||||
|
op.drop_index(op.f('ix_exchanges_name'), table_name='exchanges')
|
||||||
|
op.drop_index(op.f('ix_exchanges_exchange_id'), table_name='exchanges')
|
||||||
|
op.drop_table('exchanges')
|
||||||
|
|
||||||
|
op.drop_index(op.f('ix_asset_price_history_timestamp'), table_name='asset_price_history')
|
||||||
|
op.drop_index(op.f('ix_asset_price_history_id'), table_name='asset_price_history')
|
||||||
|
op.drop_index(op.f('ix_asset_price_history_asset_id'), table_name='asset_price_history')
|
||||||
|
op.drop_table('asset_price_history')
|
||||||
|
|
||||||
|
op.drop_index(op.f('ix_assets_symbol'), table_name='assets')
|
||||||
|
op.drop_index(op.f('ix_assets_name'), table_name='assets')
|
||||||
|
op.drop_index(op.f('ix_assets_id'), table_name='assets')
|
||||||
|
op.drop_table('assets')
|
14
app/api/routes/__init__.py
Normal file
14
app/api/routes/__init__.py
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
from fastapi import APIRouter
|
||||||
|
from app.core.config import settings
|
||||||
|
from app.api.routes import assets, exchanges, markets, rates, health
|
||||||
|
|
||||||
|
api_router = APIRouter()
|
||||||
|
|
||||||
|
# Include the health check endpoint
|
||||||
|
api_router.include_router(health.router, tags=["Health"])
|
||||||
|
|
||||||
|
# Include all the API routes
|
||||||
|
api_router.include_router(assets.router, prefix=f"{settings.API_V1_STR}/assets", tags=["Assets"])
|
||||||
|
api_router.include_router(exchanges.router, prefix=f"{settings.API_V1_STR}/exchanges", tags=["Exchanges"])
|
||||||
|
api_router.include_router(markets.router, prefix=f"{settings.API_V1_STR}/markets", tags=["Markets"])
|
||||||
|
api_router.include_router(rates.router, prefix=f"{settings.API_V1_STR}/rates", tags=["Rates"])
|
117
app/api/routes/assets.py
Normal file
117
app/api/routes/assets.py
Normal file
@ -0,0 +1,117 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query, Path, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import Optional, List
|
||||||
|
import time
|
||||||
|
|
||||||
|
from app.services.coincap_api import coincap_api
|
||||||
|
from app.core.database import get_db
|
||||||
|
from app.api.schemas.asset import (
|
||||||
|
Asset, AssetCreate,
|
||||||
|
AssetResponse, AssetsResponse,
|
||||||
|
AssetHistoryResponse
|
||||||
|
)
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_model=AssetsResponse)
|
||||||
|
async def get_assets(
|
||||||
|
search: Optional[str] = None,
|
||||||
|
ids: Optional[str] = None,
|
||||||
|
limit: Optional[int] = Query(100, le=2000),
|
||||||
|
offset: Optional[int] = Query(0, ge=0),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get a list of assets with optional filters
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_assets(search, ids, limit, offset)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch assets: {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{asset_id}", response_model=AssetResponse)
|
||||||
|
async def get_asset(
|
||||||
|
asset_id: str = Path(..., description="The asset ID (slug) to retrieve"),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get details for a specific asset by ID
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_asset(asset_id)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch asset {asset_id}: {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{asset_id}/markets", response_model=dict)
|
||||||
|
async def get_asset_markets(
|
||||||
|
asset_id: str = Path(..., description="The asset ID (slug) to retrieve markets for"),
|
||||||
|
limit: Optional[int] = Query(100, le=2000),
|
||||||
|
offset: Optional[int] = Query(0, ge=0),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get markets for a specific asset
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_asset_markets(asset_id, limit, offset)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch markets for asset {asset_id}: {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{asset_id}/history", response_model=AssetHistoryResponse)
|
||||||
|
async def get_asset_history(
|
||||||
|
asset_id: str = Path(..., description="The asset ID (slug) to retrieve history for"),
|
||||||
|
interval: str = Query(..., description="Interval choices: m1, m5, m15, m30, h1, h2, h6, h12, d1"),
|
||||||
|
start: Optional[int] = Query(None, description="UNIX time in milliseconds"),
|
||||||
|
end: Optional[int] = Query(None, description="UNIX time in milliseconds"),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get historical data for a specific asset
|
||||||
|
"""
|
||||||
|
# Validate interval choices
|
||||||
|
valid_intervals = ["m1", "m5", "m15", "m30", "h1", "h2", "h6", "h12", "d1"]
|
||||||
|
if interval not in valid_intervals:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail=f"Invalid interval. Choose from: {', '.join(valid_intervals)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
# Both start and end must be provided if one is provided
|
||||||
|
if (start is None and end is not None) or (start is not None and end is None):
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_400_BAD_REQUEST,
|
||||||
|
detail="Both 'start' and 'end' parameters must be provided together"
|
||||||
|
)
|
||||||
|
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_asset_history(asset_id, interval, start, end)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch history for asset {asset_id}: {str(e)}"
|
||||||
|
)
|
50
app/api/routes/exchanges.py
Normal file
50
app/api/routes/exchanges.py
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query, Path, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from app.services.coincap_api import coincap_api
|
||||||
|
from app.core.database import get_db
|
||||||
|
from app.api.schemas.exchange import ExchangesResponse, ExchangeResponse
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_model=ExchangesResponse)
|
||||||
|
async def get_exchanges(
|
||||||
|
limit: Optional[int] = Query(10, le=2000),
|
||||||
|
offset: Optional[int] = Query(0, ge=0),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get a list of exchanges with optional pagination
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_exchanges(limit, offset)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch exchanges: {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{exchange_id}", response_model=ExchangeResponse)
|
||||||
|
async def get_exchange(
|
||||||
|
exchange_id: str = Path(..., description="The exchange ID to retrieve"),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get details for a specific exchange
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_exchange(exchange_id)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch exchange {exchange_id}: {str(e)}"
|
||||||
|
)
|
29
app/api/routes/health.py
Normal file
29
app/api/routes/health.py
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import Dict
|
||||||
|
import time
|
||||||
|
|
||||||
|
from app.core.database import get_db
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/health", status_code=status.HTTP_200_OK)
|
||||||
|
def health_check(db: Session = Depends(get_db)) -> Dict[str, object]:
|
||||||
|
"""
|
||||||
|
Health check endpoint to verify API is running and database connection works
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
# Check database connection
|
||||||
|
db.execute("SELECT 1")
|
||||||
|
return {
|
||||||
|
"status": "ok",
|
||||||
|
"timestamp": int(time.time() * 1000),
|
||||||
|
"message": "Service is healthy",
|
||||||
|
"database": "connected"
|
||||||
|
}
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_503_SERVICE_UNAVAILABLE,
|
||||||
|
detail=f"Database connection error: {str(e)}"
|
||||||
|
)
|
47
app/api/routes/markets.py
Normal file
47
app/api/routes/markets.py
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from app.services.coincap_api import coincap_api
|
||||||
|
from app.core.database import get_db
|
||||||
|
from app.api.schemas.market import MarketsResponse
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_model=MarketsResponse)
|
||||||
|
async def get_markets(
|
||||||
|
exchange_id: Optional[str] = Query(None, description="Filter by exchange ID"),
|
||||||
|
base_symbol: Optional[str] = Query(None, description="Filter by base asset symbol"),
|
||||||
|
base_id: Optional[str] = Query(None, description="Filter by base asset ID"),
|
||||||
|
quote_symbol: Optional[str] = Query(None, description="Filter by quote asset symbol"),
|
||||||
|
quote_id: Optional[str] = Query(None, description="Filter by quote asset ID"),
|
||||||
|
asset_symbol: Optional[str] = Query(None, description="Filter by asset symbol (matches base or quote)"),
|
||||||
|
asset_id: Optional[str] = Query(None, description="Filter by asset ID (matches base or quote)"),
|
||||||
|
limit: Optional[int] = Query(10, le=2000),
|
||||||
|
offset: Optional[int] = Query(0, ge=0),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get a list of markets with optional filters and pagination
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_markets(
|
||||||
|
exchange_id,
|
||||||
|
base_symbol,
|
||||||
|
base_id,
|
||||||
|
quote_symbol,
|
||||||
|
quote_id,
|
||||||
|
asset_symbol,
|
||||||
|
asset_id,
|
||||||
|
limit,
|
||||||
|
offset
|
||||||
|
)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch markets: {str(e)}"
|
||||||
|
)
|
49
app/api/routes/rates.py
Normal file
49
app/api/routes/rates.py
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
from fastapi import APIRouter, Depends, HTTPException, Query, Path, status
|
||||||
|
from sqlalchemy.orm import Session
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
from app.services.coincap_api import coincap_api
|
||||||
|
from app.core.database import get_db
|
||||||
|
from app.api.schemas.rate import RatesResponse, RateResponse
|
||||||
|
|
||||||
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("", response_model=RatesResponse)
|
||||||
|
async def get_rates(
|
||||||
|
ids: Optional[str] = Query(None, description="Comma-separated list of slugs to filter by (e.g. `bitcoin,ethereum`)"),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get a list of conversion rates with optional filtering
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_rates(ids)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch rates: {str(e)}"
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get("/{rate_id}", response_model=RateResponse)
|
||||||
|
async def get_rate(
|
||||||
|
rate_id: str = Path(..., description="The rate ID (slug) to retrieve"),
|
||||||
|
db: Session = Depends(get_db)
|
||||||
|
):
|
||||||
|
"""
|
||||||
|
Get a specific conversion rate by ID
|
||||||
|
"""
|
||||||
|
try:
|
||||||
|
response = await coincap_api.get_rate(rate_id)
|
||||||
|
return response
|
||||||
|
except HTTPException as e:
|
||||||
|
raise e
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,
|
||||||
|
detail=f"Failed to fetch rate {rate_id}: {str(e)}"
|
||||||
|
)
|
0
app/api/schemas/__init__.py
Normal file
0
app/api/schemas/__init__.py
Normal file
64
app/api/schemas/asset.py
Normal file
64
app/api/schemas/asset.py
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import List, Optional
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
class AssetBase(BaseModel):
|
||||||
|
id: str
|
||||||
|
rank: str
|
||||||
|
symbol: str
|
||||||
|
name: str
|
||||||
|
supply: str
|
||||||
|
max_supply: Optional[str] = None
|
||||||
|
market_cap_usd: str
|
||||||
|
volume_usd_24hr: str
|
||||||
|
price_usd: str
|
||||||
|
change_percent_24hr: Optional[str] = None
|
||||||
|
vwap_24hr: Optional[str] = None
|
||||||
|
explorer: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
|
class Asset(AssetBase):
|
||||||
|
"""Data model for retrieving assets"""
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
||||||
|
|
||||||
|
|
||||||
|
class AssetCreate(AssetBase):
|
||||||
|
"""Data model for creating assets"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class AssetPriceHistoryBase(BaseModel):
|
||||||
|
price_usd: str
|
||||||
|
time: int
|
||||||
|
date: str
|
||||||
|
|
||||||
|
|
||||||
|
class AssetPriceHistory(AssetPriceHistoryBase):
|
||||||
|
"""Data model for retrieving asset price history"""
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
||||||
|
|
||||||
|
|
||||||
|
class AssetPriceHistoryCreate(AssetPriceHistoryBase):
|
||||||
|
"""Data model for creating asset price history"""
|
||||||
|
asset_id: str
|
||||||
|
|
||||||
|
|
||||||
|
class AssetResponse(BaseModel):
|
||||||
|
"""Response model for asset endpoints"""
|
||||||
|
timestamp: int
|
||||||
|
data: Asset
|
||||||
|
|
||||||
|
|
||||||
|
class AssetsResponse(BaseModel):
|
||||||
|
"""Response model for multiple assets"""
|
||||||
|
timestamp: int
|
||||||
|
data: List[Asset]
|
||||||
|
|
||||||
|
|
||||||
|
class AssetHistoryResponse(BaseModel):
|
||||||
|
"""Response model for asset history endpoint"""
|
||||||
|
timestamp: int
|
||||||
|
data: List[AssetPriceHistory]
|
37
app/api/schemas/exchange.py
Normal file
37
app/api/schemas/exchange.py
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class ExchangeBase(BaseModel):
|
||||||
|
exchangeId: str
|
||||||
|
name: str
|
||||||
|
rank: str
|
||||||
|
percentTotalVolume: Optional[str] = None
|
||||||
|
volumeUsd: Optional[str] = None
|
||||||
|
tradingPairs: str
|
||||||
|
socket: Optional[bool] = None
|
||||||
|
exchangeUrl: Optional[str] = None
|
||||||
|
updated: Optional[int] = None
|
||||||
|
|
||||||
|
|
||||||
|
class Exchange(ExchangeBase):
|
||||||
|
"""Data model for retrieving exchanges"""
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
||||||
|
|
||||||
|
|
||||||
|
class ExchangeCreate(ExchangeBase):
|
||||||
|
"""Data model for creating exchanges"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ExchangeResponse(BaseModel):
|
||||||
|
"""Response model for exchange endpoints"""
|
||||||
|
timestamp: int
|
||||||
|
data: Exchange
|
||||||
|
|
||||||
|
|
||||||
|
class ExchangesResponse(BaseModel):
|
||||||
|
"""Response model for multiple exchanges"""
|
||||||
|
timestamp: int
|
||||||
|
data: List[Exchange]
|
40
app/api/schemas/market.py
Normal file
40
app/api/schemas/market.py
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class MarketBase(BaseModel):
|
||||||
|
exchangeId: str
|
||||||
|
rank: Optional[str] = None
|
||||||
|
baseSymbol: str
|
||||||
|
baseId: str
|
||||||
|
quoteSymbol: str
|
||||||
|
quoteId: str
|
||||||
|
priceQuote: Optional[str] = None
|
||||||
|
priceUsd: Optional[str] = None
|
||||||
|
volumeUsd24Hr: Optional[str] = None
|
||||||
|
percentExchangeVolume: Optional[str] = None
|
||||||
|
tradesCount24Hr: Optional[str] = None
|
||||||
|
updated: Optional[int] = None
|
||||||
|
|
||||||
|
|
||||||
|
class Market(MarketBase):
|
||||||
|
"""Data model for retrieving markets"""
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
||||||
|
|
||||||
|
|
||||||
|
class MarketCreate(MarketBase):
|
||||||
|
"""Data model for creating markets"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class MarketResponse(BaseModel):
|
||||||
|
"""Response model for market endpoints"""
|
||||||
|
timestamp: int
|
||||||
|
data: Market
|
||||||
|
|
||||||
|
|
||||||
|
class MarketsResponse(BaseModel):
|
||||||
|
"""Response model for multiple markets"""
|
||||||
|
timestamp: int
|
||||||
|
data: List[Market]
|
33
app/api/schemas/rate.py
Normal file
33
app/api/schemas/rate.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
from pydantic import BaseModel, Field
|
||||||
|
from typing import List, Optional
|
||||||
|
|
||||||
|
|
||||||
|
class RateBase(BaseModel):
|
||||||
|
id: str
|
||||||
|
symbol: str
|
||||||
|
currencySymbol: Optional[str] = None
|
||||||
|
type: str
|
||||||
|
rateUsd: str
|
||||||
|
|
||||||
|
|
||||||
|
class Rate(RateBase):
|
||||||
|
"""Data model for retrieving rates"""
|
||||||
|
class Config:
|
||||||
|
from_attributes = True
|
||||||
|
|
||||||
|
|
||||||
|
class RateCreate(RateBase):
|
||||||
|
"""Data model for creating rates"""
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class RateResponse(BaseModel):
|
||||||
|
"""Response model for rate endpoints"""
|
||||||
|
timestamp: int
|
||||||
|
data: Rate
|
||||||
|
|
||||||
|
|
||||||
|
class RatesResponse(BaseModel):
|
||||||
|
"""Response model for multiple rates"""
|
||||||
|
timestamp: int
|
||||||
|
data: List[Rate]
|
30
app/core/config.py
Normal file
30
app/core/config.py
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
from pathlib import Path
|
||||||
|
from pydantic_settings import BaseSettings
|
||||||
|
|
||||||
|
class Settings(BaseSettings):
|
||||||
|
"""
|
||||||
|
Application settings loaded from environment variables
|
||||||
|
"""
|
||||||
|
API_V1_STR: str = "/api/v1"
|
||||||
|
PROJECT_NAME: str = "CryptoCurrency Data API Service"
|
||||||
|
|
||||||
|
# CoinCap API Settings
|
||||||
|
COINCAP_API_URL: str = "https://api.coincap.io/v3"
|
||||||
|
COINCAP_API_KEY: str = "b1afdeb4223dd63e8e26977c3539432eeaca0a7603c4655a7c9c57dadb40e7fe"
|
||||||
|
|
||||||
|
# Database Settings
|
||||||
|
DB_DIR: Path = Path("/app") / "storage" / "db"
|
||||||
|
SQLALCHEMY_DATABASE_URL: str = f"sqlite:///{DB_DIR}/db.sqlite"
|
||||||
|
|
||||||
|
# Redis Cache (optional)
|
||||||
|
# REDIS_URL: str = "redis://localhost:6379/0"
|
||||||
|
# CACHE_EXPIRATION: int = 300 # 5 minutes in seconds
|
||||||
|
|
||||||
|
class Config:
|
||||||
|
case_sensitive = True
|
||||||
|
|
||||||
|
|
||||||
|
settings = Settings()
|
||||||
|
|
||||||
|
# Ensure the database directory exists
|
||||||
|
settings.DB_DIR.mkdir(parents=True, exist_ok=True)
|
22
app/core/database.py
Normal file
22
app/core/database.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.ext.declarative import declarative_base
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
|
from app.core.config import settings
|
||||||
|
|
||||||
|
engine = create_engine(
|
||||||
|
settings.SQLALCHEMY_DATABASE_URL,
|
||||||
|
connect_args={"check_same_thread": False}
|
||||||
|
)
|
||||||
|
|
||||||
|
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)
|
||||||
|
|
||||||
|
Base = declarative_base()
|
||||||
|
|
||||||
|
# Dependency
|
||||||
|
def get_db():
|
||||||
|
db = SessionLocal()
|
||||||
|
try:
|
||||||
|
yield db
|
||||||
|
finally:
|
||||||
|
db.close()
|
4
app/models/__init__.py
Normal file
4
app/models/__init__.py
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
from app.models.asset import Asset, AssetPriceHistory
|
||||||
|
from app.models.exchange import Exchange
|
||||||
|
from app.models.market import Market
|
||||||
|
from app.models.rate import Rate
|
46
app/models/asset.py
Normal file
46
app/models/asset.py
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
from sqlalchemy import Column, String, Float, Integer, DateTime, ForeignKey
|
||||||
|
from sqlalchemy.orm import relationship
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from app.core.database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Asset(Base):
|
||||||
|
__tablename__ = "assets"
|
||||||
|
|
||||||
|
id = Column(String, primary_key=True, index=True) # Slug (e.g., "bitcoin")
|
||||||
|
rank = Column(Integer)
|
||||||
|
symbol = Column(String, index=True)
|
||||||
|
name = Column(String, index=True)
|
||||||
|
supply = Column(Float)
|
||||||
|
max_supply = Column(Float, nullable=True)
|
||||||
|
market_cap_usd = Column(Float)
|
||||||
|
volume_usd_24hr = Column(Float)
|
||||||
|
price_usd = Column(Float)
|
||||||
|
change_percent_24hr = Column(Float, nullable=True)
|
||||||
|
vwap_24hr = Column(Float, nullable=True)
|
||||||
|
explorer = Column(String, nullable=True)
|
||||||
|
|
||||||
|
# Last updated timestamp
|
||||||
|
last_updated = Column(DateTime, default=datetime.utcnow)
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
price_history = relationship("AssetPriceHistory", back_populates="asset", cascade="all, delete-orphan")
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Asset {self.symbol}: {self.name}>"
|
||||||
|
|
||||||
|
|
||||||
|
class AssetPriceHistory(Base):
|
||||||
|
__tablename__ = "asset_price_history"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, index=True)
|
||||||
|
asset_id = Column(String, ForeignKey("assets.id", ondelete="CASCADE"), index=True)
|
||||||
|
price_usd = Column(Float)
|
||||||
|
timestamp = Column(DateTime, index=True)
|
||||||
|
|
||||||
|
# Relationships
|
||||||
|
asset = relationship("Asset", back_populates="price_history")
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<AssetPriceHistory {self.asset_id}: {self.price_usd} at {self.timestamp}>"
|
24
app/models/exchange.py
Normal file
24
app/models/exchange.py
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
from sqlalchemy import Column, String, Float, Integer, Boolean, DateTime
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from app.core.database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Exchange(Base):
|
||||||
|
__tablename__ = "exchanges"
|
||||||
|
|
||||||
|
exchange_id = Column(String, primary_key=True, index=True)
|
||||||
|
name = Column(String, index=True)
|
||||||
|
rank = Column(Integer)
|
||||||
|
percent_total_volume = Column(Float, nullable=True)
|
||||||
|
volume_usd = Column(Float, nullable=True)
|
||||||
|
trading_pairs = Column(Integer)
|
||||||
|
socket = Column(Boolean, nullable=True)
|
||||||
|
exchange_url = Column(String, nullable=True)
|
||||||
|
updated_timestamp = Column(Integer, nullable=True)
|
||||||
|
|
||||||
|
# Last updated in our database
|
||||||
|
last_updated = Column(DateTime, default=datetime.utcnow)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Exchange {self.exchange_id}: {self.name}>"
|
28
app/models/market.py
Normal file
28
app/models/market.py
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
from sqlalchemy import Column, String, Float, Integer, DateTime
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from app.core.database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Market(Base):
|
||||||
|
__tablename__ = "markets"
|
||||||
|
|
||||||
|
id = Column(Integer, primary_key=True, autoincrement=True)
|
||||||
|
exchange_id = Column(String, index=True)
|
||||||
|
rank = Column(Integer, nullable=True)
|
||||||
|
base_symbol = Column(String, index=True)
|
||||||
|
base_id = Column(String, index=True)
|
||||||
|
quote_symbol = Column(String, index=True)
|
||||||
|
quote_id = Column(String, index=True)
|
||||||
|
price_quote = Column(Float, nullable=True)
|
||||||
|
price_usd = Column(Float, nullable=True)
|
||||||
|
volume_usd_24hr = Column(Float, nullable=True)
|
||||||
|
percent_exchange_volume = Column(Float, nullable=True)
|
||||||
|
trades_count_24hr = Column(Integer, nullable=True)
|
||||||
|
updated_timestamp = Column(Integer, nullable=True)
|
||||||
|
|
||||||
|
# Last updated in our database
|
||||||
|
last_updated = Column(DateTime, default=datetime.utcnow)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Market {self.exchange_id}: {self.base_symbol}/{self.quote_symbol}>"
|
20
app/models/rate.py
Normal file
20
app/models/rate.py
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
from sqlalchemy import Column, String, Float, DateTime
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from app.core.database import Base
|
||||||
|
|
||||||
|
|
||||||
|
class Rate(Base):
|
||||||
|
__tablename__ = "rates"
|
||||||
|
|
||||||
|
id = Column(String, primary_key=True, index=True) # e.g., "bitcoin" or "usd"
|
||||||
|
symbol = Column(String, index=True)
|
||||||
|
currency_symbol = Column(String, nullable=True)
|
||||||
|
type = Column(String) # "crypto" or "fiat"
|
||||||
|
rate_usd = Column(Float)
|
||||||
|
|
||||||
|
# Last updated in our database
|
||||||
|
last_updated = Column(DateTime, default=datetime.utcnow)
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return f"<Rate {self.symbol}: {self.rate_usd} USD>"
|
198
app/services/coincap_api.py
Normal file
198
app/services/coincap_api.py
Normal file
@ -0,0 +1,198 @@
|
|||||||
|
import httpx
|
||||||
|
from typing import Dict, List, Optional, Any, Union
|
||||||
|
from datetime import datetime
|
||||||
|
from fastapi import HTTPException
|
||||||
|
|
||||||
|
from app.core.config import settings
|
||||||
|
|
||||||
|
|
||||||
|
class CoinCapAPI:
|
||||||
|
"""
|
||||||
|
Service class for interacting with the CoinCap API
|
||||||
|
"""
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self.base_url = settings.COINCAP_API_URL
|
||||||
|
self.api_key = settings.COINCAP_API_KEY
|
||||||
|
self.headers = {"Authorization": f"Bearer {self.api_key}"}
|
||||||
|
|
||||||
|
async def _make_request(
|
||||||
|
self,
|
||||||
|
endpoint: str,
|
||||||
|
params: Optional[Dict[str, Any]] = None
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Make an HTTP request to the CoinCap API
|
||||||
|
"""
|
||||||
|
url = f"{self.base_url}{endpoint}"
|
||||||
|
|
||||||
|
# Add API key to params
|
||||||
|
if params is None:
|
||||||
|
params = {}
|
||||||
|
|
||||||
|
try:
|
||||||
|
async with httpx.AsyncClient() as client:
|
||||||
|
response = await client.get(
|
||||||
|
url,
|
||||||
|
params=params,
|
||||||
|
headers=self.headers,
|
||||||
|
timeout=10.0
|
||||||
|
)
|
||||||
|
|
||||||
|
if response.status_code == 200:
|
||||||
|
return response.json()
|
||||||
|
elif response.status_code == 404:
|
||||||
|
raise HTTPException(status_code=404, detail="Resource not found")
|
||||||
|
else:
|
||||||
|
raise HTTPException(
|
||||||
|
status_code=response.status_code,
|
||||||
|
detail=f"CoinCap API error: {response.text}"
|
||||||
|
)
|
||||||
|
except httpx.RequestError as e:
|
||||||
|
raise HTTPException(status_code=503, detail=f"Could not connect to CoinCap API: {str(e)}")
|
||||||
|
|
||||||
|
# Assets endpoints
|
||||||
|
async def get_assets(
|
||||||
|
self,
|
||||||
|
search: Optional[str] = None,
|
||||||
|
ids: Optional[str] = None,
|
||||||
|
limit: Optional[int] = 100,
|
||||||
|
offset: Optional[int] = 0
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get a list of assets with optional filters
|
||||||
|
"""
|
||||||
|
params = {
|
||||||
|
"limit": limit,
|
||||||
|
"offset": offset
|
||||||
|
}
|
||||||
|
|
||||||
|
if search:
|
||||||
|
params["search"] = search
|
||||||
|
if ids:
|
||||||
|
params["ids"] = ids
|
||||||
|
|
||||||
|
return await self._make_request("/assets", params)
|
||||||
|
|
||||||
|
async def get_asset(self, asset_id: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get details for a specific asset by ID (slug)
|
||||||
|
"""
|
||||||
|
return await self._make_request(f"/assets/{asset_id}")
|
||||||
|
|
||||||
|
async def get_asset_markets(
|
||||||
|
self,
|
||||||
|
asset_id: str,
|
||||||
|
limit: Optional[int] = 100,
|
||||||
|
offset: Optional[int] = 0
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get markets for a specific asset
|
||||||
|
"""
|
||||||
|
params = {
|
||||||
|
"limit": limit,
|
||||||
|
"offset": offset
|
||||||
|
}
|
||||||
|
|
||||||
|
return await self._make_request(f"/assets/{asset_id}/markets", params)
|
||||||
|
|
||||||
|
async def get_asset_history(
|
||||||
|
self,
|
||||||
|
asset_id: str,
|
||||||
|
interval: str,
|
||||||
|
start: Optional[int] = None,
|
||||||
|
end: Optional[int] = None
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get historical data for a specific asset
|
||||||
|
Interval choices: m1, m5, m15, m30, h1, h2, h6, h12, d1
|
||||||
|
"""
|
||||||
|
params = {
|
||||||
|
"interval": interval
|
||||||
|
}
|
||||||
|
|
||||||
|
if start is not None and end is not None:
|
||||||
|
params["start"] = start
|
||||||
|
params["end"] = end
|
||||||
|
|
||||||
|
return await self._make_request(f"/assets/{asset_id}/history", params)
|
||||||
|
|
||||||
|
# Exchanges endpoints
|
||||||
|
async def get_exchanges(
|
||||||
|
self,
|
||||||
|
limit: Optional[int] = 10,
|
||||||
|
offset: Optional[int] = 0
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get a list of exchanges
|
||||||
|
"""
|
||||||
|
params = {
|
||||||
|
"limit": limit,
|
||||||
|
"offset": offset
|
||||||
|
}
|
||||||
|
|
||||||
|
return await self._make_request("/exchanges", params)
|
||||||
|
|
||||||
|
async def get_exchange(self, exchange_id: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get details for a specific exchange
|
||||||
|
"""
|
||||||
|
return await self._make_request(f"/exchanges/{exchange_id}")
|
||||||
|
|
||||||
|
# Markets endpoints
|
||||||
|
async def get_markets(
|
||||||
|
self,
|
||||||
|
exchange_id: Optional[str] = None,
|
||||||
|
base_symbol: Optional[str] = None,
|
||||||
|
base_id: Optional[str] = None,
|
||||||
|
quote_symbol: Optional[str] = None,
|
||||||
|
quote_id: Optional[str] = None,
|
||||||
|
asset_symbol: Optional[str] = None,
|
||||||
|
asset_id: Optional[str] = None,
|
||||||
|
limit: Optional[int] = 10,
|
||||||
|
offset: Optional[int] = 0
|
||||||
|
) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get a list of markets with optional filters
|
||||||
|
"""
|
||||||
|
params = {
|
||||||
|
"limit": limit,
|
||||||
|
"offset": offset
|
||||||
|
}
|
||||||
|
|
||||||
|
if exchange_id:
|
||||||
|
params["exchangeId"] = exchange_id
|
||||||
|
if base_symbol:
|
||||||
|
params["baseSymbol"] = base_symbol
|
||||||
|
if base_id:
|
||||||
|
params["baseId"] = base_id
|
||||||
|
if quote_symbol:
|
||||||
|
params["quoteSymbol"] = quote_symbol
|
||||||
|
if quote_id:
|
||||||
|
params["quoteId"] = quote_id
|
||||||
|
if asset_symbol:
|
||||||
|
params["assetSymbol"] = asset_symbol
|
||||||
|
if asset_id:
|
||||||
|
params["assetId"] = asset_id
|
||||||
|
|
||||||
|
return await self._make_request("/markets", params)
|
||||||
|
|
||||||
|
# Rates endpoints
|
||||||
|
async def get_rates(self, ids: Optional[str] = None) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get a list of all conversion rates or filter by IDs
|
||||||
|
"""
|
||||||
|
params = {}
|
||||||
|
if ids:
|
||||||
|
params["ids"] = ids
|
||||||
|
|
||||||
|
return await self._make_request("/rates", params)
|
||||||
|
|
||||||
|
async def get_rate(self, rate_id: str) -> Dict[str, Any]:
|
||||||
|
"""
|
||||||
|
Get a specific conversion rate by ID
|
||||||
|
"""
|
||||||
|
return await self._make_request(f"/rates/{rate_id}")
|
||||||
|
|
||||||
|
|
||||||
|
coincap_api = CoinCapAPI()
|
36
main.py
Normal file
36
main.py
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
import uvicorn
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.middleware.cors import CORSMiddleware
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
from app.api.routes import api_router
|
||||||
|
from app.core.config import settings
|
||||||
|
from app.core.database import engine, Base
|
||||||
|
|
||||||
|
# Create database tables
|
||||||
|
Base.metadata.create_all(bind=engine)
|
||||||
|
|
||||||
|
# Initialize FastAPI app
|
||||||
|
app = FastAPI(
|
||||||
|
title=settings.PROJECT_NAME,
|
||||||
|
openapi_url=f"{settings.API_V1_STR}/openapi.json",
|
||||||
|
description="A REST API service for cryptocurrency data powered by CoinCap",
|
||||||
|
version="0.1.0",
|
||||||
|
docs_url="/docs",
|
||||||
|
redoc_url="/redoc",
|
||||||
|
)
|
||||||
|
|
||||||
|
# Configure CORS
|
||||||
|
app.add_middleware(
|
||||||
|
CORSMiddleware,
|
||||||
|
allow_origins=["*"], # Allows all origins for development purposes
|
||||||
|
allow_credentials=True,
|
||||||
|
allow_methods=["*"], # Allows all methods
|
||||||
|
allow_headers=["*"], # Allows all headers
|
||||||
|
)
|
||||||
|
|
||||||
|
# Include API router
|
||||||
|
app.include_router(api_router)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
uvicorn.run("main:app", host="0.0.0.0", port=8000, reload=True)
|
9
requirements.txt
Normal file
9
requirements.txt
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
fastapi>=0.103.1,<0.104.0
|
||||||
|
uvicorn>=0.23.2,<0.24.0
|
||||||
|
pydantic>=2.3.0,<2.4.0
|
||||||
|
pydantic-settings>=2.0.3,<2.1.0
|
||||||
|
sqlalchemy>=2.0.20,<2.1.0
|
||||||
|
alembic>=1.12.0,<1.13.0
|
||||||
|
httpx>=0.24.1,<0.25.0
|
||||||
|
ruff>=0.0.287,<0.1.0
|
||||||
|
python-dotenv>=1.0.0,<1.1.0
|
Loading…
x
Reference in New Issue
Block a user