bytekit-sdk 0.3.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.
- bytekit/__init__.py +8 -0
- bytekit/api/__init__.py +1 -0
- bytekit/api/account/__init__.py +1 -0
- bytekit/api/account/get_account.py +157 -0
- bytekit/api/bulk/__init__.py +1 -0
- bytekit/api/bulk/create_bulk.py +204 -0
- bytekit/api/bulk/delete_bulk.py +177 -0
- bytekit/api/bulk/get_bulk.py +157 -0
- bytekit/api/bulk/list_bulk_screenshots.py +152 -0
- bytekit/api/fetch/__init__.py +1 -0
- bytekit/api/fetch/get_fetch.py +254 -0
- bytekit/api/fetch/post_fetch.py +178 -0
- bytekit/api/fetch_bulk/__init__.py +1 -0
- bytekit/api/fetch_bulk/create_fetch_bulk.py +180 -0
- bytekit/api/fetch_bulk/get_fetch_bulk.py +157 -0
- bytekit/api/monitors/__init__.py +1 -0
- bytekit/api/monitors/create_monitor.py +180 -0
- bytekit/api/monitors/delete_monitor.py +155 -0
- bytekit/api/monitors/get_monitor.py +157 -0
- bytekit/api/monitors/list_monitor_captures.py +188 -0
- bytekit/api/monitors/list_monitors.py +193 -0
- bytekit/api/monitors/update_monitor.py +191 -0
- bytekit/api/scrape/__init__.py +1 -0
- bytekit/api/scrape/create_scrape.py +197 -0
- bytekit/api/scrape/get_scrape.py +231 -0
- bytekit/api/scrape_bulk/__init__.py +1 -0
- bytekit/api/scrape_bulk/create_scrape_bulk.py +188 -0
- bytekit/api/scrape_bulk/get_scrape_bulk.py +157 -0
- bytekit/api/screenshots/__init__.py +1 -0
- bytekit/api/screenshots/create_screenshot.py +208 -0
- bytekit/api/screenshots/get_screenshot.py +177 -0
- bytekit/api/search/__init__.py +1 -0
- bytekit/api/search/create_search.py +225 -0
- bytekit/api/sitemap/__init__.py +1 -0
- bytekit/api/sitemap/create_sitemap.py +192 -0
- bytekit/api/sitemap/get_sitemap.py +157 -0
- bytekit/api/usage/__init__.py +1 -0
- bytekit/api/usage/get_usage.py +126 -0
- bytekit/api/usage/get_usage_by_endpoint.py +215 -0
- bytekit/api/usage/get_usage_daily.py +203 -0
- bytekit/api/webhooks/__init__.py +1 -0
- bytekit/api/webhooks/list_webhook_deliveries.py +214 -0
- bytekit/api/webhooks/retry_webhook_delivery.py +165 -0
- bytekit/client.py +418 -0
- bytekit/errors.py +18 -0
- bytekit/models/__init__.py +401 -0
- bytekit/models/account_bearer_response.py +207 -0
- bytekit/models/account_bearer_response_api_key.py +134 -0
- bytekit/models/account_bearer_response_api_key_environment.py +8 -0
- bytekit/models/account_bearer_response_auto_topup.py +83 -0
- bytekit/models/account_bearer_response_plan.py +150 -0
- bytekit/models/account_bearer_response_subscription.py +85 -0
- bytekit/models/account_bearer_response_subscription_plan.py +99 -0
- bytekit/models/account_bearer_response_subscription_status.py +10 -0
- bytekit/models/account_session_response.py +164 -0
- bytekit/models/account_session_response_notifications.py +51 -0
- bytekit/models/account_session_response_plan.py +150 -0
- bytekit/models/bandwidth_balance.py +91 -0
- bytekit/models/capture_response.py +216 -0
- bytekit/models/capture_response_outcome.py +10 -0
- bytekit/models/clip.py +83 -0
- bytekit/models/cookie.py +90 -0
- bytekit/models/create_bulk_body.py +155 -0
- bytekit/models/create_bulk_body_defaults.py +423 -0
- bytekit/models/create_bulk_body_defaults_device.py +9 -0
- bytekit/models/create_bulk_body_defaults_device_scale_factor_type_1.py +9 -0
- bytekit/models/create_bulk_body_defaults_device_scale_factor_type_2_type_1.py +9 -0
- bytekit/models/create_bulk_body_defaults_device_scale_factor_type_3_type_1.py +9 -0
- bytekit/models/create_bulk_body_defaults_formats_item.py +12 -0
- bytekit/models/create_bulk_body_defaults_type.py +9 -0
- bytekit/models/create_bulk_body_defaults_viewport_type_0.py +67 -0
- bytekit/models/create_bulk_body_defaults_wait_until.py +10 -0
- bytekit/models/create_bulk_body_items_item.py +380 -0
- bytekit/models/create_bulk_body_items_item_device.py +9 -0
- bytekit/models/create_bulk_body_items_item_formats_item.py +12 -0
- bytekit/models/create_bulk_body_items_item_scenario.py +10 -0
- bytekit/models/create_bulk_body_items_item_type.py +9 -0
- bytekit/models/create_bulk_body_items_item_viewport_type_0.py +67 -0
- bytekit/models/create_bulk_body_items_item_wait_until.py +10 -0
- bytekit/models/create_bulk_response_202.py +164 -0
- bytekit/models/create_bulk_response_202_status.py +10 -0
- bytekit/models/create_fetch_bulk_body.py +161 -0
- bytekit/models/create_fetch_bulk_body_defaults.py +91 -0
- bytekit/models/create_fetch_bulk_body_defaults_format.py +9 -0
- bytekit/models/create_fetch_bulk_body_metadata.py +44 -0
- bytekit/models/create_fetch_bulk_body_urls_item_type_1.py +102 -0
- bytekit/models/create_fetch_bulk_body_urls_item_type_1_format.py +9 -0
- bytekit/models/create_fetch_bulk_response_202.py +44 -0
- bytekit/models/create_scrape_bulk_body.py +179 -0
- bytekit/models/create_scrape_bulk_body_custom.py +52 -0
- bytekit/models/create_scrape_bulk_body_defaults.py +167 -0
- bytekit/models/create_scrape_bulk_body_defaults_formats_item.py +12 -0
- bytekit/models/create_scrape_bulk_body_defaults_headers.py +44 -0
- bytekit/models/create_scrape_bulk_body_items_item.py +226 -0
- bytekit/models/create_scrape_bulk_body_items_item_custom.py +50 -0
- bytekit/models/create_scrape_bulk_body_items_item_formats_item.py +12 -0
- bytekit/models/create_scrape_bulk_body_items_item_headers.py +44 -0
- bytekit/models/create_scrape_bulk_body_items_item_metadata.py +44 -0
- bytekit/models/create_scrape_bulk_response_202.py +166 -0
- bytekit/models/create_scrape_bulk_response_202_status.py +10 -0
- bytekit/models/create_search_body.py +140 -0
- bytekit/models/create_search_body_date_range.py +13 -0
- bytekit/models/create_search_body_type.py +10 -0
- bytekit/models/create_search_response_200.py +119 -0
- bytekit/models/create_search_response_200_results_item.py +161 -0
- bytekit/models/create_search_response_200_results_item_image.py +113 -0
- bytekit/models/create_search_response_502.py +46 -0
- bytekit/models/create_search_response_502_error.py +8 -0
- bytekit/models/credit_balance.py +91 -0
- bytekit/models/delete_bulk_response_200.py +164 -0
- bytekit/models/delete_bulk_response_200_status.py +11 -0
- bytekit/models/error.py +83 -0
- bytekit/models/error_error.py +100 -0
- bytekit/models/error_status.py +8 -0
- bytekit/models/fetch_request.py +276 -0
- bytekit/models/fetch_request_cache_ttl_type_1.py +8 -0
- bytekit/models/fetch_request_custom.py +48 -0
- bytekit/models/fetch_request_format.py +9 -0
- bytekit/models/fetch_request_markdown_images.py +11 -0
- bytekit/models/fetch_request_markdown_links.py +11 -0
- bytekit/models/fetch_request_markdown_mode.py +10 -0
- bytekit/models/get_bulk_response_200.py +164 -0
- bytekit/models/get_bulk_response_200_status.py +11 -0
- bytekit/models/get_fetch_bulk_response_200.py +44 -0
- bytekit/models/get_fetch_cache_ttl_type_1.py +8 -0
- bytekit/models/get_fetch_format.py +9 -0
- bytekit/models/get_scrape_bulk_response_200.py +164 -0
- bytekit/models/get_scrape_bulk_response_200_status.py +11 -0
- bytekit/models/get_usage_by_endpoint_response_200.py +114 -0
- bytekit/models/get_usage_by_endpoint_response_200_rows_item.py +90 -0
- bytekit/models/get_usage_by_endpoint_response_200_rows_item_endpoint.py +17 -0
- bytekit/models/get_usage_by_endpoint_response_200_totals.py +67 -0
- bytekit/models/get_usage_daily_response_200.py +97 -0
- bytekit/models/get_usage_daily_response_200_data_item.py +219 -0
- bytekit/models/get_usage_response_200.py +344 -0
- bytekit/models/get_usage_response_200_plan_type.py +10 -0
- bytekit/models/list_bulk_screenshots_response_200.py +44 -0
- bytekit/models/list_monitor_captures_response_200.py +101 -0
- bytekit/models/list_monitors_response_200.py +101 -0
- bytekit/models/list_monitors_status.py +12 -0
- bytekit/models/list_webhook_deliveries_response_200.py +101 -0
- bytekit/models/list_webhook_deliveries_status.py +11 -0
- bytekit/models/metadata.py +44 -0
- bytekit/models/monitor_create_request.py +219 -0
- bytekit/models/monitor_create_request_interval_type.py +11 -0
- bytekit/models/monitor_create_request_notify_on.py +9 -0
- bytekit/models/monitor_create_request_options.py +44 -0
- bytekit/models/monitor_create_request_scrape_options.py +44 -0
- bytekit/models/monitor_create_request_type.py +9 -0
- bytekit/models/monitor_create_request_webhook_headers.py +44 -0
- bytekit/models/monitor_create_response.py +423 -0
- bytekit/models/monitor_response.py +411 -0
- bytekit/models/monitor_response_interval_type.py +11 -0
- bytekit/models/monitor_response_metadata_type_0.py +44 -0
- bytekit/models/monitor_response_status.py +12 -0
- bytekit/models/monitor_response_type.py +9 -0
- bytekit/models/monitor_response_webhook_headers_type_0.py +44 -0
- bytekit/models/monitor_update_request.py +223 -0
- bytekit/models/monitor_update_request_interval_type.py +11 -0
- bytekit/models/monitor_update_request_notify_on.py +9 -0
- bytekit/models/monitor_update_request_options.py +44 -0
- bytekit/models/monitor_update_request_scrape_options.py +44 -0
- bytekit/models/monitor_update_request_status.py +9 -0
- bytekit/models/monitor_update_request_webhook_headers.py +44 -0
- bytekit/models/scrape_error_envelope.py +113 -0
- bytekit/models/scrape_error_envelope_error.py +93 -0
- bytekit/models/scrape_error_envelope_error_code.py +18 -0
- bytekit/models/scrape_error_envelope_schema_version.py +8 -0
- bytekit/models/scrape_error_envelope_status.py +8 -0
- bytekit/models/scrape_formats.py +183 -0
- bytekit/models/scrape_markdown_tokens.py +75 -0
- bytekit/models/scrape_metadata.py +262 -0
- bytekit/models/scrape_queued_envelope.py +124 -0
- bytekit/models/scrape_queued_envelope_schema_version.py +8 -0
- bytekit/models/scrape_queued_envelope_status.py +8 -0
- bytekit/models/scrape_request.py +485 -0
- bytekit/models/scrape_request_cache_ttl_type_1.py +8 -0
- bytekit/models/scrape_request_custom.py +48 -0
- bytekit/models/scrape_request_events_item.py +10 -0
- bytekit/models/scrape_request_formats_item.py +12 -0
- bytekit/models/scrape_request_headers.py +44 -0
- bytekit/models/scrape_request_markdown_images.py +11 -0
- bytekit/models/scrape_request_markdown_links.py +11 -0
- bytekit/models/scrape_request_markdown_mode.py +10 -0
- bytekit/models/scrape_request_token_encoding.py +9 -0
- bytekit/models/scrape_request_x_internal_scrape_path.py +8 -0
- bytekit/models/scrape_scored_image.py +128 -0
- bytekit/models/scrape_scored_link.py +94 -0
- bytekit/models/scrape_stats.py +75 -0
- bytekit/models/scrape_success_envelope.py +345 -0
- bytekit/models/scrape_success_envelope_cache.py +10 -0
- bytekit/models/scrape_success_envelope_custom.py +48 -0
- bytekit/models/scrape_success_envelope_schema_version.py +8 -0
- bytekit/models/scrape_success_envelope_status.py +8 -0
- bytekit/models/scrape_table.py +96 -0
- bytekit/models/scrape_table_kind.py +10 -0
- bytekit/models/scrape_warning.py +88 -0
- bytekit/models/scrape_warning_code.py +17 -0
- bytekit/models/screenshot_request.py +445 -0
- bytekit/models/screenshot_request_device.py +9 -0
- bytekit/models/screenshot_request_device_scale_factor_type_1.py +9 -0
- bytekit/models/screenshot_request_device_scale_factor_type_2_type_1.py +9 -0
- bytekit/models/screenshot_request_device_scale_factor_type_3_type_1.py +9 -0
- bytekit/models/screenshot_request_format.py +9 -0
- bytekit/models/screenshot_request_headers.py +44 -0
- bytekit/models/screenshot_request_wait_until.py +10 -0
- bytekit/models/screenshot_response.py +264 -0
- bytekit/models/screenshot_response_status.py +11 -0
- bytekit/models/sitemap_request.py +210 -0
- bytekit/models/sitemap_request_process_type_0.py +90 -0
- bytekit/models/sitemap_request_process_type_0_options.py +44 -0
- bytekit/models/sitemap_request_strategy.py +10 -0
- bytekit/models/sitemap_response.py +418 -0
- bytekit/models/sitemap_response_cache.py +10 -0
- bytekit/models/sitemap_response_metadata_type_0.py +44 -0
- bytekit/models/sitemap_response_process_type_0.py +44 -0
- bytekit/models/sitemap_response_sources_type_0.py +44 -0
- bytekit/models/sitemap_response_status.py +11 -0
- bytekit/models/viewport.py +67 -0
- bytekit/models/webhook_delivery_response.py +270 -0
- bytekit/models/webhook_delivery_response_event_type.py +14 -0
- bytekit/models/webhook_delivery_response_status.py +11 -0
- bytekit/py.typed +0 -0
- bytekit/types.py +54 -0
- bytekit_sdk-0.3.0.dist-info/METADATA +141 -0
- bytekit_sdk-0.3.0.dist-info/RECORD +227 -0
- bytekit_sdk-0.3.0.dist-info/WHEEL +4 -0
bytekit/client.py
ADDED
|
@@ -0,0 +1,418 @@
|
|
|
1
|
+
import ssl
|
|
2
|
+
from typing import Any, Optional, Union
|
|
3
|
+
|
|
4
|
+
import httpx
|
|
5
|
+
from attrs import define, evolve, field
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@define
|
|
9
|
+
class Client:
|
|
10
|
+
"""A class for keeping track of data related to the API
|
|
11
|
+
|
|
12
|
+
The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
|
|
13
|
+
|
|
14
|
+
``base_url``: The base URL for the API, all requests are made to a relative path to this URL
|
|
15
|
+
|
|
16
|
+
``cookies``: A dictionary of cookies to be sent with every request
|
|
17
|
+
|
|
18
|
+
``headers``: A dictionary of headers to be sent with every request
|
|
19
|
+
|
|
20
|
+
``timeout``: The maximum amount of a time a request can take. API functions will raise
|
|
21
|
+
httpx.TimeoutException if this is exceeded.
|
|
22
|
+
|
|
23
|
+
``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
|
|
24
|
+
but can be set to False for testing purposes.
|
|
25
|
+
|
|
26
|
+
``follow_redirects``: Whether or not to follow redirects. Default value is False.
|
|
27
|
+
|
|
28
|
+
``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
Attributes:
|
|
32
|
+
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
|
|
33
|
+
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
|
|
34
|
+
argument to the constructor.
|
|
35
|
+
Explicit constructor arguments always win over these defaults (explicit arg > default).
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
raise_on_unexpected_status: bool = field(default=True, kw_only=True) # BYTEKIT-DEFAULTS
|
|
39
|
+
_base_url: str = field(default="https://api.bytekit.com", kw_only=True, alias="base_url")
|
|
40
|
+
_cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
|
|
41
|
+
_headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
|
|
42
|
+
_timeout: Optional[httpx.Timeout] = field(
|
|
43
|
+
default=httpx.Timeout(120.0), kw_only=True, alias="timeout"
|
|
44
|
+
)
|
|
45
|
+
_verify_ssl: Union[str, bool, ssl.SSLContext] = field(
|
|
46
|
+
default=True, kw_only=True, alias="verify_ssl"
|
|
47
|
+
)
|
|
48
|
+
_follow_redirects: bool = field(
|
|
49
|
+
default=False, kw_only=True, alias="follow_redirects"
|
|
50
|
+
)
|
|
51
|
+
_httpx_args: dict[str, Any] = field(factory=dict, kw_only=True, alias="httpx_args")
|
|
52
|
+
_client: Optional[httpx.Client] = field(default=None, init=False)
|
|
53
|
+
_async_client: Optional[httpx.AsyncClient] = field(default=None, init=False)
|
|
54
|
+
|
|
55
|
+
def with_headers(self, headers: dict[str, str]) -> "Client":
|
|
56
|
+
"""Get a new client matching this one with additional headers"""
|
|
57
|
+
if self._client is not None:
|
|
58
|
+
self._client.headers.update(headers)
|
|
59
|
+
if self._async_client is not None:
|
|
60
|
+
self._async_client.headers.update(headers)
|
|
61
|
+
return evolve(self, headers={**self._headers, **headers})
|
|
62
|
+
|
|
63
|
+
def with_cookies(self, cookies: dict[str, str]) -> "Client":
|
|
64
|
+
"""Get a new client matching this one with additional cookies"""
|
|
65
|
+
if self._client is not None:
|
|
66
|
+
self._client.cookies.update(cookies)
|
|
67
|
+
if self._async_client is not None:
|
|
68
|
+
self._async_client.cookies.update(cookies)
|
|
69
|
+
return evolve(self, cookies={**self._cookies, **cookies})
|
|
70
|
+
|
|
71
|
+
def with_timeout(self, timeout: httpx.Timeout) -> "Client":
|
|
72
|
+
"""Get a new client matching this one with a new timeout (in seconds)"""
|
|
73
|
+
if self._client is not None:
|
|
74
|
+
self._client.timeout = timeout
|
|
75
|
+
if self._async_client is not None:
|
|
76
|
+
self._async_client.timeout = timeout
|
|
77
|
+
return evolve(self, timeout=timeout)
|
|
78
|
+
|
|
79
|
+
def set_httpx_client(self, client: httpx.Client) -> "Client":
|
|
80
|
+
"""Manually set the underlying httpx.Client
|
|
81
|
+
|
|
82
|
+
**NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
|
|
83
|
+
"""
|
|
84
|
+
self._client = client
|
|
85
|
+
return self
|
|
86
|
+
|
|
87
|
+
def get_httpx_client(self) -> httpx.Client:
|
|
88
|
+
"""Get the underlying httpx.Client, constructing a new one if not previously set"""
|
|
89
|
+
if self._client is None:
|
|
90
|
+
self._client = httpx.Client(
|
|
91
|
+
base_url=self._base_url,
|
|
92
|
+
cookies=self._cookies,
|
|
93
|
+
headers=self._headers,
|
|
94
|
+
timeout=self._timeout,
|
|
95
|
+
verify=self._verify_ssl,
|
|
96
|
+
follow_redirects=self._follow_redirects,
|
|
97
|
+
**self._httpx_args,
|
|
98
|
+
)
|
|
99
|
+
return self._client
|
|
100
|
+
|
|
101
|
+
def __enter__(self) -> "Client":
|
|
102
|
+
"""Enter a context manager for self.client—you cannot enter twice (see httpx docs)"""
|
|
103
|
+
self.get_httpx_client().__enter__()
|
|
104
|
+
return self
|
|
105
|
+
|
|
106
|
+
def __exit__(self, *args: Any, **kwargs: Any) -> None:
|
|
107
|
+
"""Exit a context manager for internal httpx.Client (see httpx docs)"""
|
|
108
|
+
self.get_httpx_client().__exit__(*args, **kwargs)
|
|
109
|
+
|
|
110
|
+
def set_async_httpx_client(self, async_client: httpx.AsyncClient) -> "Client":
|
|
111
|
+
"""Manually the underlying httpx.AsyncClient
|
|
112
|
+
|
|
113
|
+
**NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
|
|
114
|
+
"""
|
|
115
|
+
self._async_client = async_client
|
|
116
|
+
return self
|
|
117
|
+
|
|
118
|
+
def get_async_httpx_client(self) -> httpx.AsyncClient:
|
|
119
|
+
"""Get the underlying httpx.AsyncClient, constructing a new one if not previously set"""
|
|
120
|
+
if self._async_client is None:
|
|
121
|
+
self._async_client = httpx.AsyncClient(
|
|
122
|
+
base_url=self._base_url,
|
|
123
|
+
cookies=self._cookies,
|
|
124
|
+
headers=self._headers,
|
|
125
|
+
timeout=self._timeout,
|
|
126
|
+
verify=self._verify_ssl,
|
|
127
|
+
follow_redirects=self._follow_redirects,
|
|
128
|
+
**self._httpx_args,
|
|
129
|
+
)
|
|
130
|
+
return self._async_client
|
|
131
|
+
|
|
132
|
+
async def __aenter__(self) -> "Client":
|
|
133
|
+
"""Enter a context manager for underlying httpx.AsyncClient—you cannot enter twice (see httpx docs)"""
|
|
134
|
+
await self.get_async_httpx_client().__aenter__()
|
|
135
|
+
return self
|
|
136
|
+
|
|
137
|
+
async def __aexit__(self, *args: Any, **kwargs: Any) -> None:
|
|
138
|
+
"""Exit a context manager for underlying httpx.AsyncClient (see httpx docs)"""
|
|
139
|
+
await self.get_async_httpx_client().__aexit__(*args, **kwargs)
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
@define
|
|
143
|
+
class AuthenticatedClient:
|
|
144
|
+
"""A Client which has been authenticated for use on secured endpoints
|
|
145
|
+
|
|
146
|
+
The following are accepted as keyword arguments and will be used to construct httpx Clients internally:
|
|
147
|
+
|
|
148
|
+
``base_url``: The base URL for the API, all requests are made to a relative path to this URL
|
|
149
|
+
|
|
150
|
+
``cookies``: A dictionary of cookies to be sent with every request
|
|
151
|
+
|
|
152
|
+
``headers``: A dictionary of headers to be sent with every request
|
|
153
|
+
|
|
154
|
+
``timeout``: The maximum amount of a time a request can take. API functions will raise
|
|
155
|
+
httpx.TimeoutException if this is exceeded.
|
|
156
|
+
|
|
157
|
+
``verify_ssl``: Whether or not to verify the SSL certificate of the API server. This should be True in production,
|
|
158
|
+
but can be set to False for testing purposes.
|
|
159
|
+
|
|
160
|
+
``follow_redirects``: Whether or not to follow redirects. Default value is False.
|
|
161
|
+
|
|
162
|
+
``httpx_args``: A dictionary of additional arguments to be passed to the ``httpx.Client`` and ``httpx.AsyncClient`` constructor.
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
Attributes:
|
|
166
|
+
raise_on_unexpected_status: Whether or not to raise an errors.UnexpectedStatus if the API returns a
|
|
167
|
+
status code that was not documented in the source OpenAPI document. Can also be provided as a keyword
|
|
168
|
+
argument to the constructor.
|
|
169
|
+
Explicit constructor arguments always win over these defaults (explicit arg > default).
|
|
170
|
+
token: The token to use for authentication
|
|
171
|
+
prefix: The prefix to use for the Authorization header
|
|
172
|
+
auth_header_name: The name of the Authorization header
|
|
173
|
+
"""
|
|
174
|
+
|
|
175
|
+
raise_on_unexpected_status: bool = field(default=True, kw_only=True) # BYTEKIT-DEFAULTS
|
|
176
|
+
_base_url: str = field(default="https://api.bytekit.com", kw_only=True, alias="base_url")
|
|
177
|
+
_cookies: dict[str, str] = field(factory=dict, kw_only=True, alias="cookies")
|
|
178
|
+
_headers: dict[str, str] = field(factory=dict, kw_only=True, alias="headers")
|
|
179
|
+
_timeout: Optional[httpx.Timeout] = field(
|
|
180
|
+
default=httpx.Timeout(120.0), kw_only=True, alias="timeout"
|
|
181
|
+
)
|
|
182
|
+
_verify_ssl: Union[str, bool, ssl.SSLContext] = field(
|
|
183
|
+
default=True, kw_only=True, alias="verify_ssl"
|
|
184
|
+
)
|
|
185
|
+
_follow_redirects: bool = field(
|
|
186
|
+
default=False, kw_only=True, alias="follow_redirects"
|
|
187
|
+
)
|
|
188
|
+
_httpx_args: dict[str, Any] = field(factory=dict, kw_only=True, alias="httpx_args")
|
|
189
|
+
_client: Optional[httpx.Client] = field(default=None, init=False)
|
|
190
|
+
_async_client: Optional[httpx.AsyncClient] = field(default=None, init=False)
|
|
191
|
+
|
|
192
|
+
token: str
|
|
193
|
+
prefix: str = "Bearer"
|
|
194
|
+
auth_header_name: str = "Authorization"
|
|
195
|
+
|
|
196
|
+
def with_headers(self, headers: dict[str, str]) -> "AuthenticatedClient":
|
|
197
|
+
"""Get a new client matching this one with additional headers"""
|
|
198
|
+
if self._client is not None:
|
|
199
|
+
self._client.headers.update(headers)
|
|
200
|
+
if self._async_client is not None:
|
|
201
|
+
self._async_client.headers.update(headers)
|
|
202
|
+
return evolve(self, headers={**self._headers, **headers})
|
|
203
|
+
|
|
204
|
+
def with_cookies(self, cookies: dict[str, str]) -> "AuthenticatedClient":
|
|
205
|
+
"""Get a new client matching this one with additional cookies"""
|
|
206
|
+
if self._client is not None:
|
|
207
|
+
self._client.cookies.update(cookies)
|
|
208
|
+
if self._async_client is not None:
|
|
209
|
+
self._async_client.cookies.update(cookies)
|
|
210
|
+
return evolve(self, cookies={**self._cookies, **cookies})
|
|
211
|
+
|
|
212
|
+
def with_timeout(self, timeout: httpx.Timeout) -> "AuthenticatedClient":
|
|
213
|
+
"""Get a new client matching this one with a new timeout (in seconds)"""
|
|
214
|
+
if self._client is not None:
|
|
215
|
+
self._client.timeout = timeout
|
|
216
|
+
if self._async_client is not None:
|
|
217
|
+
self._async_client.timeout = timeout
|
|
218
|
+
return evolve(self, timeout=timeout)
|
|
219
|
+
|
|
220
|
+
def set_httpx_client(self, client: httpx.Client) -> "AuthenticatedClient":
|
|
221
|
+
"""Manually set the underlying httpx.Client
|
|
222
|
+
|
|
223
|
+
**NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
|
|
224
|
+
"""
|
|
225
|
+
self._client = client
|
|
226
|
+
return self
|
|
227
|
+
|
|
228
|
+
def get_httpx_client(self) -> httpx.Client:
|
|
229
|
+
"""Get the underlying httpx.Client, constructing a new one if not previously set"""
|
|
230
|
+
if self._client is None:
|
|
231
|
+
self._headers[self.auth_header_name] = (
|
|
232
|
+
f"{self.prefix} {self.token}" if self.prefix else self.token
|
|
233
|
+
)
|
|
234
|
+
self._client = httpx.Client(
|
|
235
|
+
base_url=self._base_url,
|
|
236
|
+
cookies=self._cookies,
|
|
237
|
+
headers=self._headers,
|
|
238
|
+
timeout=self._timeout,
|
|
239
|
+
verify=self._verify_ssl,
|
|
240
|
+
follow_redirects=self._follow_redirects,
|
|
241
|
+
**self._httpx_args,
|
|
242
|
+
)
|
|
243
|
+
return self._client
|
|
244
|
+
|
|
245
|
+
def __enter__(self) -> "AuthenticatedClient":
|
|
246
|
+
"""Enter a context manager for self.client—you cannot enter twice (see httpx docs)"""
|
|
247
|
+
self.get_httpx_client().__enter__()
|
|
248
|
+
return self
|
|
249
|
+
|
|
250
|
+
def __exit__(self, *args: Any, **kwargs: Any) -> None:
|
|
251
|
+
"""Exit a context manager for internal httpx.Client (see httpx docs)"""
|
|
252
|
+
self.get_httpx_client().__exit__(*args, **kwargs)
|
|
253
|
+
|
|
254
|
+
def set_async_httpx_client(
|
|
255
|
+
self, async_client: httpx.AsyncClient
|
|
256
|
+
) -> "AuthenticatedClient":
|
|
257
|
+
"""Manually the underlying httpx.AsyncClient
|
|
258
|
+
|
|
259
|
+
**NOTE**: This will override any other settings on the client, including cookies, headers, and timeout.
|
|
260
|
+
"""
|
|
261
|
+
self._async_client = async_client
|
|
262
|
+
return self
|
|
263
|
+
|
|
264
|
+
def get_async_httpx_client(self) -> httpx.AsyncClient:
|
|
265
|
+
"""Get the underlying httpx.AsyncClient, constructing a new one if not previously set"""
|
|
266
|
+
if self._async_client is None:
|
|
267
|
+
self._headers[self.auth_header_name] = (
|
|
268
|
+
f"{self.prefix} {self.token}" if self.prefix else self.token
|
|
269
|
+
)
|
|
270
|
+
self._async_client = httpx.AsyncClient(
|
|
271
|
+
base_url=self._base_url,
|
|
272
|
+
cookies=self._cookies,
|
|
273
|
+
headers=self._headers,
|
|
274
|
+
timeout=self._timeout,
|
|
275
|
+
verify=self._verify_ssl,
|
|
276
|
+
follow_redirects=self._follow_redirects,
|
|
277
|
+
**self._httpx_args,
|
|
278
|
+
)
|
|
279
|
+
return self._async_client
|
|
280
|
+
|
|
281
|
+
async def __aenter__(self) -> "AuthenticatedClient":
|
|
282
|
+
"""Enter a context manager for underlying httpx.AsyncClient—you cannot enter twice (see httpx docs)"""
|
|
283
|
+
await self.get_async_httpx_client().__aenter__()
|
|
284
|
+
return self
|
|
285
|
+
|
|
286
|
+
async def __aexit__(self, *args: Any, **kwargs: Any) -> None:
|
|
287
|
+
"""Exit a context manager for underlying httpx.AsyncClient (see httpx docs)"""
|
|
288
|
+
await self.get_async_httpx_client().__aexit__(*args, **kwargs)
|
|
289
|
+
|
|
290
|
+
|
|
291
|
+
# BYTEKIT-HANDWRITTEN: search convenience wrapper (injected post-generation)
|
|
292
|
+
#
|
|
293
|
+
# This fragment is APPENDED to the generated src/bytekit/client.py by
|
|
294
|
+
# scripts/inject-handwritten.py after every regeneration. It lives here (outside
|
|
295
|
+
# src/bytekit/) so `openapi-python-client generate --overwrite`, which deletes and
|
|
296
|
+
# recreates the entire src/bytekit/ package, can never delete it.
|
|
297
|
+
#
|
|
298
|
+
# It attaches a `search()` convenience method onto the generated AuthenticatedClient
|
|
299
|
+
# class (defined earlier in client.py, so it is in scope when this fragment runs).
|
|
300
|
+
# Keep the body behaviorally identical to what tests/test_search.py exercises.
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def _search_unexpected(response: "httpx.Response") -> "UnexpectedStatus":
|
|
304
|
+
"""Build the SDK's typed error for a non-200 search response.
|
|
305
|
+
|
|
306
|
+
Safe against non-JSON bodies: a documented status served an HTML/LB/Cloudflare
|
|
307
|
+
page (e.g. a 502) surfaces as ``UnexpectedStatus(status, raw_body)`` rather than
|
|
308
|
+
a bare ``json.JSONDecodeError``. When the body is the documented ``Error``
|
|
309
|
+
envelope (``{"error": {"code", "message"}}``) — or a flat ``{"code", "message"}``
|
|
310
|
+
dict — those fields are attached to the error. Every other shape (including the
|
|
311
|
+
masked-provider 502 ``{"error": "search_provider_error"}`` where ``error`` is a
|
|
312
|
+
STRING) falls through to a plain typed error with ``code``/``message`` = ``None``,
|
|
313
|
+
so the upstream provider's identity is never surfaced.
|
|
314
|
+
"""
|
|
315
|
+
import json as _json
|
|
316
|
+
|
|
317
|
+
from . import errors
|
|
318
|
+
|
|
319
|
+
try:
|
|
320
|
+
body = response.json()
|
|
321
|
+
except (ValueError, _json.JSONDecodeError):
|
|
322
|
+
# Non-JSON documented-status body (HTML error page, load-balancer text).
|
|
323
|
+
return errors.UnexpectedStatus(response.status_code, response.content)
|
|
324
|
+
|
|
325
|
+
if isinstance(body, dict):
|
|
326
|
+
inner = body.get("error")
|
|
327
|
+
if isinstance(inner, dict) and ("code" in inner or "message" in inner):
|
|
328
|
+
return errors.UnexpectedStatus(
|
|
329
|
+
response.status_code,
|
|
330
|
+
response.content,
|
|
331
|
+
code=inner.get("code"),
|
|
332
|
+
message=inner.get("message"),
|
|
333
|
+
)
|
|
334
|
+
if "code" in body or "message" in body:
|
|
335
|
+
return errors.UnexpectedStatus(
|
|
336
|
+
response.status_code,
|
|
337
|
+
response.content,
|
|
338
|
+
code=body.get("code"),
|
|
339
|
+
message=body.get("message"),
|
|
340
|
+
)
|
|
341
|
+
|
|
342
|
+
return errors.UnexpectedStatus(response.status_code, response.content)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
def _search(
|
|
346
|
+
self,
|
|
347
|
+
query: str,
|
|
348
|
+
*,
|
|
349
|
+
type: str | None = None,
|
|
350
|
+
limit: int | None = None,
|
|
351
|
+
country: str | None = None,
|
|
352
|
+
language: str | None = None,
|
|
353
|
+
date_range: str | None = None,
|
|
354
|
+
) -> "CreateSearchResponse200":
|
|
355
|
+
"""Run a web search and return ranked organic results.
|
|
356
|
+
|
|
357
|
+
Convenience wrapper over the generated ``create_search`` operation: it builds
|
|
358
|
+
the request body, issues the request, and returns the parsed 200 model. Every
|
|
359
|
+
non-200 response is surfaced as ``errors.UnexpectedStatus`` carrying the raw
|
|
360
|
+
status code and (vendor-masked) response body — never a silent ``None`` and
|
|
361
|
+
never a bare ``json.JSONDecodeError`` on a non-JSON error page. When the server
|
|
362
|
+
returns the documented ``Error`` envelope, the error's ``code`` and ``message``
|
|
363
|
+
are preserved on the raised exception; the upstream provider's identity is never
|
|
364
|
+
exposed.
|
|
365
|
+
|
|
366
|
+
Args:
|
|
367
|
+
query: The search query.
|
|
368
|
+
type: Which search index to query: ``web`` (default), ``news``, or
|
|
369
|
+
``images``. Omitted from the request when ``None`` so the server
|
|
370
|
+
applies its own default.
|
|
371
|
+
limit: Max number of organic results (1-100, default 10 server-side).
|
|
372
|
+
country: Two-letter country code to localize results (e.g. ``us``).
|
|
373
|
+
language: Two-letter ISO-639-1 language code (e.g. ``en``).
|
|
374
|
+
date_range: Restrict results to a recency window: ``any`` (default),
|
|
375
|
+
``hour``, ``day``, ``week``, ``month``, or ``year``. Omitted from
|
|
376
|
+
the request when ``None``.
|
|
377
|
+
|
|
378
|
+
Returns:
|
|
379
|
+
The parsed 200 response model.
|
|
380
|
+
|
|
381
|
+
Raises:
|
|
382
|
+
errors.UnexpectedStatus: On any non-200 response. ``.status_code`` and
|
|
383
|
+
``.content`` are always populated; ``.code``/``.message`` are populated
|
|
384
|
+
when the body is the documented ``Error`` envelope.
|
|
385
|
+
"""
|
|
386
|
+
# Imported lazily: the generated operation imports this module, so a
|
|
387
|
+
# top-level import would create a circular dependency.
|
|
388
|
+
from .api.search import create_search
|
|
389
|
+
from .models.create_search_body import CreateSearchBody
|
|
390
|
+
from .models.create_search_body_date_range import CreateSearchBodyDateRange
|
|
391
|
+
from .models.create_search_body_type import CreateSearchBodyType
|
|
392
|
+
from .models.create_search_response_200 import CreateSearchResponse200
|
|
393
|
+
from .types import UNSET
|
|
394
|
+
|
|
395
|
+
# Pass UNSET (not the model's default) for absent type/date_range so the
|
|
396
|
+
# wire body stays minimal and an omitted filter is truly omitted, matching
|
|
397
|
+
# the optional-field contract across the SDKs.
|
|
398
|
+
body = CreateSearchBody(
|
|
399
|
+
query=query,
|
|
400
|
+
type_=CreateSearchBodyType(type) if type is not None else UNSET,
|
|
401
|
+
limit=limit if limit is not None else UNSET,
|
|
402
|
+
country=country if country is not None else UNSET,
|
|
403
|
+
language=language if language is not None else UNSET,
|
|
404
|
+
date_range=(CreateSearchBodyDateRange(date_range) if date_range is not None else UNSET),
|
|
405
|
+
)
|
|
406
|
+
|
|
407
|
+
# Own request -> parse directly rather than delegating to sync_detailed, whose
|
|
408
|
+
# generated _parse_response calls response.json() unconditionally on documented
|
|
409
|
+
# statuses and would raise json.JSONDecodeError inside the SDK on an HTML error
|
|
410
|
+
# page before the wrapper could inspect the response.
|
|
411
|
+
kwargs = create_search._get_kwargs(body=body)
|
|
412
|
+
response = self.get_httpx_client().request(**kwargs)
|
|
413
|
+
if response.status_code == 200:
|
|
414
|
+
return CreateSearchResponse200.from_dict(response.json())
|
|
415
|
+
raise _search_unexpected(response)
|
|
416
|
+
|
|
417
|
+
|
|
418
|
+
AuthenticatedClient.search = _search
|
bytekit/errors.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Contains shared errors types that can be raised from API functions"""
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class UnexpectedStatus(Exception):
|
|
5
|
+
"""Raised by api functions when the response status an undocumented status and Client.raise_on_unexpected_status is True"""
|
|
6
|
+
|
|
7
|
+
def __init__(self, status_code: int, content: bytes, *, code=None, message=None): # BYTEKIT-ERR-ENRICH
|
|
8
|
+
self.status_code = status_code
|
|
9
|
+
self.content = content
|
|
10
|
+
self.code = code
|
|
11
|
+
self.message = message
|
|
12
|
+
|
|
13
|
+
super().__init__(
|
|
14
|
+
f"Unexpected status code: {status_code}\n\nResponse content:\n{content.decode(errors='ignore')}"
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
__all__ = ["UnexpectedStatus"]
|