Docs in cloudevents-sdk-reqwest
Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
This commit is contained in:
parent
e330c6cc23
commit
6133a6e67f
|
@ -8,6 +8,7 @@ description = "CloudEvents official Rust SDK - Reqwest integration"
|
||||||
documentation = "https://docs.rs/cloudevents-sdk-reqwest"
|
documentation = "https://docs.rs/cloudevents-sdk-reqwest"
|
||||||
repository = "https://github.com/cloudevents/sdk-rust"
|
repository = "https://github.com/cloudevents/sdk-rust"
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
categories = ["web-programming", "encoding", "web-programming::http-client"]
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
|
|
|
@ -62,13 +62,16 @@ impl StructuredSerializer<RequestBuilder> for RequestSerializer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Method to fill a [`RequestBuilder`] with an [`Event`]
|
/// Method to fill a [`RequestBuilder`] with an [`Event`].
|
||||||
pub fn event_to_request(event: Event, request_builder: RequestBuilder) -> Result<RequestBuilder> {
|
pub fn event_to_request(event: Event, request_builder: RequestBuilder) -> Result<RequestBuilder> {
|
||||||
BinaryDeserializer::deserialize_binary(event, RequestSerializer::new(request_builder))
|
BinaryDeserializer::deserialize_binary(event, RequestSerializer::new(request_builder))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extention Trait for [`RequestBuilder`] which acts as a wrapper for the function [`event_to_request()`]
|
/// Extension Trait for [`RequestBuilder`] which acts as a wrapper for the function [`event_to_request()`].
|
||||||
pub trait RequestBuilderExt {
|
///
|
||||||
|
/// This trait is sealed and cannot be implemented for types outside of this crate.
|
||||||
|
pub trait RequestBuilderExt: private::Sealed {
|
||||||
|
/// Write in this [`RequestBuilder`] the provided [`Event`]. Similar to invoking [`Event`].
|
||||||
fn event(self, event: Event) -> Result<RequestBuilder>;
|
fn event(self, event: Event) -> Result<RequestBuilder>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +81,12 @@ impl RequestBuilderExt for RequestBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sealing the RequestBuilderExt
|
||||||
|
mod private {
|
||||||
|
pub trait Sealed {}
|
||||||
|
impl Sealed for reqwest::RequestBuilder {}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -109,8 +109,11 @@ pub async fn response_to_event(res: Response) -> Result<Event> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Extension Trait for [`Response`] which acts as a wrapper for the function [`response_to_event()`].
|
/// Extension Trait for [`Response`] which acts as a wrapper for the function [`response_to_event()`].
|
||||||
|
///
|
||||||
|
/// This trait is sealed and cannot be implemented for types outside of this crate.
|
||||||
#[async_trait(?Send)]
|
#[async_trait(?Send)]
|
||||||
pub trait ResponseExt {
|
pub trait ResponseExt: private::Sealed {
|
||||||
|
/// Convert this [`Response`] to [`Event`].
|
||||||
async fn into_event(self) -> Result<Event>;
|
async fn into_event(self) -> Result<Event>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,6 +124,12 @@ impl ResponseExt for Response {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Sealing the ResponseExt
|
||||||
|
mod private {
|
||||||
|
pub trait Sealed {}
|
||||||
|
impl Sealed for reqwest::Response {}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
//! use cloudevents::{EventBuilderV10, EventBuilder};
|
//! use cloudevents::{EventBuilderV10, EventBuilder};
|
||||||
//! use serde_json::json;
|
//! use serde_json::json;
|
||||||
//!
|
//!
|
||||||
//! # async fn example() {
|
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
//! let client = reqwest::Client::new();
|
//! let client = reqwest::Client::new();
|
||||||
//!
|
//!
|
||||||
//! // Prepare the event to send
|
//! // Prepare the event to send
|
||||||
|
@ -14,23 +14,22 @@
|
||||||
//! .ty("example.test")
|
//! .ty("example.test")
|
||||||
//! .source("http://localhost/")
|
//! .source("http://localhost/")
|
||||||
//! .data("application/json", json!({"hello": "world"}))
|
//! .data("application/json", json!({"hello": "world"}))
|
||||||
//! .build()
|
//! .build()?;
|
||||||
//! .expect("No error while building the event");
|
|
||||||
//!
|
//!
|
||||||
//! // Send request
|
//! // Send request
|
||||||
//! let response = client.post("http://localhost")
|
//! let response = client.post("http://localhost")
|
||||||
//! .event(event_to_send)
|
//! .event(event_to_send)?
|
||||||
//! .expect("Error while serializing the event")
|
//! .send().await?;
|
||||||
//! .send().await
|
|
||||||
//! .expect("Error while sending the request");
|
|
||||||
//! // Parse response as event
|
//! // Parse response as event
|
||||||
//! let received_event = response
|
//! let received_event = response
|
||||||
//! .into_event().await
|
//! .into_event().await?;
|
||||||
//! .expect("Error while deserializing the response");
|
//! # Ok(())
|
||||||
//! # }
|
//! # }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
//! Check out the [cloudevents-sdk](https://docs.rs/cloudevents-sdk) docs for more details on how to use [`cloudevents::Event`]
|
//! Check out the [cloudevents-sdk](https://docs.rs/cloudevents-sdk) docs for more details on how to use [`cloudevents::Event`].
|
||||||
|
|
||||||
|
#![deny(broken_intra_doc_links)]
|
||||||
|
|
||||||
#[macro_use]
|
#[macro_use]
|
||||||
mod headers;
|
mod headers;
|
||||||
|
|
Loading…
Reference in New Issue