|
| 1 | +# File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +import httpx |
| 6 | + |
| 7 | +from ..._types import Body, Omit, Query, Headers, NotGiven, omit, not_given |
| 8 | +from ..._utils import maybe_transform, async_maybe_transform |
| 9 | +from ..._compat import cached_property |
| 10 | +from ..._resource import SyncAPIResource, AsyncAPIResource |
| 11 | +from ..._response import ( |
| 12 | + to_raw_response_wrapper, |
| 13 | + to_streamed_response_wrapper, |
| 14 | + async_to_raw_response_wrapper, |
| 15 | + async_to_streamed_response_wrapper, |
| 16 | +) |
| 17 | +from ..._base_client import make_request_options |
| 18 | +from ...types.browsers import playwright_execute_params |
| 19 | +from ...types.browsers.playwright_execute_response import PlaywrightExecuteResponse |
| 20 | + |
| 21 | +__all__ = ["PlaywrightResource", "AsyncPlaywrightResource"] |
| 22 | + |
| 23 | + |
| 24 | +class PlaywrightResource(SyncAPIResource): |
| 25 | + @cached_property |
| 26 | + def with_raw_response(self) -> PlaywrightResourceWithRawResponse: |
| 27 | + """ |
| 28 | + This property can be used as a prefix for any HTTP method call to return |
| 29 | + the raw response object instead of the parsed content. |
| 30 | +
|
| 31 | + For more information, see https://www.github.com/onkernel/kernel-python-sdk#accessing-raw-response-data-eg-headers |
| 32 | + """ |
| 33 | + return PlaywrightResourceWithRawResponse(self) |
| 34 | + |
| 35 | + @cached_property |
| 36 | + def with_streaming_response(self) -> PlaywrightResourceWithStreamingResponse: |
| 37 | + """ |
| 38 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 39 | +
|
| 40 | + For more information, see https://www.github.com/onkernel/kernel-python-sdk#with_streaming_response |
| 41 | + """ |
| 42 | + return PlaywrightResourceWithStreamingResponse(self) |
| 43 | + |
| 44 | + def execute( |
| 45 | + self, |
| 46 | + id: str, |
| 47 | + *, |
| 48 | + code: str, |
| 49 | + timeout_sec: int | Omit = omit, |
| 50 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 51 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 52 | + extra_headers: Headers | None = None, |
| 53 | + extra_query: Query | None = None, |
| 54 | + extra_body: Body | None = None, |
| 55 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 56 | + ) -> PlaywrightExecuteResponse: |
| 57 | + """ |
| 58 | + Execute arbitrary Playwright code in a fresh execution context against the |
| 59 | + browser. The code runs in the same VM as the browser, minimizing latency and |
| 60 | + maximizing throughput. It has access to 'page', 'context', and 'browser' |
| 61 | + variables. It can `return` a value, and this value is returned in the response. |
| 62 | +
|
| 63 | + Args: |
| 64 | + code: TypeScript/JavaScript code to execute. The code has access to 'page', 'context', |
| 65 | + and 'browser' variables. It runs within a function, so you can use a return |
| 66 | + statement at the end to return a value. This value is returned as the `result` |
| 67 | + property in the response. Example: "await page.goto('https://example.com'); |
| 68 | + return await page.title();" |
| 69 | +
|
| 70 | + timeout_sec: Maximum execution time in seconds. Default is 60. |
| 71 | +
|
| 72 | + extra_headers: Send extra headers |
| 73 | +
|
| 74 | + extra_query: Add additional query parameters to the request |
| 75 | +
|
| 76 | + extra_body: Add additional JSON properties to the request |
| 77 | +
|
| 78 | + timeout: Override the client-level default timeout for this request, in seconds |
| 79 | + """ |
| 80 | + if not id: |
| 81 | + raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") |
| 82 | + return self._post( |
| 83 | + f"/browsers/{id}/playwright/execute", |
| 84 | + body=maybe_transform( |
| 85 | + { |
| 86 | + "code": code, |
| 87 | + "timeout_sec": timeout_sec, |
| 88 | + }, |
| 89 | + playwright_execute_params.PlaywrightExecuteParams, |
| 90 | + ), |
| 91 | + options=make_request_options( |
| 92 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 93 | + ), |
| 94 | + cast_to=PlaywrightExecuteResponse, |
| 95 | + ) |
| 96 | + |
| 97 | + |
| 98 | +class AsyncPlaywrightResource(AsyncAPIResource): |
| 99 | + @cached_property |
| 100 | + def with_raw_response(self) -> AsyncPlaywrightResourceWithRawResponse: |
| 101 | + """ |
| 102 | + This property can be used as a prefix for any HTTP method call to return |
| 103 | + the raw response object instead of the parsed content. |
| 104 | +
|
| 105 | + For more information, see https://www.github.com/onkernel/kernel-python-sdk#accessing-raw-response-data-eg-headers |
| 106 | + """ |
| 107 | + return AsyncPlaywrightResourceWithRawResponse(self) |
| 108 | + |
| 109 | + @cached_property |
| 110 | + def with_streaming_response(self) -> AsyncPlaywrightResourceWithStreamingResponse: |
| 111 | + """ |
| 112 | + An alternative to `.with_raw_response` that doesn't eagerly read the response body. |
| 113 | +
|
| 114 | + For more information, see https://www.github.com/onkernel/kernel-python-sdk#with_streaming_response |
| 115 | + """ |
| 116 | + return AsyncPlaywrightResourceWithStreamingResponse(self) |
| 117 | + |
| 118 | + async def execute( |
| 119 | + self, |
| 120 | + id: str, |
| 121 | + *, |
| 122 | + code: str, |
| 123 | + timeout_sec: int | Omit = omit, |
| 124 | + # Use the following arguments if you need to pass additional parameters to the API that aren't available via kwargs. |
| 125 | + # The extra values given here take precedence over values defined on the client or passed to this method. |
| 126 | + extra_headers: Headers | None = None, |
| 127 | + extra_query: Query | None = None, |
| 128 | + extra_body: Body | None = None, |
| 129 | + timeout: float | httpx.Timeout | None | NotGiven = not_given, |
| 130 | + ) -> PlaywrightExecuteResponse: |
| 131 | + """ |
| 132 | + Execute arbitrary Playwright code in a fresh execution context against the |
| 133 | + browser. The code runs in the same VM as the browser, minimizing latency and |
| 134 | + maximizing throughput. It has access to 'page', 'context', and 'browser' |
| 135 | + variables. It can `return` a value, and this value is returned in the response. |
| 136 | +
|
| 137 | + Args: |
| 138 | + code: TypeScript/JavaScript code to execute. The code has access to 'page', 'context', |
| 139 | + and 'browser' variables. It runs within a function, so you can use a return |
| 140 | + statement at the end to return a value. This value is returned as the `result` |
| 141 | + property in the response. Example: "await page.goto('https://example.com'); |
| 142 | + return await page.title();" |
| 143 | +
|
| 144 | + timeout_sec: Maximum execution time in seconds. Default is 60. |
| 145 | +
|
| 146 | + extra_headers: Send extra headers |
| 147 | +
|
| 148 | + extra_query: Add additional query parameters to the request |
| 149 | +
|
| 150 | + extra_body: Add additional JSON properties to the request |
| 151 | +
|
| 152 | + timeout: Override the client-level default timeout for this request, in seconds |
| 153 | + """ |
| 154 | + if not id: |
| 155 | + raise ValueError(f"Expected a non-empty value for `id` but received {id!r}") |
| 156 | + return await self._post( |
| 157 | + f"/browsers/{id}/playwright/execute", |
| 158 | + body=await async_maybe_transform( |
| 159 | + { |
| 160 | + "code": code, |
| 161 | + "timeout_sec": timeout_sec, |
| 162 | + }, |
| 163 | + playwright_execute_params.PlaywrightExecuteParams, |
| 164 | + ), |
| 165 | + options=make_request_options( |
| 166 | + extra_headers=extra_headers, extra_query=extra_query, extra_body=extra_body, timeout=timeout |
| 167 | + ), |
| 168 | + cast_to=PlaywrightExecuteResponse, |
| 169 | + ) |
| 170 | + |
| 171 | + |
| 172 | +class PlaywrightResourceWithRawResponse: |
| 173 | + def __init__(self, playwright: PlaywrightResource) -> None: |
| 174 | + self._playwright = playwright |
| 175 | + |
| 176 | + self.execute = to_raw_response_wrapper( |
| 177 | + playwright.execute, |
| 178 | + ) |
| 179 | + |
| 180 | + |
| 181 | +class AsyncPlaywrightResourceWithRawResponse: |
| 182 | + def __init__(self, playwright: AsyncPlaywrightResource) -> None: |
| 183 | + self._playwright = playwright |
| 184 | + |
| 185 | + self.execute = async_to_raw_response_wrapper( |
| 186 | + playwright.execute, |
| 187 | + ) |
| 188 | + |
| 189 | + |
| 190 | +class PlaywrightResourceWithStreamingResponse: |
| 191 | + def __init__(self, playwright: PlaywrightResource) -> None: |
| 192 | + self._playwright = playwright |
| 193 | + |
| 194 | + self.execute = to_streamed_response_wrapper( |
| 195 | + playwright.execute, |
| 196 | + ) |
| 197 | + |
| 198 | + |
| 199 | +class AsyncPlaywrightResourceWithStreamingResponse: |
| 200 | + def __init__(self, playwright: AsyncPlaywrightResource) -> None: |
| 201 | + self._playwright = playwright |
| 202 | + |
| 203 | + self.execute = async_to_streamed_response_wrapper( |
| 204 | + playwright.execute, |
| 205 | + ) |
0 commit comments