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

move example client into examples directory #5

Open
wants to merge 3 commits into
base: release_candidate_1
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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,5 @@ wasm-bindgen-test = { version = "0.3.2" }
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
tokio = { version = "1.28.2", features = ["rt", "macros"] }


[[example]]
name = "client"
38 changes: 18 additions & 20 deletions src/transport/client/example.rs → examples/client.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
// THIS FILE MUST NOT INCLUDED IN WORKSPACE
**Some Broken Code to Ensure the Build Fails if this File is Included in the Workspace**

use crate::transport::endpoints::{SiaApiRequest};
// **Some Broken Code to Ensure the Build Fails if this File is Included in the Workspace**
//
// cargo run --example client
//
use async_trait::async_trait;
use serde::Deserialize;
use crate::transport::client::{ApiClient, ApiClientError, ApiClientHelpers, EndpointSchema};

use sia_rust::transport::client::{ApiClient, ApiClientError, ApiClientHelpers, EndpointSchema};
use sia_rust::transport::endpoints::SiaApiRequest;

#[derive(Clone)]
pub struct ExampleClient {
pub client: String, // Placeholder for a client type, generally something allowing data requests
pub client: String, // Placeholder for a client type, generally something allowing data requests
}

// Placeholder configuration struct for ExampleClient
// Generally this will hold everything neccesary to init a new instance of ExampleClient
// It can include things like authentication, timeouts, etc.
#[derive(Clone, Debug, Deserialize)]
pub struct ExampleClientConf {
pub foo: String, // Placeholders for some client specific configurations
pub bar: Option<String>,
pub foo: String, // Placeholders for some client specific configurations
pub bar: Option<String>,
}

#[async_trait]
impl ApiClient for ExampleClient {
type Request = PlaceholderRequest; // Placeholder for the request type, like `reqwest::Request`
type Request = PlaceholderRequest; // Placeholder for the request type, like `reqwest::Request`
type Response = PlaceholderResponse; // Placeholder for the response type, like `reqwest::Response`
type Conf = ExampleClientConf; // Configuration type for ExampleClient
type Conf = ExampleClientConf; // Configuration type for ExampleClient

// Instantiate a new ExampleClient
async fn new(conf: Self::Conf) -> Result<Self, ApiClientError> {
Expand All @@ -37,17 +38,14 @@ impl ApiClient for ExampleClient {
fn process_schema(&self, schema: EndpointSchema) -> Result<Self::Request, ApiClientError> {
/// Add logic for converting the schema into a request
/// Schema is a standard format for providing a client with the information needed
/// to create their request type.
todo!();
/// to create their request type.
todo!();
}

// Convert an `SiaApiRequest` into a `Request`
fn to_data_request<R: SiaApiRequest>(&self, request: R) -> Result<Self::Request, ApiClientError> {
/// Convert an `SiaApiRequest` into a `Request` that can be executed
/// SiaApiRequest represents a request-response pair defining data types for input and output
/// This function converts the request into a format that ExampleClient can execute
let schema = request.to_endpoint_schema()?; // Convert request to schema
self.process_schema(schema) // Process schema into a request
let schema = request.to_endpoint_schema()?; // Convert request to schema
self.process_schema(schema) // Process schema into a request
}

// Execute the request and return a response
Expand All @@ -58,11 +56,11 @@ impl ApiClient for ExampleClient {

// Dispatcher function that converts the request and handles execution
async fn dispatcher<R: SiaApiRequest>(&self, request: R) -> Result<R::Response, ApiClientError> {
let request = self.to_data_request(request)?; // Convert request to data request
let request = self.to_data_request(request)?; // Convert request to data request

// Execute the request
let response = self.execute_request(request).await?;

// Check the response status and return the appropriate result
todo!(); // Handle status and response, similar to NativeClient's dispatcher
}
Expand All @@ -72,4 +70,4 @@ impl ApiClient for ExampleClient {
// Just this is needed to implement the `ApiClientHelpers` trait
// unless custom implementations for the traits methods are needed
#[async_trait]
impl ApiClientHelpers for ExampleClient {}
impl ApiClientHelpers for ExampleClient {}