mirror of
https://github.com/fastapi-practices/fastapi-best-architecture.git
synced 2026-09-21 21:15:13 +00:00
Optimize dependencies to reduce package size (#548)
This commit is contained in:
@@ -1,9 +1,9 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
from datetime import datetime
|
||||
from typing import Annotated
|
||||
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr, validate_email
|
||||
from pydantic_extra_types.phone_numbers import PhoneNumber
|
||||
from pydantic import BaseModel, ConfigDict, EmailStr, Field, validate_email
|
||||
|
||||
from backend.core.conf import settings
|
||||
|
||||
@@ -142,8 +142,7 @@ CUSTOM_USAGE_ERROR_MESSAGES = {
|
||||
}
|
||||
|
||||
|
||||
class CustomPhoneNumber(PhoneNumber):
|
||||
default_region_code = 'CN'
|
||||
CustomPhoneNumber = Annotated[str, Field(pattern=r'^1[3-9]\d{9}$')]
|
||||
|
||||
|
||||
class CustomEmailStr(EmailStr):
|
||||
|
||||
@@ -4,8 +4,8 @@ import httpx
|
||||
|
||||
from asgiref.sync import sync_to_async
|
||||
from fastapi import Request
|
||||
from ip2loc import XdbSearcher
|
||||
from user_agents import parse
|
||||
from XdbSearchIP.xdbSearcher import XdbSearcher
|
||||
|
||||
from backend.common.dataclasses import IpInfo, UserAgentInfo
|
||||
from backend.common.log import log
|
||||
@@ -80,7 +80,7 @@ async def parse_ip_info(request: Request) -> IpInfo:
|
||||
ip = get_request_ip(request)
|
||||
location = await redis_client.get(f'{settings.IP_LOCATION_REDIS_PREFIX}:{ip}')
|
||||
if location:
|
||||
country, region, city = location.split(' ')
|
||||
country, region, city = location.split('|')
|
||||
return IpInfo(ip=ip, country=country, region=region, city=city)
|
||||
if settings.IP_LOCATION_PARSE == 'online':
|
||||
location_info = await get_location_online(ip, request.headers.get('User-Agent'))
|
||||
@@ -94,7 +94,7 @@ async def parse_ip_info(request: Request) -> IpInfo:
|
||||
city = location_info.get('city')
|
||||
await redis_client.set(
|
||||
f'{settings.IP_LOCATION_REDIS_PREFIX}:{ip}',
|
||||
f'{country} {region} {city}',
|
||||
f'{country}|{region}|{city}',
|
||||
ex=settings.IP_LOCATION_EXPIRE_SECONDS,
|
||||
)
|
||||
return IpInfo(ip=ip, country=country, region=region, city=city)
|
||||
|
||||
Reference in New Issue
Block a user