levelfour 0.1.0__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.
- levelfour/__init__.py +495 -0
- levelfour/api_keys/__init__.py +34 -0
- levelfour/api_keys/client.py +337 -0
- levelfour/api_keys/raw_client.py +621 -0
- levelfour/api_keys/types/__init__.py +34 -0
- levelfour/api_keys/types/create_api_key_request_scope.py +5 -0
- levelfour/async_levelfour_client.py +39 -0
- levelfour/base_client.py +346 -0
- levelfour/client.py +54 -0
- levelfour/core/__init__.py +127 -0
- levelfour/core/api_error.py +23 -0
- levelfour/core/client_wrapper.py +105 -0
- levelfour/core/datetime_utils.py +70 -0
- levelfour/core/file.py +67 -0
- levelfour/core/force_multipart.py +18 -0
- levelfour/core/http_client.py +776 -0
- levelfour/core/http_response.py +59 -0
- levelfour/core/http_sse/__init__.py +42 -0
- levelfour/core/http_sse/_api.py +112 -0
- levelfour/core/http_sse/_decoders.py +61 -0
- levelfour/core/http_sse/_exceptions.py +7 -0
- levelfour/core/http_sse/_models.py +17 -0
- levelfour/core/jsonable_encoder.py +108 -0
- levelfour/core/logging.py +107 -0
- levelfour/core/pagination.py +82 -0
- levelfour/core/pydantic_utilities.py +577 -0
- levelfour/core/query_encoder.py +58 -0
- levelfour/core/remove_none_from_dict.py +11 -0
- levelfour/core/request_options.py +35 -0
- levelfour/core/serialization.py +276 -0
- levelfour/errors/__init__.py +40 -0
- levelfour/errors/forbidden_error.py +11 -0
- levelfour/errors/unauthorized_error.py +11 -0
- levelfour/errors/unprocessable_entity_error.py +11 -0
- levelfour/evaluation/__init__.py +4 -0
- levelfour/evaluation/client.py +102 -0
- levelfour/evaluation/raw_client.py +141 -0
- levelfour/exceptions.py +63 -0
- levelfour/health/__init__.py +4 -0
- levelfour/health/client.py +163 -0
- levelfour/health/raw_client.py +164 -0
- levelfour/integrations/__init__.py +0 -0
- levelfour/integrations/django.py +26 -0
- levelfour/integrations/fastapi.py +39 -0
- levelfour/integrations/telemetry.py +66 -0
- levelfour/levelfour_client.py +47 -0
- levelfour/pagination.py +84 -0
- levelfour/pr_analysis/__init__.py +4 -0
- levelfour/pr_analysis/client.py +408 -0
- levelfour/pr_analysis/raw_client.py +620 -0
- levelfour/pr_analysis_actions/__init__.py +4 -0
- levelfour/pr_analysis_actions/client.py +588 -0
- levelfour/pr_analysis_actions/raw_client.py +939 -0
- levelfour/pr_analysis_installations/__init__.py +4 -0
- levelfour/pr_analysis_installations/client.py +470 -0
- levelfour/pr_analysis_installations/raw_client.py +868 -0
- levelfour/providers/__init__.py +34 -0
- levelfour/providers/client.py +1504 -0
- levelfour/providers/raw_client.py +2398 -0
- levelfour/providers/types/__init__.py +36 -0
- levelfour/providers/types/list_spending_breakdown_providers_response.py +11 -0
- levelfour/py.typed +0 -0
- levelfour/recommendations/__init__.py +4 -0
- levelfour/recommendations/client.py +1018 -0
- levelfour/recommendations/raw_client.py +1810 -0
- levelfour/savings/__init__.py +4 -0
- levelfour/savings/client.py +401 -0
- levelfour/savings/raw_client.py +624 -0
- levelfour/spending/__init__.py +34 -0
- levelfour/spending/client.py +437 -0
- levelfour/spending/raw_client.py +684 -0
- levelfour/spending/types/__init__.py +34 -0
- levelfour/spending/types/list_breakdown_spending_response.py +12 -0
- levelfour/types/__init__.py +413 -0
- levelfour/types/accept_terms_data.py +25 -0
- levelfour/types/accept_terms_response.py +27 -0
- levelfour/types/action_request.py +42 -0
- levelfour/types/api_key_created_data.py +28 -0
- levelfour/types/api_key_data.py +27 -0
- levelfour/types/authentication_error.py +5 -0
- levelfour/types/authorization_error.py +5 -0
- levelfour/types/bad_request_error.py +5 -0
- levelfour/types/comparison_item.py +32 -0
- levelfour/types/complete_wizard_data.py +27 -0
- levelfour/types/complete_wizard_response.py +27 -0
- levelfour/types/conflict_error.py +5 -0
- levelfour/types/cost_breakdown_data.py +35 -0
- levelfour/types/cost_breakdown_item.py +71 -0
- levelfour/types/cost_breakdown_raw_data.py +37 -0
- levelfour/types/cost_breakdown_raw_response.py +27 -0
- levelfour/types/cost_breakdown_response.py +27 -0
- levelfour/types/costs_breakdown_chart_data.py +48 -0
- levelfour/types/costs_breakdown_chart_response.py +27 -0
- levelfour/types/costs_breakdown_data_point.py +46 -0
- levelfour/types/costs_breakdown_grouped_chart_data.py +52 -0
- levelfour/types/costs_breakdown_grouped_chart_response.py +27 -0
- levelfour/types/costs_breakdown_grouped_data_point.py +36 -0
- levelfour/types/costs_breakdown_totals.py +41 -0
- levelfour/types/create_api_key_response.py +23 -0
- levelfour/types/daily_spending_breakdown_data.py +42 -0
- levelfour/types/daily_spending_breakdown_response.py +27 -0
- levelfour/types/daily_spending_data_point.py +31 -0
- levelfour/types/dashboard_summary_data.py +72 -0
- levelfour/types/dashboard_summary_response.py +27 -0
- levelfour/types/delete_endpoint_data.py +20 -0
- levelfour/types/delete_endpoint_response.py +23 -0
- levelfour/types/endpoint_data.py +23 -0
- levelfour/types/error_response.py +23 -0
- levelfour/types/error_response_error.py +21 -0
- levelfour/types/evaluation_status_data.py +71 -0
- levelfour/types/evaluation_status_response.py +27 -0
- levelfour/types/execution_log_command.py +21 -0
- levelfour/types/execution_log_entry.py +23 -0
- levelfour/types/execution_step_schema.py +23 -0
- levelfour/types/http_validation_error.py +20 -0
- levelfour/types/implementation_method_data.py +25 -0
- levelfour/types/implementation_method_response.py +27 -0
- levelfour/types/implementation_step.py +27 -0
- levelfour/types/internal_server_error.py +5 -0
- levelfour/types/list_api_keys_data.py +21 -0
- levelfour/types/list_api_keys_response.py +23 -0
- levelfour/types/list_endpoints_response.py +23 -0
- levelfour/types/metric_data_point.py +27 -0
- levelfour/types/metric_series.py +40 -0
- levelfour/types/metric_series_entry.py +25 -0
- levelfour/types/monthly_spending_data.py +27 -0
- levelfour/types/monthly_spending_data_point.py +31 -0
- levelfour/types/monthly_spending_response.py +27 -0
- levelfour/types/not_found_error.py +5 -0
- levelfour/types/pagination_meta.py +51 -0
- levelfour/types/processing_recommendation_item.py +61 -0
- levelfour/types/processing_recommendations_data.py +28 -0
- levelfour/types/processing_recommendations_response.py +27 -0
- levelfour/types/provider.py +46 -0
- levelfour/types/provider_breakdown_data.py +31 -0
- levelfour/types/provider_breakdown_item.py +77 -0
- levelfour/types/provider_breakdown_response.py +27 -0
- levelfour/types/provider_filter_options_data.py +51 -0
- levelfour/types/provider_filter_options_response.py +27 -0
- levelfour/types/provider_potential_savings_summary_data.py +46 -0
- levelfour/types/provider_potential_savings_summary_response.py +27 -0
- levelfour/types/provider_recommendations_summary_data.py +37 -0
- levelfour/types/provider_recommendations_summary_response.py +27 -0
- levelfour/types/provider_saved_summary_data.py +56 -0
- levelfour/types/provider_saved_summary_response.py +27 -0
- levelfour/types/provider_service_breakdown_data.py +65 -0
- levelfour/types/provider_service_breakdown_item.py +72 -0
- levelfour/types/provider_service_breakdown_raw_data.py +62 -0
- levelfour/types/provider_service_breakdown_raw_response.py +27 -0
- levelfour/types/provider_service_breakdown_response.py +27 -0
- levelfour/types/provider_service_costs_breakdown_data.py +62 -0
- levelfour/types/provider_service_costs_breakdown_response.py +27 -0
- levelfour/types/provider_service_costs_data_point.py +36 -0
- levelfour/types/provider_spending.py +61 -0
- levelfour/types/provider_spending_summary_data.py +67 -0
- levelfour/types/provider_spending_summary_response.py +27 -0
- levelfour/types/provider_timeline_data.py +57 -0
- levelfour/types/provider_timeline_response.py +27 -0
- levelfour/types/provider_top_service.py +36 -0
- levelfour/types/providers_summary_data.py +24 -0
- levelfour/types/providers_summary_response.py +27 -0
- levelfour/types/rate_limit_error.py +5 -0
- levelfour/types/recommendation_actions.py +60 -0
- levelfour/types/recommendation_detail.py +42 -0
- levelfour/types/recommendation_detail_response.py +27 -0
- levelfour/types/recommendation_filter_options_data.py +41 -0
- levelfour/types/recommendation_filter_options_response.py +27 -0
- levelfour/types/recommendation_item.py +36 -0
- levelfour/types/recommendation_wizard_data.py +173 -0
- levelfour/types/recommendation_wizard_data_metrics.py +7 -0
- levelfour/types/recommendation_wizard_response.py +27 -0
- levelfour/types/recommendations_breakdown_data.py +26 -0
- levelfour/types/recommendations_breakdown_response.py +27 -0
- levelfour/types/recommendations_overview_data.py +51 -0
- levelfour/types/recommendations_overview_response.py +27 -0
- levelfour/types/register_endpoint_response.py +23 -0
- levelfour/types/resource_change.py +42 -0
- levelfour/types/resource_snapshot.py +21 -0
- levelfour/types/revoke_api_key_data.py +20 -0
- levelfour/types/revoke_api_key_response.py +23 -0
- levelfour/types/risk_assessment.py +29 -0
- levelfour/types/risk_factor.py +27 -0
- levelfour/types/saved_breakdown_data.py +31 -0
- levelfour/types/saved_breakdown_response.py +27 -0
- levelfour/types/saved_by_provider_breakdown_data.py +31 -0
- levelfour/types/saved_by_provider_breakdown_response.py +27 -0
- levelfour/types/saved_by_provider_item.py +76 -0
- levelfour/types/saved_item.py +35 -0
- levelfour/types/saved_provider_summary.py +36 -0
- levelfour/types/saved_summary_data.py +57 -0
- levelfour/types/saved_summary_response.py +27 -0
- levelfour/types/savings_decision_data.py +28 -0
- levelfour/types/savings_decision_response.py +27 -0
- levelfour/types/spending_by_date_item.py +36 -0
- levelfour/types/top_recommendation.py +56 -0
- levelfour/types/trigger_info.py +22 -0
- levelfour/types/update_tag_data.py +20 -0
- levelfour/types/update_tag_response.py +23 -0
- levelfour/types/validation_error.py +24 -0
- levelfour/types/validation_error_loc_item.py +5 -0
- levelfour/webhooks/__init__.py +4 -0
- levelfour/webhooks/client.py +273 -0
- levelfour/webhooks/raw_client.py +483 -0
- levelfour/webhooks/verifier.py +61 -0
- levelfour-0.1.0.dist-info/METADATA +12 -0
- levelfour-0.1.0.dist-info/RECORD +208 -0
- levelfour-0.1.0.dist-info/WHEEL +5 -0
- levelfour-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
# This file was auto-generated by Fern from our API Definition.
|
|
2
|
+
|
|
3
|
+
import typing
|
|
4
|
+
|
|
5
|
+
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
|
|
6
|
+
from ..core.request_options import RequestOptions
|
|
7
|
+
from .raw_client import AsyncRawHealthClient, RawHealthClient
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class HealthClient:
|
|
11
|
+
def __init__(self, *, client_wrapper: SyncClientWrapper):
|
|
12
|
+
self._raw_client = RawHealthClient(client_wrapper=client_wrapper)
|
|
13
|
+
|
|
14
|
+
@property
|
|
15
|
+
def with_raw_response(self) -> RawHealthClient:
|
|
16
|
+
"""
|
|
17
|
+
Retrieves a raw implementation of this client that returns raw responses.
|
|
18
|
+
|
|
19
|
+
Returns
|
|
20
|
+
-------
|
|
21
|
+
RawHealthClient
|
|
22
|
+
"""
|
|
23
|
+
return self._raw_client
|
|
24
|
+
|
|
25
|
+
def root(self, *, request_options: typing.Optional[RequestOptions] = None) -> typing.Dict[str, str]:
|
|
26
|
+
"""
|
|
27
|
+
Root endpoint (supports GET and HEAD)
|
|
28
|
+
|
|
29
|
+
Parameters
|
|
30
|
+
----------
|
|
31
|
+
request_options : typing.Optional[RequestOptions]
|
|
32
|
+
Request-specific configuration.
|
|
33
|
+
|
|
34
|
+
Returns
|
|
35
|
+
-------
|
|
36
|
+
typing.Dict[str, str]
|
|
37
|
+
Successful Response
|
|
38
|
+
|
|
39
|
+
Examples
|
|
40
|
+
--------
|
|
41
|
+
from levelfour import BaseLevelFour
|
|
42
|
+
|
|
43
|
+
client = BaseLevelFour(
|
|
44
|
+
token="YOUR_TOKEN",
|
|
45
|
+
base_url="https://yourhost.com/path/to/api",
|
|
46
|
+
)
|
|
47
|
+
client.health.root()
|
|
48
|
+
"""
|
|
49
|
+
_response = self._raw_client.root(request_options=request_options)
|
|
50
|
+
return _response.headers
|
|
51
|
+
|
|
52
|
+
def health_check(self, *, request_options: typing.Optional[RequestOptions] = None) -> typing.Dict[str, str]:
|
|
53
|
+
"""
|
|
54
|
+
Health check endpoint (supports GET and HEAD)
|
|
55
|
+
|
|
56
|
+
Parameters
|
|
57
|
+
----------
|
|
58
|
+
request_options : typing.Optional[RequestOptions]
|
|
59
|
+
Request-specific configuration.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
typing.Dict[str, str]
|
|
64
|
+
Successful Response
|
|
65
|
+
|
|
66
|
+
Examples
|
|
67
|
+
--------
|
|
68
|
+
from levelfour import BaseLevelFour
|
|
69
|
+
|
|
70
|
+
client = BaseLevelFour(
|
|
71
|
+
token="YOUR_TOKEN",
|
|
72
|
+
base_url="https://yourhost.com/path/to/api",
|
|
73
|
+
)
|
|
74
|
+
client.health.health_check()
|
|
75
|
+
"""
|
|
76
|
+
_response = self._raw_client.health_check(request_options=request_options)
|
|
77
|
+
return _response.headers
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class AsyncHealthClient:
|
|
81
|
+
def __init__(self, *, client_wrapper: AsyncClientWrapper):
|
|
82
|
+
self._raw_client = AsyncRawHealthClient(client_wrapper=client_wrapper)
|
|
83
|
+
|
|
84
|
+
@property
|
|
85
|
+
def with_raw_response(self) -> AsyncRawHealthClient:
|
|
86
|
+
"""
|
|
87
|
+
Retrieves a raw implementation of this client that returns raw responses.
|
|
88
|
+
|
|
89
|
+
Returns
|
|
90
|
+
-------
|
|
91
|
+
AsyncRawHealthClient
|
|
92
|
+
"""
|
|
93
|
+
return self._raw_client
|
|
94
|
+
|
|
95
|
+
async def root(self, *, request_options: typing.Optional[RequestOptions] = None) -> typing.Dict[str, str]:
|
|
96
|
+
"""
|
|
97
|
+
Root endpoint (supports GET and HEAD)
|
|
98
|
+
|
|
99
|
+
Parameters
|
|
100
|
+
----------
|
|
101
|
+
request_options : typing.Optional[RequestOptions]
|
|
102
|
+
Request-specific configuration.
|
|
103
|
+
|
|
104
|
+
Returns
|
|
105
|
+
-------
|
|
106
|
+
typing.Dict[str, str]
|
|
107
|
+
Successful Response
|
|
108
|
+
|
|
109
|
+
Examples
|
|
110
|
+
--------
|
|
111
|
+
import asyncio
|
|
112
|
+
|
|
113
|
+
from levelfour import AsyncBaseLevelFour
|
|
114
|
+
|
|
115
|
+
client = AsyncBaseLevelFour(
|
|
116
|
+
token="YOUR_TOKEN",
|
|
117
|
+
base_url="https://yourhost.com/path/to/api",
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
async def main() -> None:
|
|
122
|
+
await client.health.root()
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
asyncio.run(main())
|
|
126
|
+
"""
|
|
127
|
+
_response = await self._raw_client.root(request_options=request_options)
|
|
128
|
+
return _response.headers
|
|
129
|
+
|
|
130
|
+
async def health_check(self, *, request_options: typing.Optional[RequestOptions] = None) -> typing.Dict[str, str]:
|
|
131
|
+
"""
|
|
132
|
+
Health check endpoint (supports GET and HEAD)
|
|
133
|
+
|
|
134
|
+
Parameters
|
|
135
|
+
----------
|
|
136
|
+
request_options : typing.Optional[RequestOptions]
|
|
137
|
+
Request-specific configuration.
|
|
138
|
+
|
|
139
|
+
Returns
|
|
140
|
+
-------
|
|
141
|
+
typing.Dict[str, str]
|
|
142
|
+
Successful Response
|
|
143
|
+
|
|
144
|
+
Examples
|
|
145
|
+
--------
|
|
146
|
+
import asyncio
|
|
147
|
+
|
|
148
|
+
from levelfour import AsyncBaseLevelFour
|
|
149
|
+
|
|
150
|
+
client = AsyncBaseLevelFour(
|
|
151
|
+
token="YOUR_TOKEN",
|
|
152
|
+
base_url="https://yourhost.com/path/to/api",
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
async def main() -> None:
|
|
157
|
+
await client.health.health_check()
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
asyncio.run(main())
|
|
161
|
+
"""
|
|
162
|
+
_response = await self._raw_client.health_check(request_options=request_options)
|
|
163
|
+
return _response.headers
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
# This file was auto-generated by Fern from our API Definition.
|
|
2
|
+
|
|
3
|
+
import typing
|
|
4
|
+
from json.decoder import JSONDecodeError
|
|
5
|
+
|
|
6
|
+
from ..core.api_error import ApiError
|
|
7
|
+
from ..core.client_wrapper import AsyncClientWrapper, SyncClientWrapper
|
|
8
|
+
from ..core.http_response import AsyncHttpResponse, HttpResponse
|
|
9
|
+
from ..core.pydantic_utilities import parse_obj_as
|
|
10
|
+
from ..core.request_options import RequestOptions
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class RawHealthClient:
|
|
14
|
+
def __init__(self, *, client_wrapper: SyncClientWrapper):
|
|
15
|
+
self._client_wrapper = client_wrapper
|
|
16
|
+
|
|
17
|
+
def root(self, *, request_options: typing.Optional[RequestOptions] = None) -> HttpResponse[typing.Any]:
|
|
18
|
+
"""
|
|
19
|
+
Root endpoint (supports GET and HEAD)
|
|
20
|
+
|
|
21
|
+
Parameters
|
|
22
|
+
----------
|
|
23
|
+
request_options : typing.Optional[RequestOptions]
|
|
24
|
+
Request-specific configuration.
|
|
25
|
+
|
|
26
|
+
Returns
|
|
27
|
+
-------
|
|
28
|
+
HttpResponse[typing.Any]
|
|
29
|
+
Successful Response
|
|
30
|
+
"""
|
|
31
|
+
_response = self._client_wrapper.httpx_client.request(
|
|
32
|
+
method="HEAD",
|
|
33
|
+
request_options=request_options,
|
|
34
|
+
)
|
|
35
|
+
try:
|
|
36
|
+
if _response is None or not _response.text.strip():
|
|
37
|
+
return HttpResponse(response=_response, data=None)
|
|
38
|
+
if 200 <= _response.status_code < 300:
|
|
39
|
+
_data = typing.cast(
|
|
40
|
+
typing.Any,
|
|
41
|
+
parse_obj_as(
|
|
42
|
+
type_=typing.Any, # type: ignore
|
|
43
|
+
object_=_response.json(),
|
|
44
|
+
),
|
|
45
|
+
)
|
|
46
|
+
return HttpResponse(response=_response, data=_data)
|
|
47
|
+
_response_json = _response.json()
|
|
48
|
+
except JSONDecodeError:
|
|
49
|
+
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
|
|
50
|
+
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
|
|
51
|
+
|
|
52
|
+
def health_check(self, *, request_options: typing.Optional[RequestOptions] = None) -> HttpResponse[typing.Any]:
|
|
53
|
+
"""
|
|
54
|
+
Health check endpoint (supports GET and HEAD)
|
|
55
|
+
|
|
56
|
+
Parameters
|
|
57
|
+
----------
|
|
58
|
+
request_options : typing.Optional[RequestOptions]
|
|
59
|
+
Request-specific configuration.
|
|
60
|
+
|
|
61
|
+
Returns
|
|
62
|
+
-------
|
|
63
|
+
HttpResponse[typing.Any]
|
|
64
|
+
Successful Response
|
|
65
|
+
"""
|
|
66
|
+
_response = self._client_wrapper.httpx_client.request(
|
|
67
|
+
"health",
|
|
68
|
+
method="HEAD",
|
|
69
|
+
request_options=request_options,
|
|
70
|
+
)
|
|
71
|
+
try:
|
|
72
|
+
if _response is None or not _response.text.strip():
|
|
73
|
+
return HttpResponse(response=_response, data=None)
|
|
74
|
+
if 200 <= _response.status_code < 300:
|
|
75
|
+
_data = typing.cast(
|
|
76
|
+
typing.Any,
|
|
77
|
+
parse_obj_as(
|
|
78
|
+
type_=typing.Any, # type: ignore
|
|
79
|
+
object_=_response.json(),
|
|
80
|
+
),
|
|
81
|
+
)
|
|
82
|
+
return HttpResponse(response=_response, data=_data)
|
|
83
|
+
_response_json = _response.json()
|
|
84
|
+
except JSONDecodeError:
|
|
85
|
+
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
|
|
86
|
+
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
class AsyncRawHealthClient:
|
|
90
|
+
def __init__(self, *, client_wrapper: AsyncClientWrapper):
|
|
91
|
+
self._client_wrapper = client_wrapper
|
|
92
|
+
|
|
93
|
+
async def root(self, *, request_options: typing.Optional[RequestOptions] = None) -> AsyncHttpResponse[typing.Any]:
|
|
94
|
+
"""
|
|
95
|
+
Root endpoint (supports GET and HEAD)
|
|
96
|
+
|
|
97
|
+
Parameters
|
|
98
|
+
----------
|
|
99
|
+
request_options : typing.Optional[RequestOptions]
|
|
100
|
+
Request-specific configuration.
|
|
101
|
+
|
|
102
|
+
Returns
|
|
103
|
+
-------
|
|
104
|
+
AsyncHttpResponse[typing.Any]
|
|
105
|
+
Successful Response
|
|
106
|
+
"""
|
|
107
|
+
_response = await self._client_wrapper.httpx_client.request(
|
|
108
|
+
method="HEAD",
|
|
109
|
+
request_options=request_options,
|
|
110
|
+
)
|
|
111
|
+
try:
|
|
112
|
+
if _response is None or not _response.text.strip():
|
|
113
|
+
return AsyncHttpResponse(response=_response, data=None)
|
|
114
|
+
if 200 <= _response.status_code < 300:
|
|
115
|
+
_data = typing.cast(
|
|
116
|
+
typing.Any,
|
|
117
|
+
parse_obj_as(
|
|
118
|
+
type_=typing.Any, # type: ignore
|
|
119
|
+
object_=_response.json(),
|
|
120
|
+
),
|
|
121
|
+
)
|
|
122
|
+
return AsyncHttpResponse(response=_response, data=_data)
|
|
123
|
+
_response_json = _response.json()
|
|
124
|
+
except JSONDecodeError:
|
|
125
|
+
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
|
|
126
|
+
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
|
|
127
|
+
|
|
128
|
+
async def health_check(
|
|
129
|
+
self, *, request_options: typing.Optional[RequestOptions] = None
|
|
130
|
+
) -> AsyncHttpResponse[typing.Any]:
|
|
131
|
+
"""
|
|
132
|
+
Health check endpoint (supports GET and HEAD)
|
|
133
|
+
|
|
134
|
+
Parameters
|
|
135
|
+
----------
|
|
136
|
+
request_options : typing.Optional[RequestOptions]
|
|
137
|
+
Request-specific configuration.
|
|
138
|
+
|
|
139
|
+
Returns
|
|
140
|
+
-------
|
|
141
|
+
AsyncHttpResponse[typing.Any]
|
|
142
|
+
Successful Response
|
|
143
|
+
"""
|
|
144
|
+
_response = await self._client_wrapper.httpx_client.request(
|
|
145
|
+
"health",
|
|
146
|
+
method="HEAD",
|
|
147
|
+
request_options=request_options,
|
|
148
|
+
)
|
|
149
|
+
try:
|
|
150
|
+
if _response is None or not _response.text.strip():
|
|
151
|
+
return AsyncHttpResponse(response=_response, data=None)
|
|
152
|
+
if 200 <= _response.status_code < 300:
|
|
153
|
+
_data = typing.cast(
|
|
154
|
+
typing.Any,
|
|
155
|
+
parse_obj_as(
|
|
156
|
+
type_=typing.Any, # type: ignore
|
|
157
|
+
object_=_response.json(),
|
|
158
|
+
),
|
|
159
|
+
)
|
|
160
|
+
return AsyncHttpResponse(response=_response, data=_data)
|
|
161
|
+
_response_json = _response.json()
|
|
162
|
+
except JSONDecodeError:
|
|
163
|
+
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response.text)
|
|
164
|
+
raise ApiError(status_code=_response.status_code, headers=dict(_response.headers), body=_response_json)
|
|
File without changes
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from levelfour import LevelFour
|
|
4
|
+
|
|
5
|
+
_client: LevelFour | None = None
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_client(
|
|
9
|
+
*,
|
|
10
|
+
api_key: str | None = None,
|
|
11
|
+
base_url: str = "https://api.levelfour.ai",
|
|
12
|
+
) -> LevelFour:
|
|
13
|
+
global _client
|
|
14
|
+
if _client is not None:
|
|
15
|
+
return _client
|
|
16
|
+
|
|
17
|
+
resolved_key = api_key
|
|
18
|
+
if resolved_key is None:
|
|
19
|
+
try:
|
|
20
|
+
from django.conf import settings
|
|
21
|
+
resolved_key = getattr(settings, "LEVELFOUR_API_KEY", None)
|
|
22
|
+
except ImportError:
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
_client = LevelFour(api_key=resolved_key, base_url=base_url)
|
|
26
|
+
return _client
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from levelfour import AsyncLevelFour
|
|
6
|
+
|
|
7
|
+
_client: AsyncLevelFour | None = None
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class LevelFourMiddleware:
|
|
11
|
+
def __init__(
|
|
12
|
+
self,
|
|
13
|
+
app: Any,
|
|
14
|
+
*,
|
|
15
|
+
api_key: str | None = None,
|
|
16
|
+
base_url: str = "https://api.levelfour.ai",
|
|
17
|
+
timeout: float = 30.0,
|
|
18
|
+
max_retries: int = 2,
|
|
19
|
+
):
|
|
20
|
+
global _client
|
|
21
|
+
_client = AsyncLevelFour(
|
|
22
|
+
api_key=api_key,
|
|
23
|
+
base_url=base_url,
|
|
24
|
+
timeout=timeout,
|
|
25
|
+
max_retries=max_retries,
|
|
26
|
+
)
|
|
27
|
+
self.app = app
|
|
28
|
+
|
|
29
|
+
async def __call__(self, scope: dict, receive: Any, send: Any) -> None:
|
|
30
|
+
await self.app(scope, receive, send)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def get_levelfour() -> AsyncLevelFour:
|
|
34
|
+
if _client is None:
|
|
35
|
+
raise RuntimeError(
|
|
36
|
+
"LevelFourMiddleware not initialized. "
|
|
37
|
+
"Add LevelFourMiddleware to your FastAPI app first."
|
|
38
|
+
)
|
|
39
|
+
return _client
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import functools
|
|
4
|
+
from typing import Any, Callable, TypeVar
|
|
5
|
+
|
|
6
|
+
F = TypeVar("F", bound=Callable[..., Any])
|
|
7
|
+
|
|
8
|
+
_tracer = None
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def _get_tracer():
|
|
12
|
+
global _tracer
|
|
13
|
+
if _tracer is not None:
|
|
14
|
+
return _tracer
|
|
15
|
+
try:
|
|
16
|
+
from opentelemetry import trace
|
|
17
|
+
_tracer = trace.get_tracer("levelfour-sdk")
|
|
18
|
+
except ImportError:
|
|
19
|
+
_tracer = None
|
|
20
|
+
return _tracer
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def traced(operation_name: str) -> Callable[[F], F]:
|
|
24
|
+
def decorator(func: F) -> F:
|
|
25
|
+
@functools.wraps(func)
|
|
26
|
+
def wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
27
|
+
tracer = _get_tracer()
|
|
28
|
+
if tracer is None:
|
|
29
|
+
return func(*args, **kwargs)
|
|
30
|
+
|
|
31
|
+
with tracer.start_as_current_span(f"levelfour.{operation_name}") as span:
|
|
32
|
+
span.set_attribute("levelfour.operation", operation_name)
|
|
33
|
+
try:
|
|
34
|
+
result = func(*args, **kwargs)
|
|
35
|
+
span.set_attribute("levelfour.success", True)
|
|
36
|
+
return result
|
|
37
|
+
except Exception as e:
|
|
38
|
+
span.set_attribute("levelfour.success", False)
|
|
39
|
+
span.set_attribute("levelfour.error.type", type(e).__name__)
|
|
40
|
+
span.record_exception(e)
|
|
41
|
+
raise
|
|
42
|
+
|
|
43
|
+
@functools.wraps(func)
|
|
44
|
+
async def async_wrapper(*args: Any, **kwargs: Any) -> Any:
|
|
45
|
+
tracer = _get_tracer()
|
|
46
|
+
if tracer is None:
|
|
47
|
+
return await func(*args, **kwargs)
|
|
48
|
+
|
|
49
|
+
with tracer.start_as_current_span(f"levelfour.{operation_name}") as span:
|
|
50
|
+
span.set_attribute("levelfour.operation", operation_name)
|
|
51
|
+
try:
|
|
52
|
+
result = await func(*args, **kwargs)
|
|
53
|
+
span.set_attribute("levelfour.success", True)
|
|
54
|
+
return result
|
|
55
|
+
except Exception as e:
|
|
56
|
+
span.set_attribute("levelfour.success", False)
|
|
57
|
+
span.set_attribute("levelfour.error.type", type(e).__name__)
|
|
58
|
+
span.record_exception(e)
|
|
59
|
+
raise
|
|
60
|
+
|
|
61
|
+
import asyncio
|
|
62
|
+
if asyncio.iscoroutinefunction(func):
|
|
63
|
+
return async_wrapper # type: ignore[return-value]
|
|
64
|
+
return wrapper # type: ignore[return-value]
|
|
65
|
+
|
|
66
|
+
return decorator
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import uuid
|
|
3
|
+
|
|
4
|
+
from .base_client import BaseLevelFour
|
|
5
|
+
from .core.api_error import ApiError
|
|
6
|
+
from .exceptions import (
|
|
7
|
+
STATUS_TO_EXCEPTION,
|
|
8
|
+
AuthenticationError,
|
|
9
|
+
LevelFourError,
|
|
10
|
+
RateLimitError,
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
_VALID_PREFIXES = ("l4_live_", "l4_test_")
|
|
14
|
+
_DEFAULT_BASE_URL = "https://api.levelfour.ai"
|
|
15
|
+
_MUTATING_METHODS = ("POST", "PUT", "PATCH")
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _resolve_api_key(api_key: str | None) -> str:
|
|
19
|
+
key = api_key or os.environ.get("LEVELFOUR_API_KEY")
|
|
20
|
+
if not key:
|
|
21
|
+
raise AuthenticationError(
|
|
22
|
+
"No API key provided. Set LEVELFOUR_API_KEY environment variable or pass api_key= to the constructor."
|
|
23
|
+
)
|
|
24
|
+
if not key.startswith(_VALID_PREFIXES):
|
|
25
|
+
raise AuthenticationError(
|
|
26
|
+
f"Invalid API key format. Key must start with one of: {', '.join(_VALID_PREFIXES)}"
|
|
27
|
+
)
|
|
28
|
+
return key
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class LevelFour(BaseLevelFour):
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
*,
|
|
35
|
+
api_key: str | None = None,
|
|
36
|
+
base_url: str = _DEFAULT_BASE_URL,
|
|
37
|
+
timeout: float = 30.0,
|
|
38
|
+
max_retries: int = 2,
|
|
39
|
+
):
|
|
40
|
+
resolved_key = _resolve_api_key(api_key)
|
|
41
|
+
super().__init__(
|
|
42
|
+
base_url=base_url,
|
|
43
|
+
token=resolved_key,
|
|
44
|
+
timeout=timeout,
|
|
45
|
+
)
|
|
46
|
+
self._api_key = resolved_key
|
|
47
|
+
self._max_retries = max_retries
|
levelfour/pagination.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
from collections.abc import AsyncIterator, Iterator
|
|
2
|
+
from typing import Any, Callable, Generic, TypeVar
|
|
3
|
+
|
|
4
|
+
T = TypeVar("T")
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class PageIterator(Generic[T], Iterator[T]):
|
|
8
|
+
def __init__(self, fetch_page: Callable[[int, int], dict], page_size: int = 100):
|
|
9
|
+
self._fetch_page = fetch_page
|
|
10
|
+
self._page_size = page_size
|
|
11
|
+
self._current_page = 1
|
|
12
|
+
self._buffer: list[T] = []
|
|
13
|
+
self._index = 0
|
|
14
|
+
self._exhausted = False
|
|
15
|
+
|
|
16
|
+
def __iter__(self) -> "PageIterator[T]":
|
|
17
|
+
return self
|
|
18
|
+
|
|
19
|
+
def __next__(self) -> T:
|
|
20
|
+
if self._index < len(self._buffer):
|
|
21
|
+
item = self._buffer[self._index]
|
|
22
|
+
self._index += 1
|
|
23
|
+
return item
|
|
24
|
+
|
|
25
|
+
if self._exhausted:
|
|
26
|
+
raise StopIteration
|
|
27
|
+
|
|
28
|
+
result = self._fetch_page(self._current_page, self._page_size)
|
|
29
|
+
data = result.get("data", result)
|
|
30
|
+
items = data.get("items", [])
|
|
31
|
+
pagination = data.get("pagination", {})
|
|
32
|
+
|
|
33
|
+
if not items:
|
|
34
|
+
self._exhausted = True
|
|
35
|
+
raise StopIteration
|
|
36
|
+
|
|
37
|
+
self._buffer = items
|
|
38
|
+
self._index = 1
|
|
39
|
+
self._current_page += 1
|
|
40
|
+
|
|
41
|
+
if not pagination.get("has_next", False):
|
|
42
|
+
self._exhausted = True
|
|
43
|
+
|
|
44
|
+
return self._buffer[0]
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
class AsyncPageIterator(Generic[T], AsyncIterator[T]):
|
|
48
|
+
def __init__(self, fetch_page: Callable[[int, int], Any], page_size: int = 100):
|
|
49
|
+
self._fetch_page = fetch_page
|
|
50
|
+
self._page_size = page_size
|
|
51
|
+
self._current_page = 1
|
|
52
|
+
self._buffer: list[T] = []
|
|
53
|
+
self._index = 0
|
|
54
|
+
self._exhausted = False
|
|
55
|
+
|
|
56
|
+
def __aiter__(self) -> "AsyncPageIterator[T]":
|
|
57
|
+
return self
|
|
58
|
+
|
|
59
|
+
async def __anext__(self) -> T:
|
|
60
|
+
if self._index < len(self._buffer):
|
|
61
|
+
item = self._buffer[self._index]
|
|
62
|
+
self._index += 1
|
|
63
|
+
return item
|
|
64
|
+
|
|
65
|
+
if self._exhausted:
|
|
66
|
+
raise StopAsyncIteration
|
|
67
|
+
|
|
68
|
+
result = await self._fetch_page(self._current_page, self._page_size)
|
|
69
|
+
data = result.get("data", result)
|
|
70
|
+
items = data.get("items", [])
|
|
71
|
+
pagination = data.get("pagination", {})
|
|
72
|
+
|
|
73
|
+
if not items:
|
|
74
|
+
self._exhausted = True
|
|
75
|
+
raise StopAsyncIteration
|
|
76
|
+
|
|
77
|
+
self._buffer = items
|
|
78
|
+
self._index = 1
|
|
79
|
+
self._current_page += 1
|
|
80
|
+
|
|
81
|
+
if not pagination.get("has_next", False):
|
|
82
|
+
self._exhausted = True
|
|
83
|
+
|
|
84
|
+
return self._buffer[0]
|