Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions easyeda2kicad/easyeda/easyeda_api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
# Global imports
import logging
import ssl

import requests
import httpx
import truststore

from easyeda2kicad import __version__

Expand All @@ -22,8 +24,14 @@ def __init__(self) -> None:
"User-Agent": f"easyeda2kicad v{__version__}",
}

self._ssl_context = truststore.SSLContext(ssl.PROTOCOL_TLS_CLIENT)

def get_info_from_easyeda_api(self, lcsc_id: str) -> dict:
r = requests.get(url=API_ENDPOINT.format(lcsc_id=lcsc_id), headers=self.headers)
with httpx.Client(verify=self._ssl_context) as client:
r = client.get(
url=API_ENDPOINT.format(lcsc_id=lcsc_id), headers=self.headers
)

api_response = r.json()

if not api_response or (
Expand All @@ -41,21 +49,31 @@ def get_cad_data_of_component(self, lcsc_id: str) -> dict:
return cp_cad_info["result"]

def get_raw_3d_model_obj(self, uuid: str) -> str:
r = requests.get(
url=ENDPOINT_3D_MODEL.format(uuid=uuid),
headers={"User-Agent": self.headers["User-Agent"]},
)
if r.status_code != requests.codes.ok:
with httpx.Client(verify=self._ssl_context) as client:
r = client.get(
url=ENDPOINT_3D_MODEL.format(uuid=uuid),
headers={"User-Agent": self.headers["User-Agent"]},
)

try:
r.raise_for_status()
except httpx.HTTPStatusError:
logging.error(f"No raw 3D model data found for uuid:{uuid} on easyeda")
return None

return r.content.decode()

def get_step_3d_model(self, uuid: str) -> bytes:
r = requests.get(
url=ENDPOINT_3D_MODEL_STEP.format(uuid=uuid),
headers={"User-Agent": self.headers["User-Agent"]},
)
if r.status_code != requests.codes.ok:
with httpx.Client(verify=self._ssl_context) as client:
r = client.get(
url=ENDPOINT_3D_MODEL_STEP.format(uuid=uuid),
headers={"User-Agent": self.headers["User-Agent"]},
)

try:
r.raise_for_status()
except httpx.HTTPStatusError:
logging.error(f"No step 3D model data found for uuid:{uuid} on easyeda")
return None

return r.content
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ classifiers = [
"Topic :: Scientific/Engineering :: Electronic Design Automation (EDA)",
]
dependencies = [
"httpx>=0.28.1",
"pydantic>=2.5.0",
"requests>=2.31.0",
"truststore>=0.10.4",
]

[project.optional-dependencies]
Expand Down
Loading