Lukazlim: Replace dependency requests with httpx (#12744)

* Replace dependency `requests` with `httpx`

Fixes #12742
Signed-off-by: Lim, Lukaz Wei Hwang <lukaz.wei.hwang.lim@intel.com>

* updating DIRECTORY.md

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

---------

Signed-off-by: Lim, Lukaz Wei Hwang <lukaz.wei.hwang.lim@intel.com>
Co-authored-by: Lim, Lukaz Wei Hwang <lukaz.wei.hwang.lim@intel.com>
Co-authored-by: cclauss <cclauss@users.noreply.github.com>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
This commit is contained in:
Christian Clauss
2025-05-14 03:42:11 +02:00
committed by GitHub
parent 6e4d1b3765
commit a2fa32c7ad
40 changed files with 957 additions and 640 deletions

View File

@ -1,6 +1,11 @@
import shutil
# /// script
# requires-python = ">=3.13"
# dependencies = [
# "httpx",
# ]
# ///
import requests
import httpx
def get_apod_data(api_key: str) -> dict:
@ -9,17 +14,17 @@ def get_apod_data(api_key: str) -> dict:
Get your API Key from: https://api.nasa.gov/
"""
url = "https://api.nasa.gov/planetary/apod"
return requests.get(url, params={"api_key": api_key}, timeout=10).json()
return httpx.get(url, params={"api_key": api_key}, timeout=10).json()
def save_apod(api_key: str, path: str = ".") -> dict:
apod_data = get_apod_data(api_key)
img_url = apod_data["url"]
img_name = img_url.split("/")[-1]
response = requests.get(img_url, stream=True, timeout=10)
response = httpx.get(img_url, timeout=10)
with open(f"{path}/{img_name}", "wb+") as img_file:
shutil.copyfileobj(response.raw, img_file)
img_file.write(response.content)
del response
return apod_data
@ -29,7 +34,7 @@ def get_archive_data(query: str) -> dict:
Get the data of a particular query from NASA archives
"""
url = "https://images-api.nasa.gov/search"
return requests.get(url, params={"q": query}, timeout=10).json()
return httpx.get(url, params={"q": query}, timeout=10).json()
if __name__ == "__main__":