From 94b767710849479f4d405dc3f6509958b10272e9 Mon Sep 17 00:00:00 2001 From: T-BENZIN Date: Tue, 19 Nov 2024 21:53:58 +0500 Subject: [PATCH] + 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 --- .idea/.gitignore | 3 +++ astronomy_api/auth.py | 12 +++++++++ astronomy_api/moon_phase.py | 53 +++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 astronomy_api/auth.py create mode 100644 astronomy_api/moon_phase.py diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/astronomy_api/auth.py b/astronomy_api/auth.py new file mode 100644 index 0000000..b64a638 --- /dev/null +++ b/astronomy_api/auth.py @@ -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) diff --git a/astronomy_api/moon_phase.py b/astronomy_api/moon_phase.py new file mode 100644 index 0000000..dea1756 --- /dev/null +++ b/astronomy_api/moon_phase.py @@ -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))