[−][src]Struct exonum_rust_runtime::api::ServiceApiBuilder
Exonum service API builder which is used to add endpoints to the node API.
Examples
The example below shows a common practice of the API implementation.
use serde_derive::{Deserialize, Serialize}; use exonum::{blockchain::Schema, crypto::Hash, merkledb::ObjectHash}; use exonum_rust_runtime::api::{self, ServiceApiBuilder, ServiceApiState}; // Declare a type which describes an API specification and implementation. pub struct MyApi; // Declare structures for requests and responses. // For the web backend `MyQuery` will be deserialized from the `block_height={number}` string. #[derive(Deserialize, Clone, Copy)] pub struct MyQuery { pub block_height: u64, } // For the web backend `BlockInfo` will be serialized into a JSON string. #[derive(Serialize, Clone, Copy)] pub struct BlockInfo { pub hash: Hash, } // Create API handlers. impl MyApi { /// Immutable handler which returns a hash of the block at the given height. pub async fn block_hash( state: ServiceApiState, query: MyQuery, ) -> api::Result<Option<BlockInfo>> { let schema = state.data().for_core(); Ok(schema .block_hashes_by_height() .get(query.block_height) .map(|hash| BlockInfo { hash })) } /// Simple handler without any parameters. pub async fn ping(_state: ServiceApiState, _query: ()) -> api::Result<()> { Ok(()) } /// You may also use asynchronous tasks. pub async fn async_operation( _state: ServiceApiState, query: MyQuery, ) -> api::Result<Option<Hash>> { Ok(long_async_task(query).await) } } fn wire_api(builder: &mut ServiceApiBuilder) -> &mut ServiceApiBuilder { // Add `MyApi` handlers to the corresponding builder. builder .public_scope() .endpoint("v1/block_hash", MyApi::block_hash) .endpoint("v1/async_operation", MyApi::async_operation); // Add a mutable endpoint to the private API. builder .private_scope() .endpoint("v1/ping", MyApi::ping); builder }
Methods
impl ServiceApiBuilder
[src]
pub fn public_scope(&mut self) -> &mut ServiceApiScope
[src]
Returns a mutable reference to the public API scope builder.
pub fn private_scope(&mut self) -> &mut ServiceApiScope
[src]
Returns a mutable reference to the private API scope builder.
pub fn blockchain(&self) -> &Blockchain
[src]
Returns a reference to the blockchain.
Trait Implementations
impl Debug for ServiceApiBuilder
[src]
impl From<ServiceApiBuilder> for ApiBuilder
[src]
fn from(inner: ServiceApiBuilder) -> Self
[src]
Auto Trait Implementations
impl !RefUnwindSafe for ServiceApiBuilder
impl Send for ServiceApiBuilder
impl Sync for ServiceApiBuilder
impl Unpin for ServiceApiBuilder
impl !UnwindSafe for ServiceApiBuilder
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,