Skip to content
Merged
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
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ license = "GPL-3.0-or-later"
description = "Rust SDK for the TinyHumans backend"
repository = "https://github.com/tinyhumansai/sdk"

[features]
default = ["socket"]
socket = ["dep:rust_socketio"]

[dependencies]
base64 = "0.22"
# `stream` is for the SSE session stream in `src/sse.rs`; every other route
Expand All @@ -21,7 +25,7 @@ percent-encoding = "2"
# what earns these two their place in an otherwise dependency-light crate.
futures = "0.3"
# Socket.IO is a distinct framed protocol over Engine.IO, not a raw WebSocket.
rust_socketio = { version = "0.6", features = ["async"] }
rust_socketio = { version = "0.6", features = ["async"], optional = true }
Comment thread
senamakel marked this conversation as resolved.
tokio = { version = "1", features = ["sync", "time"] }

[dev-dependencies]
Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use url::Url;
pub mod api;
pub mod generated_public_routes;
pub mod jwt;
#[cfg(feature = "socket")]
pub mod socket;
pub mod sse;

Expand Down Expand Up @@ -50,14 +51,19 @@ pub enum Error {
Header(#[from] reqwest::header::InvalidHeaderValue),
#[error("response decoding failed: {0}")]
Decode(#[from] serde_json::Error),
#[cfg(feature = "socket")]
#[error("socket.io transport failed: {0}")]
Socket(Box<rust_socketio::Error>),
#[cfg(feature = "socket")]
#[error("a bearer token is required for socket.io connections")]
MissingSocketToken,
#[cfg(feature = "socket")]
#[error("socket event {0} did not carry a JSON payload")]
UnexpectedSocketPayload(String),
#[cfg(feature = "socket")]
#[error("socket acknowledgement timed out")]
SocketAckTimeout,
#[cfg(feature = "socket")]
#[error("socket acknowledgement channel closed")]
SocketAckClosed,
#[error("route is intentionally not exposed by the SDK: {0} {1}")]
Expand All @@ -75,6 +81,7 @@ pub enum Error {
},
}

#[cfg(feature = "socket")]
impl From<rust_socketio::Error> for Error {
fn from(error: rust_socketio::Error) -> Self {
Self::Socket(Box::new(error))
Expand Down Expand Up @@ -208,6 +215,7 @@ impl TinyHumansClient {
/// The returned connection receives every public socket event through one
/// generic stream and also exposes typed helpers for the Medulla harness
/// and workflow protocol.
#[cfg(feature = "socket")]
pub async fn connect_socket(&self) -> Result<socket::SocketConnection, Error> {
let token = self.http.token.clone().ok_or(Error::MissingSocketToken)?;
socket::SocketConnection::connect(self.http.base_url.clone(), token).await
Expand Down
2 changes: 2 additions & 0 deletions tests/socket.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg(feature = "socket")]

use serde_json::json;
use tinyhumans_sdk::socket::medulla::{
CapabilitiesRequest, RegisterWorkflows, TaskRun, WorkflowRequest, WorkflowRequestOp,
Expand Down
Loading