lean-explore 0.1.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1 @@
1
+ """Local package for lean explore."""
@@ -0,0 +1 @@
1
+ """Local package for lean explore."""
@@ -0,0 +1,124 @@
1
+ # src/lean_explore/api/client.py
2
+
3
+ """Provides a client for interacting with the remote Lean Explore API.
4
+
5
+ This module contains the Client class, which facilitates
6
+ communication with the backend Lean Explore search engine API for
7
+ performing searches and retrieving detailed information.
8
+ """
9
+
10
+ from typing import List, Optional
11
+
12
+ import httpx
13
+
14
+ from lean_explore.shared.models.api import (
15
+ APICitationsResponse,
16
+ APISearchResponse,
17
+ APISearchResultItem,
18
+ )
19
+
20
+ _DEFAULT_API_BASE_URL = "https://www.leanexplore.com/api/v1"
21
+
22
+
23
+ class Client:
24
+ """An asynchronous client for the Lean Explore backend API.
25
+
26
+ This client handles making HTTP requests to the production API base URL,
27
+ authenticating with an API key, and parsing responses into Pydantic models.
28
+
29
+ Attributes:
30
+ api_key: The API key used for authenticating requests.
31
+ timeout: The timeout for HTTP requests in seconds.
32
+ base_url: The hardcoded base URL for the API.
33
+ """
34
+
35
+ def __init__(self, api_key: str, timeout: float = 10.0):
36
+ """Initializes the API Client.
37
+
38
+ Args:
39
+ api_key: The API key for authentication.
40
+ timeout: Default timeout for HTTP requests in seconds.
41
+ """
42
+ self.base_url: str = _DEFAULT_API_BASE_URL
43
+ self.api_key: str = api_key
44
+ self.timeout: float = timeout
45
+ self._headers: dict = {"Authorization": f"Bearer {self.api_key}"}
46
+
47
+ async def search(
48
+ self, query: str, package_filters: Optional[List[str]] = None
49
+ ) -> APISearchResponse:
50
+ """Performs a search for statement groups via the API.
51
+
52
+ Args:
53
+ query: The search query string.
54
+ package_filters: An optional list of package names to filter the
55
+ search by.
56
+
57
+ Returns:
58
+ An APISearchResponse object containing the search results and
59
+ associated metadata.
60
+
61
+ Raises:
62
+ httpx.HTTPStatusError: If the API returns an HTTP error status (4xx or 5xx).
63
+ httpx.RequestError: For network-related issues or other request errors.
64
+ """
65
+ endpoint = f"{self.base_url}/search"
66
+ params = {"q": query}
67
+ if package_filters:
68
+ params["pkg"] = package_filters
69
+
70
+ async with httpx.AsyncClient(timeout=self.timeout) as client:
71
+ response = await client.get(endpoint, params=params, headers=self._headers)
72
+ response.raise_for_status()
73
+ return APISearchResponse(**response.json())
74
+
75
+ async def get_by_id(self, group_id: int) -> Optional[APISearchResultItem]:
76
+ """Retrieves a specific statement group by its unique ID via the API.
77
+
78
+ Args:
79
+ group_id: The unique identifier of the statement group.
80
+
81
+ Returns:
82
+ An APISearchResultItem object if the statement group is found,
83
+ otherwise None if a 404 error is received.
84
+
85
+ Raises:
86
+ httpx.HTTPStatusError: If the API returns an HTTP error status
87
+ other than 404 (e.g., 401, 403, 5xx).
88
+ httpx.RequestError: For network-related issues or other request errors.
89
+ """
90
+ endpoint = f"{self.base_url}/statement_groups/{group_id}"
91
+ async with httpx.AsyncClient(timeout=self.timeout) as client:
92
+ response = await client.get(endpoint, headers=self._headers)
93
+ if response.status_code == 404:
94
+ return None
95
+ response.raise_for_status()
96
+ return APISearchResultItem(**response.json())
97
+
98
+ async def get_dependencies(self, group_id: int) -> Optional[APICitationsResponse]:
99
+ """Retrieves the dependencies (citations) for a specific statement group.
100
+
101
+ This method fetches the statement groups that the specified 'group_id'
102
+ depends on (i.e., cites).
103
+
104
+ Args:
105
+ group_id: The unique identifier of the statement group for which
106
+ to fetch dependencies.
107
+
108
+ Returns:
109
+ An APICitationsResponse object containing the list of dependencies
110
+ (cited items) if the source statement group is found. Returns None
111
+ if the source statement group itself is not found (receives a 404).
112
+
113
+ Raises:
114
+ httpx.HTTPStatusError: If the API returns an HTTP error status
115
+ other than 404 (e.g., 401, 403, 5xx).
116
+ httpx.RequestError: For network-related issues or other request errors.
117
+ """
118
+ endpoint = f"{self.base_url}/statement_groups/{group_id}/dependencies"
119
+ async with httpx.AsyncClient(timeout=self.timeout) as client:
120
+ response = await client.get(endpoint, headers=self._headers)
121
+ if response.status_code == 404:
122
+ return None
123
+ response.raise_for_status()
124
+ return APICitationsResponse(**response.json())
@@ -0,0 +1 @@
1
+ """Local package for lean explore."""