+ moon_phase.py integration module for Astronomy API: implements Pydantic models for handling API request and response

+ auth.py integration module for Astronomy API: responsible for generating an API authentication hash to be placed in request header
This commit is contained in:
T-BENZIN 2024-11-19 21:53:58 +05:00
parent 48aa47c0f6
commit 94b7677108
3 changed files with 68 additions and 0 deletions

3
.idea/.gitignore generated vendored Normal file
View File

@ -0,0 +1,3 @@
# Default ignored files
/shelf/
/workspace.xml

12
astronomy_api/auth.py Normal file
View File

@ -0,0 +1,12 @@
"""Authentication header encoding"""
import base64
from os import environ
app_id, app_key = environ.get('ASTRONOMY_API_APP_ID'), environ.get('ASTRONOMY_API_APP_SECRET')
userpass = f'{app_id}: {app_key}'
auth_string = base64.b64encode(userpass.encode()).decode()
auth_header = {'Authorization': f'Basic {auth_string}'}
app_id, app_key, userpass = None, None, None
if __name__ == '__main__':
print(auth_header)

View File

@ -0,0 +1,53 @@
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))