Skip to main content

Python wrapper for fawazahmed0's exchange-api.

Project description

exratepy

A Python library for fetching currency exchange rates and currency information from the Fawazahmed0 Currency API. It provides a simple and convenient way to access up-to-date exchange rate data, with a fallback mechanism to ensure reliability.

:octocat: Github repo

Installation

pip install exratepy

Usage:

from exratepy.client import Client
from datetime import date

client = Client()

# Get available currencies
currencies = client.get_currencies()
print(currencies)

# Get latest exchange rates:
rates = client.get_exchange_rates(base="INR") # date defaults to latest
print(rates)
# Example: {"USD": 86.7248, "AED": 22.6574, ...}

# Get exchange rate as on a specific date:
rates = client.get_exchange_rates(base="INR", date=date(2024,12,1))
print(rates)
# Example: {'INR': 89.7248, 'USD': 1.08275, ...}

Features

  • Currency Information: Fetch a list of available currencies and their names.
  • Exchange Rates: Retrieve exchange rates for a specific base currency and date (or "latest").
  • Fallback Mechanism: Uses a secondary API endpoint as a fallback in case the primary endpoint is unavailable, ensuring higher reliability.
  • Data Validation: Uses Pydantic models to validate and normalize the API responses, ensuring data consistency. Currency codes are converted to uppercase, currency names to title case, and inverse exchange rates are calculated and rounded to 6 decimal places.
  • Error Handling: Includes robust error handling for API requests and invalid data. Custom exceptions (ApiResponseError) provide informative error messages.
  • Logging: Integrates with the loguru library for detailed logging of API requests and any issues encountered.
  • Type Hinting: Comprehensive type hinting enhances code readability and maintainability.

API Endpoints

The library interacts with the following API endpoints:

Primary: https://cdn.jsdelivr.net/npm/@fawazahmed0/currency-api@<date>/v1/currencies/<base>.min.json

Fallback: https://<date>.currency-api.pages.dev/v1/currencies/<base>.json

Error Handling

The library uses a custom exception ApiResponseError to handle API-related errors. This exception provides information about the failed request, including the URL and error message.

from exratepy.exceptions import ApiResponseError

try:
    client.get_exchange_rates(base="INVALID")
except ApiResponseError as e:
    print(f"An error occurred: {e}")

Logging

The library uses the loguru library for logging. To enable logging, you can configure loguru as needed. By default, errors are logged.

from loguru import logger

# Configure logging (optional)
logger.add("my_log_file.log", rotation="10:00")  # Log to a file

client = Client()
# ... your code ...

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

exratepy-0.1.0.tar.gz (24.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

exratepy-0.1.0-py3-none-any.whl (6.2 kB view details)

Uploaded Python 3

File details

Details for the file exratepy-0.1.0.tar.gz.

File metadata

  • Download URL: exratepy-0.1.0.tar.gz
  • Upload date:
  • Size: 24.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.30

File hashes

Hashes for exratepy-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bed8ba36dd5784573ee63e2ce68f9c8eba86d8180c5aa9fcf4f7884f2891acf7
MD5 1347cdcc04dcb9f75a62e07cea9eb4df
BLAKE2b-256 50fc08eb7c6dda833ec48c3b77f11d929013df1d96f31cf341e99a533a473b54

See more details on using hashes here.

File details

Details for the file exratepy-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: exratepy-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.5.30

File hashes

Hashes for exratepy-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e36691e6f012a780ed7e49e3432a6696270da3d437ced26063a1e9aa18a53c59
MD5 a0ebe5657a755191ae12d3677cbf4715
BLAKE2b-256 f3a130d1729f4da5443ad84ceb5a4199c12cc3eee1520e2ae13a52b9a4dd5636

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page