Response

Struct Response 

Source
pub struct Response<T> { /* private fields */ }
Expand description

Represents a Google Cloud service response.

A response from a Google Cloud service consists of a body (potentially the unit type), and some metadata, currently just headers.

Typically you get a response as the result of making a request via some client in the Google Cloud client libraries for Rust. You may also create responses directly when mocking clients for your own tests.

§Examples

Inspecting the result of a request

// A type representing a Google Cloud service resource, for example, a
// secret manager secret.
struct Resource {
  // ...
}

async fn make_google_service_rpc(project_id: &str) -> Result<Response<Resource>> {
  // ...
}

let response = make_google_service_rpc("my-project").await?;
if let Some(date) = response.headers().get("Date") {
    // do something with the date
}
let resource = response.body();
// do something with

Creating a response for mocks

// A type representing a Google Cloud service resource, for example, a
// secret manager secret.
struct Resource {
  // ...
}

fn make_mock_response(body: Resource) -> Result<Response<Resource>> {
    Ok(Response::from(body))
}

Implementations§

Source§

impl<T> Response<T>

Source

pub fn from(body: T) -> Self

Creates a response from the body.

§Example
#[derive(Clone, Default)]
pub struct Resource {
  // ...
}

let body = Resource::default();
let response = Response::from(body);
Source

pub fn from_parts(parts: Parts, body: T) -> Self

Creates a response from the given parts.

§Example
#[derive(Clone, Default)]
pub struct Resource {
  // ...
}

let mut headers = http::HeaderMap::new();
headers.insert(http::header::CONTENT_TYPE, http::HeaderValue::from_static("application/json"));
let body = Resource::default();
let response : Response<Resource> = Response::from_parts(
    Parts::new().set_headers(headers), body);
assert!(response.headers().get(http::header::CONTENT_TYPE).is_some());
Source

pub fn headers(&self) -> &HeaderMap<HeaderValue>

Returns the headers associated with this response.

§Example
let response = Response::from(());
assert!(response.headers().is_empty());
Source

pub fn body(&self) -> &T

Returns the body associated with this response.

§Example
let response = Response::from("test".to_string());
assert_eq!(response.body().as_str(), "test");
Source

pub fn into_parts(self) -> (Parts, T)

Consumes the response returning the metadata, and body.

§Example
let response = Response::from("test".to_string());
let (parts, body) = response.into_parts();
assert_eq!(body.as_str(), "test");
assert!(parts.headers.is_empty());
Source

pub fn into_body(self) -> T

Consumes the response returning only its body.

§Example
let response = Response::from("test".to_string());
let body = response.into_body();
assert_eq!(body.as_str(), "test");

Trait Implementations§

Source§

impl<T: Clone> Clone for Response<T>

Source§

fn clone(&self) -> Response<T>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<T: Debug> Debug for Response<T>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<T> Freeze for Response<T>
where T: Freeze,

§

impl<T> RefUnwindSafe for Response<T>
where T: RefUnwindSafe,

§

impl<T> Send for Response<T>
where T: Send,

§

impl<T> Sync for Response<T>
where T: Sync,

§

impl<T> Unpin for Response<T>
where T: Unpin,

§

impl<T> UnwindSafe for Response<T>
where T: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V