FastAPI Geolocation
Project description
FastAPI Geolocation
fastapi-geolocation
is a simple middleware package for FastAPI that provides geolocation features by integrating IP-based location lookup. This package is particularly useful for FastAPI applications that need to determine the geographical location of their users.
Features
- Easy integration with FastAPI applications.
- Utilizes the GeoIP class from Django for location lookup.
- Provides an easy-to-use middleware that enriches requests with geolocation data.
Installation
To install fastapi-geolocation
, run the following command:
pip install fastapi-geolocation
Quick Start
Here is a simple example of how to use fastapi-geolocation in a FastAPI application:
import aioredis
from fastapi import FastAPI, Request
from fastapi_geolocation import GeoIPMiddleware
app = FastAPI()
redis_client = aioredis.from_url("redis://localhost", encoding="utf-8", decode_responses=True)
app.add_middleware(GeoIPMiddleware, redis_client=redis_client, license_key="mykey")
@app.get('/')
async def index(request: Request):
geo_data = request.state.geo
return {
'country': geo_data.country.name if geo_data else 'Unknown',
}
Configuration
To set up the middleware, you need to add it to your FastAPI application with the necessary parameters:
redis_client: An instance of aioredis client for caching purpose. license_key: Your MaxMind license key for the GeoIP database.
Usage
After adding the middleware to your FastAPI application, you can access the geolocation data in your route handlers using the request.state.geo property.
Contributing
Contributions are welcome! If you would like to contribute to the project, please follow these steps:
Fork the repository. Create a new branch for your feature or fix. Write your code and add tests if applicable. Submit a pull request with a clear description of your changes.
License
fastapi-geolocation is open source software licensed as MIT.
Credits
This project was inspired by the GeoIP functionality in Django and has been adapted for use in FastAPI applications.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file fastapi-geolocation-1.0.1.tar.gz
.
File metadata
- Download URL: fastapi-geolocation-1.0.1.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fc5fea4530f17f28ed8f7dee136af89f4da5446dfdc1f7ca83f285e6da3d9fd |
|
MD5 | b90d291702ab78d5810e575b179c9e11 |
|
BLAKE2b-256 | 5c5631814d027ab057fc03fb98023865494379bd784cfb67602fd43a940354d6 |
File details
Details for the file fastapi_geolocation-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: fastapi_geolocation-1.0.1-py3-none-any.whl
- Upload date:
- Size: 10.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.0.0 CPython/3.8.10
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a2ae3ba06ec91331f1bc0fd6959a350e111e79409d3175008cb41f3b2460a94d |
|
MD5 | f2d03b1e6f0d9aaa522de8fc8f04ee1f |
|
BLAKE2b-256 | 27e802f51a63e94105bc5839e4c7689f4b6542afd9192838b568591d443a3197 |