Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestExt take 2 #1014

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@ use std::sync::Arc;
use std::time::Duration;

use http::Uri;
use ureq_proto::http::Response;

use crate::http;
use crate::middleware::{Middleware, MiddlewareChain};
use crate::{http, Body, Error, WithAgent};
use crate::{Agent, AsSendBody, Proxy, RequestBuilder};

#[cfg(feature = "_tls")]
Expand All @@ -25,6 +26,8 @@ mod private {
}

pub(crate) mod typestate {
use crate::WithAgent;

use super::*;

/// Typestate for [`Config`] when configured for an [`Agent`].
Expand All @@ -33,6 +36,8 @@ pub(crate) mod typestate {
pub struct RequestScope<Any>(pub(crate) RequestBuilder<Any>);
/// Typestate for for [`Config`] when configured via [`Agent::configure_request`].
pub struct HttpCrateScope<S: AsSendBody>(pub(crate) http::Request<S>);
/// TODO
pub struct RequestExtScope<'a, S: AsSendBody>(pub(crate) WithAgent<'a, S>);

impl private::ConfigScope for AgentScope {
fn config(&mut self) -> &mut Config {
Expand All @@ -54,10 +59,17 @@ pub(crate) mod typestate {
&mut req_level.0
}
}

impl<'a, S: AsSendBody> private::ConfigScope for RequestExtScope<'a, S> {
fn config(&mut self) -> &mut Config {
todo!()
}
}
}

use typestate::AgentScope;
use typestate::HttpCrateScope;
use typestate::RequestExtScope;
use typestate::RequestScope;

/// Config primarily for the [`Agent`], but also per-request.
Expand Down Expand Up @@ -781,6 +793,18 @@ impl<S: AsSendBody> ConfigBuilder<HttpCrateScope<S>> {
}
}

impl<'a, S: AsSendBody> ConfigBuilder<RequestExtScope<'a, S>> {
/// TODO
pub fn build(self) -> WithAgent<'a, S> {
self.0 .0
}

/// TODO
pub fn run(self) -> Result<Response<Body>, Error> {
self.0 .0.run()
}
}

/// Request timeout configuration.
///
/// This can be configured both on Agent level as well as per request.
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,8 @@ use http::{Request, Response, Uri};
pub use proxy::Proxy;
pub use request::RequestBuilder;
use request::{WithBody, WithoutBody};
pub use response::ResponseExt;
pub use request_ext::{AgentRef, RequestExt, WithAgent};
pub use response_ext::ResponseExt;
pub use send_body::AsSendBody;

mod agent;
Expand All @@ -536,7 +537,8 @@ mod pool;
mod proxy;
mod query;
mod request;
mod response;
mod request_ext;
mod response_ext;
mod run;
mod send_body;
mod timings;
Expand Down Expand Up @@ -568,6 +570,7 @@ pub mod typestate {

pub use super::config::typestate::AgentScope;
pub use super::config::typestate::HttpCrateScope;
pub use super::config::typestate::RequestExtScope;
pub use super::config::typestate::RequestScope;
}

Expand Down
125 changes: 125 additions & 0 deletions src/request_ext.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
use std::ops::{Deref, DerefMut};

use ureq_proto::http::{Request, Response};

use crate::config::typestate::RequestExtScope;
use crate::config::ConfigBuilder;
use crate::{Agent, AsSendBody, Body, Error};

/// TODO
pub trait RequestExt<S: AsSendBody>: Sized {
/// TODO
///
/// # Example
///
/// ```
/// use ureq::{http, RequestExt};
///
/// http::Request::builder()
/// .method(http::Method::POST)
/// .uri("http://httpbin.org/post")
/// .body(())
/// .unwrap()
/// .with_default_agent()
/// .run();
/// ```
fn with_default_agent(self) -> WithAgent<'static, S> {
let agent = Agent::new_with_defaults();
Self::with_agent(self, agent)
}

/// Use this [`Request`] with a ureq [`Agent`].
///
/// # Example
///
/// ```
/// use ureq::{http, Agent, RequestExt};
/// use std::time::Duration;
///
/// let request: http::Request<()> = http::Request::builder()
/// .method(http::Method::GET)
/// .uri("http://httpbin.org/get")
/// .body(())
/// .unwrap();
///
/// let mut agent = Agent::config_builder()
/// .timeout_global(Some(Duration::from_secs(30)))
/// .build()
/// .new_agent();
///
/// let response = request
/// .with_agent(&mut agent)
/// .configure()
/// .https_only(true)
/// .run()
/// .unwrap();
/// ```
fn with_agent<'a>(self, agent: impl Into<AgentRef<'a>>) -> WithAgent<'a, S>;
}

impl<S: AsSendBody> RequestExt<S> for Request<S> {
fn with_agent<'a>(self, agent: impl Into<AgentRef<'a>>) -> WithAgent<'a, S> {
WithAgent {
agent: agent.into(),
request: self,
}
}
}

/// TODO
pub struct WithAgent<'a, S: AsSendBody> {
agent: AgentRef<'a>,
request: Request<S>,
}

impl<'a, S: AsSendBody> WithAgent<'a, S> {
/// TODO
pub fn configure(self) -> ConfigBuilder<RequestExtScope<'a, S>> {
todo!()
}

/// TODO
pub fn run(self) -> Result<Response<Body>, Error> {
self.agent.run(self.request)
}
}

/// Glue type to hold an owned or &mut Agent.
pub enum AgentRef<'a> {
/// TODO
Owned(Agent),
/// TODO
Borrowed(&'a mut Agent),
}

impl From<Agent> for AgentRef<'static> {
fn from(value: Agent) -> Self {
AgentRef::Owned(value)
}
}

impl<'a> From<&'a mut Agent> for AgentRef<'a> {
fn from(value: &'a mut Agent) -> Self {
AgentRef::Borrowed(value)
}
}

impl Deref for AgentRef<'_> {
type Target = Agent;

fn deref(&self) -> &Self::Target {
match self {
AgentRef::Owned(agent) => agent,
AgentRef::Borrowed(agent) => &*agent,
}
}
}

impl DerefMut for AgentRef<'_> {
fn deref_mut(&mut self) -> &mut Self::Target {
match self {
AgentRef::Owned(agent) => agent,
AgentRef::Borrowed(agent) => agent,
}
}
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::config::{Config, RequestLevelConfig, DEFAULT_USER_AGENT};
use crate::http;
use crate::pool::Connection;
use crate::request::ForceSendBody;
use crate::response::{RedirectHistory, ResponseUri};
use crate::response_ext::{RedirectHistory, ResponseUri};
use crate::timings::{CallTimings, CurrentTime};
use crate::transport::time::{Duration, Instant};
use crate::transport::ConnectionDetails;
Expand Down
Loading