rename sbot dir to api
This commit is contained in:
parent
5cd2cae3ef
commit
f40cc793f0
|
@ -4,12 +4,10 @@ use async_std::stream::{Stream, StreamExt};
|
|||
use futures::pin_mut;
|
||||
|
||||
use crate::{
|
||||
api::get_subset::{SubsetQuery, SubsetQueryOptions},
|
||||
error::GolgiError,
|
||||
messages::{SsbMessageContentType, SsbMessageValue},
|
||||
sbot::{
|
||||
get_subset::{SubsetQuery, SubsetQueryOptions},
|
||||
sbot_connection::Sbot,
|
||||
},
|
||||
sbot::Sbot,
|
||||
};
|
||||
|
||||
impl Sbot {
|
|
@ -1,6 +1,10 @@
|
|||
use kuska_ssb::api::dto::content::{FriendsHops, RelationshipQuery};
|
||||
|
||||
use crate::{error::GolgiError, messages::SsbMessageContent, sbot::sbot_connection::Sbot, utils};
|
||||
use crate::{
|
||||
api::{FriendsHops, RelationshipQuery},
|
||||
error::GolgiError,
|
||||
messages::SsbMessageContent,
|
||||
sbot::Sbot,
|
||||
utils,
|
||||
};
|
||||
|
||||
impl Sbot {
|
||||
/// Convenience method to set a relationship with following: true, blocking: false.
|
|
@ -1,8 +1,7 @@
|
|||
use async_std::stream::Stream;
|
||||
|
||||
use crate::{
|
||||
error::GolgiError, messages::SsbMessageValue, sbot::sbot_connection::Sbot, utils,
|
||||
utils::get_source_stream,
|
||||
error::GolgiError, messages::SsbMessageValue, sbot::Sbot, utils, utils::get_source_stream,
|
||||
};
|
||||
|
||||
pub use kuska_ssb::api::dto::content::{SubsetQuery, SubsetQueryOptions};
|
|
@ -1,10 +1,7 @@
|
|||
use async_std::stream::Stream;
|
||||
use kuska_ssb::api::dto::CreateHistoryStreamIn;
|
||||
|
||||
use crate::{
|
||||
error::GolgiError, messages::SsbMessageValue, sbot::sbot_connection::Sbot, utils,
|
||||
utils::get_source_stream,
|
||||
};
|
||||
use crate::{error::GolgiError, messages::SsbMessageValue, sbot::Sbot, utils};
|
||||
|
||||
impl Sbot {
|
||||
/// Call the `createHistoryStream` RPC method
|
||||
|
@ -19,7 +16,7 @@ impl Sbot {
|
|||
.client
|
||||
.create_history_stream_req_send(&args)
|
||||
.await?;
|
||||
let history_stream = get_source_stream(
|
||||
let history_stream = utils::get_source_stream(
|
||||
sbot_connection.rpc_reader,
|
||||
req_id,
|
||||
utils::ssb_message_res_parse,
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{error::GolgiError, sbot::sbot_connection::Sbot, utils};
|
||||
use crate::{error::GolgiError, sbot::Sbot, utils};
|
||||
|
||||
impl Sbot {
|
||||
/// Call the `invite create` RPC method and return the created invite
|
|
@ -0,0 +1,12 @@
|
|||
//! API for interacting with a running go-sbot instance.
|
||||
mod about;
|
||||
mod friends;
|
||||
mod get_subset;
|
||||
mod history_stream;
|
||||
mod invite;
|
||||
mod publish;
|
||||
mod whoami;
|
||||
|
||||
pub use crate::sbot::*;
|
||||
|
||||
pub use kuska_ssb::api::dto::content::{FriendsHops, RelationshipQuery};
|
|
@ -1,4 +1,4 @@
|
|||
use crate::{error::GolgiError, messages::SsbMessageContent, sbot::sbot_connection::Sbot, utils};
|
||||
use crate::{error::GolgiError, messages::SsbMessageContent, sbot::Sbot, utils};
|
||||
|
||||
impl Sbot {
|
||||
/// Call the `publish` RPC method and return a message reference.
|
|
@ -0,0 +1,25 @@
|
|||
//! Sbot type and associated methods.
|
||||
|
||||
use crate::{error::GolgiError, sbot::Sbot, utils};
|
||||
|
||||
impl Sbot {
|
||||
/// Call the `whoami` RPC method and return an `id`.
|
||||
pub async fn whoami(&mut self) -> Result<String, GolgiError> {
|
||||
let mut sbot_connection = self.get_sbot_connection().await?;
|
||||
let req_id = sbot_connection.client.whoami_req_send().await?;
|
||||
|
||||
let result = utils::get_async(
|
||||
&mut sbot_connection.rpc_reader,
|
||||
req_id,
|
||||
utils::json_res_parse,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let id = result
|
||||
.get("id")
|
||||
.ok_or_else(|| GolgiError::Sbot("id key not found on whoami call".to_string()))?
|
||||
.as_str()
|
||||
.ok_or_else(|| GolgiError::Sbot("whoami returned non-string value".to_string()))?;
|
||||
Ok(id.to_string())
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
//! API for interacting with a running go-sbot instance.
|
||||
mod about;
|
||||
mod friends;
|
||||
mod get_subset;
|
||||
mod history_stream;
|
||||
mod invite;
|
||||
mod publish;
|
||||
mod sbot_connection;
|
||||
|
||||
pub use sbot_connection::*;
|
||||
|
||||
// re-export types from kuska
|
||||
pub use kuska_ssb::api::dto::content::{
|
||||
FriendsHops, RelationshipQuery, SubsetQuery, SubsetQueryOptions,
|
||||
};
|
|
@ -1,135 +0,0 @@
|
|||
//! Sbot type and associated methods.
|
||||
use async_std::net::TcpStream;
|
||||
|
||||
use kuska_handshake::async_std::BoxStream;
|
||||
use kuska_sodiumoxide::crypto::{auth, sign::ed25519};
|
||||
use kuska_ssb::{
|
||||
api::ApiCaller,
|
||||
discovery, keystore,
|
||||
keystore::OwnedIdentity,
|
||||
rpc::{RpcReader, RpcWriter},
|
||||
};
|
||||
|
||||
use crate::{error::GolgiError, utils};
|
||||
|
||||
/// A struct representing a connection with a running sbot.
|
||||
/// A client and an rpc_reader can together be used to make requests to the sbot
|
||||
/// and read the responses.
|
||||
/// Note there can be multiple SbotConnection at the same time.
|
||||
pub struct SbotConnection {
|
||||
/// client for writing requests to go-bot
|
||||
pub client: ApiCaller<TcpStream>,
|
||||
/// RpcReader object for reading responses from go-sbot
|
||||
pub rpc_reader: RpcReader<TcpStream>,
|
||||
}
|
||||
|
||||
/// The Scuttlebutt identity, keys and configuration parameters for connecting to a local sbot
|
||||
pub struct Sbot {
|
||||
/// The ID (public key value) of the account associated with the local sbot instance.
|
||||
pub id: String,
|
||||
public_key: ed25519::PublicKey,
|
||||
private_key: ed25519::SecretKey,
|
||||
address: String,
|
||||
// aka caps key (scuttleverse identifier)
|
||||
network_id: auth::Key,
|
||||
}
|
||||
|
||||
impl Sbot {
|
||||
/// Initiate a connection with an sbot instance. Define the IP address, port and network key
|
||||
/// for the sbot, then retrieve the public key, private key (secret) and identity from the
|
||||
/// `.ssb-go/secret` file. Open a TCP stream to the sbot and perform the secret handshake. If successful, create a box stream and split it into a writer and reader. Return RPC handles to the sbot as part of the `struct` output.
|
||||
pub async fn connect(
|
||||
ip_port: Option<String>,
|
||||
net_id: Option<String>,
|
||||
) -> Result<Sbot, GolgiError> {
|
||||
let address = if ip_port.is_none() {
|
||||
"127.0.0.1:8008".to_string()
|
||||
} else {
|
||||
ip_port.unwrap()
|
||||
};
|
||||
|
||||
let network_id = if net_id.is_none() {
|
||||
discovery::ssb_net_id()
|
||||
} else {
|
||||
auth::Key::from_slice(&hex::decode(net_id.unwrap()).unwrap()).unwrap()
|
||||
};
|
||||
|
||||
let OwnedIdentity { pk, sk, id } = keystore::from_gosbot_local()
|
||||
.await
|
||||
.expect("couldn't read local secret");
|
||||
|
||||
Ok(Self {
|
||||
id,
|
||||
public_key: pk,
|
||||
private_key: sk,
|
||||
address,
|
||||
network_id,
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates a new connection with the sbot,
|
||||
/// using the address, network_id, public_key and private_key supplied when Sbot was initialized.
|
||||
///
|
||||
/// Note that a single Sbot can have multiple SbotConnection at the same time.
|
||||
pub async fn get_sbot_connection(&self) -> Result<SbotConnection, GolgiError> {
|
||||
let address = self.address.clone();
|
||||
let network_id = self.network_id.clone();
|
||||
let public_key = self.public_key;
|
||||
let private_key = self.private_key.clone();
|
||||
Sbot::_get_sbot_connection_helper(address, network_id, public_key, private_key).await
|
||||
}
|
||||
|
||||
/// Private helper function which creates a new connection with sbot,
|
||||
/// but with all variables passed as arguments.
|
||||
async fn _get_sbot_connection_helper(
|
||||
address: String,
|
||||
network_id: auth::Key,
|
||||
public_key: ed25519::PublicKey,
|
||||
private_key: ed25519::SecretKey,
|
||||
) -> Result<SbotConnection, GolgiError> {
|
||||
let socket = TcpStream::connect(&address)
|
||||
.await
|
||||
.map_err(|source| GolgiError::Io {
|
||||
source,
|
||||
context: "socket error; failed to initiate tcp stream connection".to_string(),
|
||||
})?;
|
||||
|
||||
let handshake = kuska_handshake::async_std::handshake_client(
|
||||
&mut &socket,
|
||||
network_id.clone(),
|
||||
public_key,
|
||||
private_key.clone(),
|
||||
public_key,
|
||||
)
|
||||
.await
|
||||
.map_err(GolgiError::Handshake)?;
|
||||
|
||||
let (box_stream_read, box_stream_write) =
|
||||
BoxStream::from_handshake(socket.clone(), socket, handshake, 0x8000).split_read_write();
|
||||
|
||||
let rpc_reader = RpcReader::new(box_stream_read);
|
||||
let client = ApiCaller::new(RpcWriter::new(box_stream_write));
|
||||
let sbot_connection = SbotConnection { rpc_reader, client };
|
||||
Ok(sbot_connection)
|
||||
}
|
||||
|
||||
/// Call the `whoami` RPC method and return an `id`.
|
||||
pub async fn whoami(&mut self) -> Result<String, GolgiError> {
|
||||
let mut sbot_connection = self.get_sbot_connection().await?;
|
||||
let req_id = sbot_connection.client.whoami_req_send().await?;
|
||||
|
||||
let result = utils::get_async(
|
||||
&mut sbot_connection.rpc_reader,
|
||||
req_id,
|
||||
utils::json_res_parse,
|
||||
)
|
||||
.await?;
|
||||
|
||||
let id = result
|
||||
.get("id")
|
||||
.ok_or_else(|| GolgiError::Sbot("id key not found on whoami call".to_string()))?
|
||||
.as_str()
|
||||
.ok_or_else(|| GolgiError::Sbot("whoami returned non-string value".to_string()))?;
|
||||
Ok(id.to_string())
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue