All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.openqa.selenium.devtools.v122.fetch.Fetch Maven / Gradle / Ivy

Go to download

Selenium automates browsers. That's it! What you do with that power is entirely up to you.

There is a newer version: 4.20.0
Show newest version
package org.openqa.selenium.devtools.v122.fetch;

import org.openqa.selenium.Beta;
import org.openqa.selenium.devtools.Command;
import org.openqa.selenium.devtools.Event;
import org.openqa.selenium.devtools.ConverterFunctions;
import java.util.Map;
import java.util.LinkedHashMap;
import org.openqa.selenium.json.JsonInput;

/**
 * A domain for letting clients substitute browser's network layer with client code.
 */
public class Fetch {

    /**
     * Disables the fetch domain.
     */
    public static Command disable() {
        LinkedHashMap params = new LinkedHashMap<>();
        return new Command<>("Fetch.disable", Map.copyOf(params));
    }

    /**
     * Enables issuing of requestPaused events. A request will be paused until client
     * calls one of failRequest, fulfillRequest or continueRequest/continueWithAuth.
     */
    public static Command enable(java.util.Optional> patterns, java.util.Optional handleAuthRequests) {
        LinkedHashMap params = new LinkedHashMap<>();
        patterns.ifPresent(p -> params.put("patterns", p));
        handleAuthRequests.ifPresent(p -> params.put("handleAuthRequests", p));
        return new Command<>("Fetch.enable", Map.copyOf(params));
    }

    /**
     * Causes the request to fail with specified reason.
     */
    public static Command failRequest(org.openqa.selenium.devtools.v122.fetch.model.RequestId requestId, org.openqa.selenium.devtools.v122.network.model.ErrorReason errorReason) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        java.util.Objects.requireNonNull(errorReason, "errorReason is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("requestId", requestId);
        params.put("errorReason", errorReason);
        return new Command<>("Fetch.failRequest", Map.copyOf(params));
    }

    /**
     * Provides response to the request.
     */
    public static Command fulfillRequest(org.openqa.selenium.devtools.v122.fetch.model.RequestId requestId, java.lang.Integer responseCode, java.util.Optional> responseHeaders, java.util.Optional binaryResponseHeaders, java.util.Optional body, java.util.Optional responsePhrase) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        java.util.Objects.requireNonNull(responseCode, "responseCode is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("requestId", requestId);
        params.put("responseCode", responseCode);
        responseHeaders.ifPresent(p -> params.put("responseHeaders", p));
        binaryResponseHeaders.ifPresent(p -> params.put("binaryResponseHeaders", p));
        body.ifPresent(p -> params.put("body", p));
        responsePhrase.ifPresent(p -> params.put("responsePhrase", p));
        return new Command<>("Fetch.fulfillRequest", Map.copyOf(params));
    }

    /**
     * Continues the request, optionally modifying some of its parameters.
     */
    public static Command continueRequest(org.openqa.selenium.devtools.v122.fetch.model.RequestId requestId, java.util.Optional url, java.util.Optional method, java.util.Optional postData, java.util.Optional> headers, java.util.Optional interceptResponse) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("requestId", requestId);
        url.ifPresent(p -> params.put("url", p));
        method.ifPresent(p -> params.put("method", p));
        postData.ifPresent(p -> params.put("postData", p));
        headers.ifPresent(p -> params.put("headers", p));
        interceptResponse.ifPresent(p -> params.put("interceptResponse", p));
        return new Command<>("Fetch.continueRequest", Map.copyOf(params));
    }

    /**
     * Continues a request supplying authChallengeResponse following authRequired event.
     */
    public static Command continueWithAuth(org.openqa.selenium.devtools.v122.fetch.model.RequestId requestId, org.openqa.selenium.devtools.v122.fetch.model.AuthChallengeResponse authChallengeResponse) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        java.util.Objects.requireNonNull(authChallengeResponse, "authChallengeResponse is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("requestId", requestId);
        params.put("authChallengeResponse", authChallengeResponse);
        return new Command<>("Fetch.continueWithAuth", Map.copyOf(params));
    }

    /**
     * Continues loading of the paused response, optionally modifying the
     * response headers. If either responseCode or headers are modified, all of them
     * must be present.
     */
    @Beta()
    public static Command continueResponse(org.openqa.selenium.devtools.v122.fetch.model.RequestId requestId, java.util.Optional responseCode, java.util.Optional responsePhrase, java.util.Optional> responseHeaders, java.util.Optional binaryResponseHeaders) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("requestId", requestId);
        responseCode.ifPresent(p -> params.put("responseCode", p));
        responsePhrase.ifPresent(p -> params.put("responsePhrase", p));
        responseHeaders.ifPresent(p -> params.put("responseHeaders", p));
        binaryResponseHeaders.ifPresent(p -> params.put("binaryResponseHeaders", p));
        return new Command<>("Fetch.continueResponse", Map.copyOf(params));
    }

    public static class GetResponseBodyResponse {

        private final java.lang.String body;

        private final java.lang.Boolean base64Encoded;

        public GetResponseBodyResponse(java.lang.String body, java.lang.Boolean base64Encoded) {
            this.body = java.util.Objects.requireNonNull(body, "body is required");
            this.base64Encoded = java.util.Objects.requireNonNull(base64Encoded, "base64Encoded is required");
        }

        /**
         * Response body.
         */
        public java.lang.String getBody() {
            return body;
        }

        /**
         * True, if content was sent as base64.
         */
        public java.lang.Boolean getBase64Encoded() {
            return base64Encoded;
        }

        private static GetResponseBodyResponse fromJson(JsonInput input) {
            java.lang.String body = null;
            java.lang.Boolean base64Encoded = false;
            input.beginObject();
            while (input.hasNext()) {
                switch(input.nextName()) {
                    case "body":
                        body = input.nextString();
                        break;
                    case "base64Encoded":
                        base64Encoded = input.nextBoolean();
                        break;
                    default:
                        input.skipValue();
                        break;
                }
            }
            input.endObject();
            return new GetResponseBodyResponse(body, base64Encoded);
        }
    }

    /**
     * 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.
     */
    public static Command getResponseBody(org.openqa.selenium.devtools.v122.fetch.model.RequestId requestId) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("requestId", requestId);
        return new Command<>("Fetch.getResponseBody", Map.copyOf(params), input -> input.read(org.openqa.selenium.devtools.v122.fetch.Fetch.GetResponseBodyResponse.class));
    }

    /**
     * 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.
     */
    public static Command takeResponseBodyAsStream(org.openqa.selenium.devtools.v122.fetch.model.RequestId requestId) {
        java.util.Objects.requireNonNull(requestId, "requestId is required");
        LinkedHashMap params = new LinkedHashMap<>();
        params.put("requestId", requestId);
        return new Command<>("Fetch.takeResponseBodyAsStream", Map.copyOf(params), ConverterFunctions.map("stream", org.openqa.selenium.devtools.v122.io.model.StreamHandle.class));
    }

    public static Event requestPaused() {
        return new Event<>("Fetch.requestPaused", input -> input.read(org.openqa.selenium.devtools.v122.fetch.model.RequestPaused.class));
    }

    public static Event authRequired() {
        return new Event<>("Fetch.authRequired", input -> input.read(org.openqa.selenium.devtools.v122.fetch.model.AuthRequired.class));
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy