+ auth.py integration module for Astronomy API: responsible for generating an API authentication hash to be placed in request header
54 lines
1.7 KiB
Python
54 lines
1.7 KiB
Python
from pydantic import BaseModel, Field
|
|
from typing import Literal
|
|
import datetime
|
|
|
|
|
|
class Style(BaseModel):
|
|
moon_style: Literal['default', 'sketch', 'shaded'] = Field(default='default', serialization_alias='moonStyle')
|
|
background_style: Literal['stars', 'solid'] = Field(default='solid', serialization_alias='backgroundStyle')
|
|
background_color: str = Field(default='black', serialization_alias='backgroundColor')
|
|
heading_color: str = Field(default='white', serialization_alias='headingColor')
|
|
text_color: str = Field(default='white', serialization_alias='textColor')
|
|
|
|
|
|
class Observer(BaseModel):
|
|
latitude: float = Field(default=0, lt=-90, gt=90)
|
|
longitude: float = Field(default=0, lt=-180, gt=180)
|
|
date: datetime.date = Field(default_factory=datetime.date.today)
|
|
|
|
class Config:
|
|
json_encoders = {
|
|
datetime: lambda dt: dt.strftime('%Y-%m-%d') # customize the format as needed
|
|
}
|
|
|
|
|
|
class View(BaseModel):
|
|
image_type: Literal['portrait-simple', 'landscape-simple']\
|
|
= Field(default='landscape-simple', serialization_alias='type')
|
|
orientation: Literal['north-up', 'south-up'] = 'north-up'
|
|
|
|
|
|
class MoonPhase(BaseModel):
|
|
format: Literal['png', 'svg'] = 'png'
|
|
style: Style = Field(default_factory=Style)
|
|
observer: Observer = Field(default_factory=Observer)
|
|
view: View = Field(default_factory=View)
|
|
|
|
class Config:
|
|
populate_by_name = True
|
|
|
|
|
|
class ImageURL(BaseModel):
|
|
image_url: str = Field()
|
|
|
|
|
|
class Data(BaseModel):
|
|
data: dict[Literal['data'], ImageURL]
|
|
|
|
|
|
url = 'https://api.astronomyapi.com/api/v2/studio/moon-phase'
|
|
|
|
if __name__ == '__main__':
|
|
model = MoonPhase()
|
|
print(model.model_dump_json(indent=4, by_alias=True))
|