pub struct APIRequestContext { /* private fields */ }Expand description
Context for making API requests.
APIRequestContext can be created standalone or from a browser context.
When created from a browser context, cookies are shared between the two.
§Creating a Standalone Context
use viewpoint_core::api::{APIRequestContext, APIContextOptions};
let api = APIRequestContext::new(
APIContextOptions::new()
.base_url("https://api.example.com")
).await?;
// Make requests
let response = api.get("/users").send().await?;§Creating from Browser Context
use viewpoint_core::Browser;
let browser = Browser::launch().headless(true).launch().await.unwrap();
let context = browser.new_context().await.unwrap();
// Get API context that shares cookies with browser
let api = context.request().await.unwrap();
// API requests will include browser cookies
let response = api.get("https://httpbin.org/get").send().await.unwrap();Implementations§
Source§impl APIRequestContext
impl APIRequestContext
Sourcepub async fn new(options: APIContextOptions) -> Result<Self, APIError>
pub async fn new(options: APIContextOptions) -> Result<Self, APIError>
Sourcepub fn get(&self, url: impl Into<String>) -> APIRequestBuilder
pub fn get(&self, url: impl Into<String>) -> APIRequestBuilder
Create a GET request builder.
§Example
let response = api.get("https://api.example.com/users")
.query(&[("page", "1")])
.send()
.await?;Sourcepub fn post(&self, url: impl Into<String>) -> APIRequestBuilder
pub fn post(&self, url: impl Into<String>) -> APIRequestBuilder
Create a POST request builder.
§Example
let response = api.post("https://api.example.com/users")
.json(&serde_json::json!({"name": "John"}))
.send()
.await?;Sourcepub fn put(&self, url: impl Into<String>) -> APIRequestBuilder
pub fn put(&self, url: impl Into<String>) -> APIRequestBuilder
Create a PUT request builder.
§Example
let response = api.put("https://api.example.com/users/1")
.json(&serde_json::json!({"name": "John Updated"}))
.send()
.await?;Sourcepub fn patch(&self, url: impl Into<String>) -> APIRequestBuilder
pub fn patch(&self, url: impl Into<String>) -> APIRequestBuilder
Create a PATCH request builder.
§Example
let response = api.patch("https://api.example.com/users/1")
.json(&serde_json::json!({"status": "active"}))
.send()
.await?;Sourcepub fn delete(&self, url: impl Into<String>) -> APIRequestBuilder
pub fn delete(&self, url: impl Into<String>) -> APIRequestBuilder
Create a DELETE request builder.
§Example
let response = api.delete("https://api.example.com/users/1")
.send()
.await?;Sourcepub fn head(&self, url: impl Into<String>) -> APIRequestBuilder
pub fn head(&self, url: impl Into<String>) -> APIRequestBuilder
Create a HEAD request builder.
§Example
let response = api.head("https://api.example.com/users")
.send()
.await?;
println!("Content-Length: {:?}", response.header("content-length"));Sourcepub fn fetch(
&self,
method: HttpMethod,
url: impl Into<String>,
) -> APIRequestBuilder
pub fn fetch( &self, method: HttpMethod, url: impl Into<String>, ) -> APIRequestBuilder
Create a request builder with a specific HTTP method.
This is the underlying method used by get(), post(), etc.
§Example
let response = api.fetch(HttpMethod::Get, "https://api.example.com/users")
.send()
.await?;Sourcepub async fn dispose(&self)
pub async fn dispose(&self)
Dispose of this API context, releasing resources.
After calling this method, any new requests will fail with APIError::Disposed.
§Example
let api = APIRequestContext::new(APIContextOptions::new()).await?;
// ... use the API ...
api.dispose().await;Sourcepub fn is_disposed(&self) -> bool
pub fn is_disposed(&self) -> bool
Check if this context has been disposed.
Get access to the cookie jar.
This can be used to inspect or manually add cookies.
Trait Implementations§
Source§impl Clone for APIRequestContext
impl Clone for APIRequestContext
Auto Trait Implementations§
impl Freeze for APIRequestContext
impl !RefUnwindSafe for APIRequestContext
impl Send for APIRequestContext
impl Sync for APIRequestContext
impl Unpin for APIRequestContext
impl !UnwindSafe for APIRequestContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more