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 withCreating 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>
impl<T> Response<T>
Sourcepub fn from(body: T) -> Self
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);Sourcepub fn from_parts(parts: Parts, body: T) -> Self
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());Sourcepub fn headers(&self) -> &HeaderMap<HeaderValue>
pub fn headers(&self) -> &HeaderMap<HeaderValue>
Returns the headers associated with this response.
§Example
let response = Response::from(());
assert!(response.headers().is_empty());Sourcepub fn body(&self) -> &T
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");Sourcepub fn into_parts(self) -> (Parts, T)
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());Trait Implementations§
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> 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