browser_commander/playwright/protocol/
request.rs1#![allow(clippy::all, missing_docs, unused_imports)]
5
6use serde::{Deserialize, Serialize};
7
8use super::*;
9use crate::playwright::connection::{
10 Binary, Channel, ChannelType, ObjectRef, ProtocolError, ProtocolEvent, Ref,
11};
12
13#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
15pub struct RequestInitializer {
16 #[serde(rename = "frame", default, skip_serializing_if = "Option::is_none")]
17 pub frame: Option<Ref<Frame>>,
18 #[serde(rename = "serviceWorker", default, skip_serializing_if = "Option::is_none")]
19 pub service_worker: Option<Ref<Worker>>,
20 #[serde(rename = "url")]
21 pub url: String,
22 #[serde(rename = "resourceType")]
23 pub resource_type: String,
24 #[serde(rename = "method")]
25 pub method: String,
26 #[serde(rename = "postData", default, skip_serializing_if = "Option::is_none")]
27 pub post_data: Option<Binary>,
28 #[serde(rename = "headers")]
29 pub headers: Vec<NameValue>,
30 #[serde(rename = "isNavigationRequest")]
31 pub is_navigation_request: bool,
32 #[serde(rename = "redirectedFrom", default, skip_serializing_if = "Option::is_none")]
33 pub redirected_from: Option<Ref<Request>>,
34}
35
36#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
38pub struct RequestResponseResult {
39 #[serde(rename = "response", default, skip_serializing_if = "Option::is_none")]
40 pub response: Option<Ref<Response>>,
41}
42
43#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
45pub struct RequestRawRequestHeadersResult {
46 #[serde(rename = "headers")]
47 pub headers: Vec<NameValue>,
48}
49
50#[derive(Debug, Clone, PartialEq)]
52pub enum RequestEvent {
53 Unknown {
55 method: String,
57 params: serde_json::Value,
59 },
60}
61
62impl ProtocolEvent for RequestEvent {
63 fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
64 match method {
65 _ => Ok(Self::Unknown { method: method.to_string(), params }),
66 }
67 }
68}
69
70#[derive(Debug, Clone)]
72pub struct Request {
73 channel: Channel,
74}
75
76impl ChannelType for Request {
77 const INTERFACE: &'static str = "Request";
78 type Initializer = RequestInitializer;
79 type Event = RequestEvent;
80
81 fn accepts(interface: &str) -> bool {
82 matches!(interface, "Request")
83 }
84
85 fn from_channel(channel: Channel) -> Self {
86 Self { channel }
87 }
88
89 fn channel(&self) -> &Channel {
90 &self.channel
91 }
92}
93
94impl Request {
95 pub async fn response(&self) -> Result<RequestResponseResult, ProtocolError> {
97 self.channel.send("response", &serde_json::json!({})).await
98 }
99
100 pub async fn raw_request_headers(&self) -> Result<RequestRawRequestHeadersResult, ProtocolError> {
102 self.channel.send("rawRequestHeaders", &serde_json::json!({})).await
103 }
104}