Packages

A browser automation tool and interface to the Chrome DevTools Protocol.

Current section

Files

Jump to
chrobot_extra src chrobot_extra protocol fetch.gleam
Raw

src/chrobot_extra/protocol/fetch.gleam

//// > ⚙️ This module was generated from the Chrome DevTools Protocol version **1.3**
//// ## Fetch Domain
////
//// A domain for letting clients substitute browser's network layer with client code.
////
//// [📖 View this domain on the DevTools Protocol API Docs](https://chromedevtools.github.io/devtools-protocol/1-3/Fetch/)
// ---------------------------------------------------------------------------
// | !!!!!! This is an autogenerated file - Do not edit manually !!!!!! |
// | Run `codegen.sh` to regenerate. |
// ---------------------------------------------------------------------------
import chrobot_extra/chrome
import chrobot_extra/internal/utils
import chrobot_extra/protocol/io
import chrobot_extra/protocol/network
import gleam/dynamic/decode
import gleam/json
import gleam/option
import gleam/result
/// Unique request identifier.
pub type RequestId {
RequestId(String)
}
@internal
pub fn encode__request_id(value__: RequestId) {
case value__ {
RequestId(inner_value__) -> json.string(inner_value__)
}
}
@internal
pub fn decode__request_id() {
{
use value__ <- decode.then(decode.string)
decode.success(RequestId(value__))
}
}
/// Stages of the request to handle. Request will intercept before the request is
/// sent. Response will intercept after the response is received (but before response
/// body is received).
pub type RequestStage {
RequestStageRequest
RequestStageResponse
}
@internal
pub fn encode__request_stage(value__: RequestStage) {
case value__ {
RequestStageRequest -> "Request"
RequestStageResponse -> "Response"
}
|> json.string()
}
@internal
pub fn decode__request_stage() {
{
use value__ <- decode.then(decode.string)
case value__ {
"Request" -> decode.success(RequestStageRequest)
"Response" -> decode.success(RequestStageResponse)
_ -> decode.failure(RequestStageRequest, "valid enum property")
}
}
}
pub type RequestPattern {
RequestPattern(
/// Wildcards (`'*'` -> zero or more, `'?'` -> exactly one) are allowed. Escape character is
/// backslash. Omitting is equivalent to `"*"`.
url_pattern: option.Option(String),
/// If set, only requests for matching resource types will be intercepted.
resource_type: option.Option(network.ResourceType),
/// Stage at which to begin intercepting requests. Default is Request.
request_stage: option.Option(RequestStage),
)
}
@internal
pub fn encode__request_pattern(value__: RequestPattern) {
json.object(
[]
|> utils.add_optional(value__.url_pattern, fn(inner_value__) {
#("urlPattern", json.string(inner_value__))
})
|> utils.add_optional(value__.resource_type, fn(inner_value__) {
#("resourceType", network.encode__resource_type(inner_value__))
})
|> utils.add_optional(value__.request_stage, fn(inner_value__) {
#("requestStage", encode__request_stage(inner_value__))
}),
)
}
@internal
pub fn decode__request_pattern() {
{
use url_pattern <- decode.optional_field(
"urlPattern",
option.None,
decode.optional(decode.string),
)
use resource_type <- decode.optional_field(
"resourceType",
option.None,
decode.optional(network.decode__resource_type()),
)
use request_stage <- decode.optional_field(
"requestStage",
option.None,
decode.optional(decode__request_stage()),
)
decode.success(RequestPattern(
url_pattern: url_pattern,
resource_type: resource_type,
request_stage: request_stage,
))
}
}
/// Response HTTP header entry
pub type HeaderEntry {
HeaderEntry(name: String, value: String)
}
@internal
pub fn encode__header_entry(value__: HeaderEntry) {
json.object([
#("name", json.string(value__.name)),
#("value", json.string(value__.value)),
])
}
@internal
pub fn decode__header_entry() {
{
use name <- decode.field("name", decode.string)
use value <- decode.field("value", decode.string)
decode.success(HeaderEntry(name: name, value: value))
}
}
/// Authorization challenge for HTTP status code 401 or 407.
pub type AuthChallenge {
AuthChallenge(
/// Source of the authentication challenge.
source: option.Option(AuthChallengeSource),
/// Origin of the challenger.
origin: String,
/// The authentication scheme used, such as basic or digest
scheme: String,
/// The realm of the challenge. May be empty.
realm: String,
)
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the possible values of the enum property `source` of `AuthChallenge`
pub type AuthChallengeSource {
AuthChallengeSourceServer
AuthChallengeSourceProxy
}
@internal
pub fn encode__auth_challenge_source(value__: AuthChallengeSource) {
case value__ {
AuthChallengeSourceServer -> "Server"
AuthChallengeSourceProxy -> "Proxy"
}
|> json.string()
}
@internal
pub fn decode__auth_challenge_source() {
{
use value__ <- decode.then(decode.string)
case value__ {
"Server" -> decode.success(AuthChallengeSourceServer)
"Proxy" -> decode.success(AuthChallengeSourceProxy)
_ -> decode.failure(AuthChallengeSourceServer, "valid enum property")
}
}
}
@internal
pub fn encode__auth_challenge(value__: AuthChallenge) {
json.object(
[
#("origin", json.string(value__.origin)),
#("scheme", json.string(value__.scheme)),
#("realm", json.string(value__.realm)),
]
|> utils.add_optional(value__.source, fn(inner_value__) {
#("source", encode__auth_challenge_source(inner_value__))
}),
)
}
@internal
pub fn decode__auth_challenge() {
{
use source <- decode.optional_field(
"source",
option.None,
decode.optional(decode__auth_challenge_source()),
)
use origin <- decode.field("origin", decode.string)
use scheme <- decode.field("scheme", decode.string)
use realm <- decode.field("realm", decode.string)
decode.success(AuthChallenge(
source: source,
origin: origin,
scheme: scheme,
realm: realm,
))
}
}
/// Response to an AuthChallenge.
pub type AuthChallengeResponse {
AuthChallengeResponse(
/// The decision on what to do in response to the authorization challenge. Default means
/// deferring to the default behavior of the net stack, which will likely either the Cancel
/// authentication or display a popup dialog box.
response: AuthChallengeResponseResponse,
/// The username to provide, possibly empty. Should only be set if response is
/// ProvideCredentials.
username: option.Option(String),
/// The password to provide, possibly empty. Should only be set if response is
/// ProvideCredentials.
password: option.Option(String),
)
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the possible values of the enum property `response` of `AuthChallengeResponse`
pub type AuthChallengeResponseResponse {
AuthChallengeResponseResponseDefault
AuthChallengeResponseResponseCancelAuth
AuthChallengeResponseResponseProvideCredentials
}
@internal
pub fn encode__auth_challenge_response_response(
value__: AuthChallengeResponseResponse,
) {
case value__ {
AuthChallengeResponseResponseDefault -> "Default"
AuthChallengeResponseResponseCancelAuth -> "CancelAuth"
AuthChallengeResponseResponseProvideCredentials -> "ProvideCredentials"
}
|> json.string()
}
@internal
pub fn decode__auth_challenge_response_response() {
{
use value__ <- decode.then(decode.string)
case value__ {
"Default" -> decode.success(AuthChallengeResponseResponseDefault)
"CancelAuth" -> decode.success(AuthChallengeResponseResponseCancelAuth)
"ProvideCredentials" ->
decode.success(AuthChallengeResponseResponseProvideCredentials)
_ ->
decode.failure(
AuthChallengeResponseResponseDefault,
"valid enum property",
)
}
}
}
@internal
pub fn encode__auth_challenge_response(value__: AuthChallengeResponse) {
json.object(
[
#("response", encode__auth_challenge_response_response(value__.response)),
]
|> utils.add_optional(value__.username, fn(inner_value__) {
#("username", json.string(inner_value__))
})
|> utils.add_optional(value__.password, fn(inner_value__) {
#("password", json.string(inner_value__))
}),
)
}
@internal
pub fn decode__auth_challenge_response() {
{
use response <- decode.field(
"response",
decode__auth_challenge_response_response(),
)
use username <- decode.optional_field(
"username",
option.None,
decode.optional(decode.string),
)
use password <- decode.optional_field(
"password",
option.None,
decode.optional(decode.string),
)
decode.success(AuthChallengeResponse(
response: response,
username: username,
password: password,
))
}
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the response to the command `get_response_body`
pub type GetResponseBodyResponse {
GetResponseBodyResponse(
/// Response body.
body: String,
/// True, if content was sent as base64.
base64_encoded: Bool,
)
}
@internal
pub fn decode__get_response_body_response() {
{
use body <- decode.field("body", decode.string)
use base64_encoded <- decode.field("base64Encoded", decode.bool)
decode.success(GetResponseBodyResponse(
body: body,
base64_encoded: base64_encoded,
))
}
}
/// This type is not part of the protocol spec, it has been generated dynamically
/// to represent the response to the command `take_response_body_as_stream`
pub type TakeResponseBodyAsStreamResponse {
TakeResponseBodyAsStreamResponse(stream: io.StreamHandle)
}
@internal
pub fn decode__take_response_body_as_stream_response() {
{
use stream <- decode.field("stream", io.decode__stream_handle())
decode.success(TakeResponseBodyAsStreamResponse(stream: stream))
}
}
/// Disables the fetch domain.
///
pub fn disable(callback__) {
callback__("Fetch.disable", option.None)
}
/// Enables issuing of requestPaused events. A request will be paused until client
/// calls one of failRequest, fulfillRequest or continueRequest/continueWithAuth.
///
/// Parameters:
/// - `patterns` : If specified, only requests matching any of these patterns will produce
/// fetchRequested event and will be paused until clients response. If not set,
/// all requests will be affected.
/// - `handle_auth_requests` : If true, authRequired events will be issued and requests will be paused
/// expecting a call to continueWithAuth.
///
/// Returns:
///
pub fn enable(
callback__,
patterns patterns: option.Option(List(RequestPattern)),
handle_auth_requests handle_auth_requests: option.Option(Bool),
) {
callback__(
"Fetch.enable",
option.Some(json.object(
[]
|> utils.add_optional(patterns, fn(inner_value__) {
#("patterns", json.array(inner_value__, of: encode__request_pattern))
})
|> utils.add_optional(handle_auth_requests, fn(inner_value__) {
#("handleAuthRequests", json.bool(inner_value__))
}),
)),
)
}
/// Causes the request to fail with specified reason.
///
/// Parameters:
/// - `request_id` : An id the client received in requestPaused event.
/// - `error_reason` : Causes the request to fail with the given reason.
///
/// Returns:
///
pub fn fail_request(
callback__,
request_id request_id: RequestId,
error_reason error_reason: network.ErrorReason,
) {
callback__(
"Fetch.failRequest",
option.Some(
json.object([
#("requestId", encode__request_id(request_id)),
#("errorReason", network.encode__error_reason(error_reason)),
]),
),
)
}
/// Provides response to the request.
///
/// Parameters:
/// - `request_id` : An id the client received in requestPaused event.
/// - `response_code` : An HTTP response code.
/// - `response_headers` : Response headers.
/// - `binary_response_headers` : Alternative way of specifying response headers as a \0-separated
/// series of name: value pairs. Prefer the above method unless you
/// need to represent some non-UTF8 values that can't be transmitted
/// over the protocol as text. (Encoded as a base64 string when passed over JSON)
/// - `body` : A response body. If absent, original response body will be used if
/// the request is intercepted at the response stage and empty body
/// will be used if the request is intercepted at the request stage. (Encoded as a base64 string when passed over JSON)
/// - `response_phrase` : A textual representation of responseCode.
/// If absent, a standard phrase matching responseCode is used.
///
/// Returns:
///
pub fn fulfill_request(
callback__,
request_id request_id: RequestId,
response_code response_code: Int,
response_headers response_headers: option.Option(List(HeaderEntry)),
binary_response_headers binary_response_headers: option.Option(String),
body body: option.Option(String),
response_phrase response_phrase: option.Option(String),
) {
callback__(
"Fetch.fulfillRequest",
option.Some(json.object(
[
#("requestId", encode__request_id(request_id)),
#("responseCode", json.int(response_code)),
]
|> utils.add_optional(response_headers, fn(inner_value__) {
#(
"responseHeaders",
json.array(inner_value__, of: encode__header_entry),
)
})
|> utils.add_optional(binary_response_headers, fn(inner_value__) {
#("binaryResponseHeaders", json.string(inner_value__))
})
|> utils.add_optional(body, fn(inner_value__) {
#("body", json.string(inner_value__))
})
|> utils.add_optional(response_phrase, fn(inner_value__) {
#("responsePhrase", json.string(inner_value__))
}),
)),
)
}
/// Continues the request, optionally modifying some of its parameters.
///
/// Parameters:
/// - `request_id` : An id the client received in requestPaused event.
/// - `url` : If set, the request url will be modified in a way that's not observable by page.
/// - `method` : If set, the request method is overridden.
/// - `post_data` : If set, overrides the post data in the request. (Encoded as a base64 string when passed over JSON)
/// - `headers` : If set, overrides the request headers. Note that the overrides do not
/// extend to subsequent redirect hops, if a redirect happens. Another override
/// may be applied to a different request produced by a redirect.
///
/// Returns:
///
pub fn continue_request(
callback__,
request_id request_id: RequestId,
url url: option.Option(String),
method method: option.Option(String),
post_data post_data: option.Option(String),
headers headers: option.Option(List(HeaderEntry)),
) {
callback__(
"Fetch.continueRequest",
option.Some(json.object(
[
#("requestId", encode__request_id(request_id)),
]
|> utils.add_optional(url, fn(inner_value__) {
#("url", json.string(inner_value__))
})
|> utils.add_optional(method, fn(inner_value__) {
#("method", json.string(inner_value__))
})
|> utils.add_optional(post_data, fn(inner_value__) {
#("postData", json.string(inner_value__))
})
|> utils.add_optional(headers, fn(inner_value__) {
#("headers", json.array(inner_value__, of: encode__header_entry))
}),
)),
)
}
/// Continues a request supplying authChallengeResponse following authRequired event.
///
/// Parameters:
/// - `request_id` : An id the client received in authRequired event.
/// - `auth_challenge_response` : Response to with an authChallenge.
///
/// Returns:
///
pub fn continue_with_auth(
callback__,
request_id request_id: RequestId,
auth_challenge_response auth_challenge_response: AuthChallengeResponse,
) {
callback__(
"Fetch.continueWithAuth",
option.Some(
json.object([
#("requestId", encode__request_id(request_id)),
#(
"authChallengeResponse",
encode__auth_challenge_response(auth_challenge_response),
),
]),
),
)
}
/// Causes the body of the response to be received from the server and
/// returned as a single string. May only be issued for a request that
/// is paused in the Response stage and is mutually exclusive with
/// takeResponseBodyForInterceptionAsStream. Calling other methods that
/// affect the request or disabling fetch domain before body is received
/// results in an undefined behavior.
/// Note that the response body is not available for redirects. Requests
/// paused in the _redirect received_ state may be differentiated by
/// `responseCode` and presence of `location` response header, see
/// comments to `requestPaused` for details.
///
/// Parameters:
/// - `request_id` : Identifier for the intercepted request to get body for.
///
/// Returns:
/// - `body` : Response body.
/// - `base64_encoded` : True, if content was sent as base64.
///
pub fn get_response_body(callback__, request_id request_id: RequestId) {
use result__ <- result.try(callback__(
"Fetch.getResponseBody",
option.Some(
json.object([
#("requestId", encode__request_id(request_id)),
]),
),
))
decode.run(result__, decode__get_response_body_response())
|> result.replace_error(chrome.ProtocolError)
}
/// Returns a handle to the stream representing the response body.
/// The request must be paused in the HeadersReceived stage.
/// Note that after this command the request can't be continued
/// as is -- client either needs to cancel it or to provide the
/// response body.
/// The stream only supports sequential read, IO.read will fail if the position
/// is specified.
/// This method is mutually exclusive with getResponseBody.
/// Calling other methods that affect the request or disabling fetch
/// domain before body is received results in an undefined behavior.
///
/// Parameters:
/// - `request_id`
///
/// Returns:
/// - `stream`
///
pub fn take_response_body_as_stream(
callback__,
request_id request_id: RequestId,
) {
use result__ <- result.try(callback__(
"Fetch.takeResponseBodyAsStream",
option.Some(
json.object([
#("requestId", encode__request_id(request_id)),
]),
),
))
decode.run(result__, decode__take_response_body_as_stream_response())
|> result.replace_error(chrome.ProtocolError)
}