Skip to content

Commit

Permalink
refactor: moved to attrs model from pydantic.
Browse files Browse the repository at this point in the history
  • Loading branch information
Almas-Ali committed Jan 30, 2024
1 parent e55af33 commit d81ca78
Showing 1 changed file with 39 additions and 46 deletions.
85 changes: 39 additions & 46 deletions spyip/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from pydantic import BaseModel, Field, field_validator
from attr import define, field, asdict
import json


class IPResponse(BaseModel):
@define
class IPResponse:
"""
Example response from API:
Expand Down Expand Up @@ -32,51 +34,42 @@ class IPResponse(BaseModel):
}
"""

status: str = Field(..., description='Status of the request.')
continent: str = Field(..., description='Continent name.')
continentCode: str = Field(..., description='Continent code.')
country: str = Field(..., description='Country name.')
countryCode: str = Field(..., description='Country code.')
region: str = Field(..., description='Region code.')
regionName: str = Field(..., description='Region name.')
city: str = Field(..., description='City name.')
district: str = Field(..., description='District name.')
zip_: str = Field(..., description='Zip code.')
lat: float = Field(..., description='Latitude.')
lon: float = Field(..., description='Longitude.')
timezone: str = Field(..., description='Timezone.')
offset: int = Field(..., description='Offset.')
currency: str = Field(..., description='Currency.')
isp: str = Field(..., description='ISP name.')
org: str = Field(..., description='Organization name.')
as_: str = Field(..., description='AS number and name.')
asname: str = Field(..., description='AS name.')
mobile: bool = Field(..., description='Mobile status.')
proxy: bool = Field(..., description='Proxy status.')
hosting: bool = Field(..., description='Hosting status.')
query: str = Field(..., description='IP address.')
status: str = field(metadata={'description': 'Status of the request.'})
continent: str = field(metadata={'description': 'Continent name.'})
continentCode: str = field(metadata={'description': 'Continent code.'})
country: str = field(metadata={'description': 'Country name.'})
countryCode: str = field(metadata={'description': 'Country code.'})
region: str = field(metadata={'description': 'Region code.'})
regionName: str = field(metadata={'description': 'Region name.'})
city: str = field(metadata={'description': 'City name.'})
district: str = field(metadata={'description': 'District name.'})
zip_: str = field(metadata={'description': 'Zip code.'}, alias='zip')
lat: float = field(metadata={'description': 'Latitude.'})
lon: float = field(metadata={'description': 'Longitude.'})
timezone: str = field(metadata={'description': 'Timezone.'})
offset: int = field(metadata={'description': 'Offset.'})
currency: str = field(metadata={'description': 'Currency.'})
isp: str = field(metadata={'description': 'ISP name.'})
org: str = field(metadata={'description': 'Organization name.'})
as_: str = field(metadata={'description': 'AS number and name.'}, alias='as_')
asname: str = field(metadata={'description': 'AS name.'})
mobile: bool = field(metadata={'description': 'Mobile status.'})
proxy: bool = field(metadata={'description': 'Proxy status.'})
hosting: bool = field(metadata={'description': 'Hosting status.'})
query: str = field(metadata={'description': 'IP address.'})

class Config:
def alias_generator(x):
return x.replace('_', '')

populate_by_name = True
# fields = { # Alias for reserved keywords
# "as_": "as",
# "zip_": "zip",
# }

@field_validator('status')
def check_status(cls, v):
if v != 'success':
@status.validator
def validate_status(self, attribute, value):
if value != 'success':
raise ValueError('Invalid IP address.')
return v
return value

def json(self, **kwargs) -> str:
return self.model_dump_json(**kwargs)
def json(self, **kwargs):
return json.dumps(asdict(self), **kwargs)


class DNSResponse(BaseModel):
@define
class DNSResponse:
"""
Example response from API:
"dns": {
Expand All @@ -85,8 +78,8 @@ class DNSResponse(BaseModel):
}
"""

ip: str = Field(..., description='IP address.')
geo: str = Field(..., description='Geo location.')
ip: str = field(metadata={'description': 'IP address.'})
geo: str = field(metadata={'description': 'Geo location.'})

def json(self, **kwargs) -> str:
return self.model_dump_json(**kwargs)
def json(self, **kwargs):
return json.dumps(asdict(self), **kwargs)

0 comments on commit d81ca78

Please sign in to comment.