pub struct Client { /* private fields */ }Expand description
对于Onebot V11协议API调用和事件接收的高层抽象
需要具体实现 CommunicationService 的底层服务支持
§Examples
use std::time::Duration;
use onebot_api::communication::utils::Client;
use onebot_api::communication::ws::WsService;
let ws_service = WsService::new_with_options("wss://example.com", Some("example_token".to_string())).unwrap();
let client = Client::new_with_options(ws_service, Duration::from_secs(5), None, None);
client.start_service().await.unwrap();Implementations§
Source§impl Client
impl Client
pub fn subscribe_api_response(&self) -> PublicAPIResponseReceiver
pub fn subscribe_normal_event(&self) -> PublicEventReceiver
Source§impl Client
impl Client
Sourcepub fn new_with_options(
service: impl IntoService,
timeout: Option<Duration>,
public_api_response_channel_cap: Option<usize>,
public_event_channel_cap: Option<usize>,
internal_api_channel_cap: Option<usize>,
internal_event_channel_cap: Option<usize>,
echo_generator: Option<Box<dyn Fn() -> String + Send + Sync>>,
) -> Self
pub fn new_with_options( service: impl IntoService, timeout: Option<Duration>, public_api_response_channel_cap: Option<usize>, public_event_channel_cap: Option<usize>, internal_api_channel_cap: Option<usize>, internal_event_channel_cap: Option<usize>, echo_generator: Option<Box<dyn Fn() -> String + Send + Sync>>, ) -> Self
创建一个 Client 实例
§Params
service实现IntoService特征或CommunicationService特征的对象timeoutAPI请求超时时间,若为None则一直等待public_api_response_channel_capAPI响应通道容量,默认为16public_event_channel_capEvent事件通道容量,默认为16internal_api_channel_capAPI请求通道容量,默认为16internal_event_channel_cap原始事件通道容量,默认为16
pub fn new_with_union_channel_cap( service: impl IntoService, timeout: Option<Duration>, channel_cap: Option<usize>, ) -> Self
pub fn new_with_timeout( service: impl IntoService, timeout: Option<Duration>, ) -> Self
pub fn new(service: impl IntoService) -> Self
pub fn builder(service: impl IntoService) -> ClientBuilder
Source§impl Client
impl Client
Sourcepub async fn start_service(&self) -> ServiceStartResult<()>
pub async fn start_service(&self) -> ServiceStartResult<()>
启动服务
在 Client 实例构造完成或调用 change_service 后都需要调用该方法启动服务
pub fn stop_service(&self)
pub async fn restart_service(&self) -> ServiceStartResult<()>
Sourcepub fn change_service(
&mut self,
service: impl IntoService,
) -> Box<dyn CommunicationService>
pub fn change_service( &mut self, service: impl IntoService, ) -> Box<dyn CommunicationService>
更换服务
即使在原服务启动后也可以更换服务
§Examples
use std::time::Duration;
use onebot_api::communication::utils::Client;
use onebot_api::communication::ws::WsService;
use onebot_api::communication::ws_reverse::WsReverseService;
let ws_service = WsService::new_with_options("wss://example.com", Some("example_token".to_string())).unwrap();
let mut client = Client::new_with_options(ws_service, Duration::from_secs(5), None, None);
client.start_service().await.unwrap();
let ws_reverse_service = WsReverseService::new("0.0.0.0:8080", Some("example_token".to_string()));
client.change_service(ws_reverse_service);
client.start_service().await.unwrap();Sourcepub fn get_service(&self) -> &dyn CommunicationService
pub fn get_service(&self) -> &dyn CommunicationService
获取当前服务的引用
Sourcepub fn get_service_mut(&mut self) -> &mut dyn CommunicationService
pub fn get_service_mut(&mut self) -> &mut dyn CommunicationService
获取当前服务的可变引用
Source§impl Client
impl Client
Sourcepub fn generate_id() -> String
pub fn generate_id() -> String
§随机生成uuid格式的id
用于echo的生成
不要说用uuid有几率冲突
一个请求就那么点时间
这么点时间能产生一个uuid冲突建议你直接去买彩票
Sourcepub async fn get_response(&self, echo: String) -> Option<ArcAPIResponse>
pub async fn get_response(&self, echo: String) -> Option<ArcAPIResponse>
pub fn parse_response<T: DeserializeOwned>( response: ArcAPIResponse, ) -> APIResult<T>
pub async fn send_request( &self, action: String, params: JsonValue, echo: String, ) -> Result<(), SendError<APIRequest>>
Sourcepub async fn send_and_parse<T: DeserializeOwned>(
&self,
action: impl ToString,
params: JsonValue,
) -> APIResult<T>
pub async fn send_and_parse<T: DeserializeOwned>( &self, action: impl ToString, params: JsonValue, ) -> APIResult<T>
生成echo并发送API请求
同时等待API响应并自动解析
§Params
action要调用的action的名称params调用action所需要的参数
§Examples
use std::time::Duration;
use serde_json::{json, Value};
use onebot_api::communication::utils::Client;
use onebot_api::communication::ws::WsService;
let client: Client = Client::new_with_options(WsService::new_with_options("ws://localhost:8080", None).unwrap(), Some(Duration::from_secs(5)), None, None);
let response: Value = client.send_and_parse("send_like", json!({})).await.unwrap();Trait Implementations§
Source§impl APISender for Client
impl APISender for Client
fn send_private_msg<'life0, 'async_trait>(
&'life0 self,
user_id: i64,
message: Vec<SendSegment>,
auto_escape: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<i32>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn send_group_msg<'life0, 'async_trait>(
&'life0 self,
group_id: i64,
message: Vec<SendSegment>,
auto_escape: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<i32>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn send_msg<'life0, 'async_trait>(
&'life0 self,
message_type: Option<MessageType>,
user_id: i64,
group_id: i64,
message: Vec<SendSegment>,
auto_escape: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<i32>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn delete_msg<'life0, 'async_trait>(
&'life0 self,
message_id: i32,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_msg<'life0, 'async_trait>(
&'life0 self,
message_id: i32,
) -> Pin<Box<dyn Future<Output = APIResult<GetMsgResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_forward_msg<'life0, 'async_trait>(
&'life0 self,
id: String,
) -> Pin<Box<dyn Future<Output = APIResult<Vec<ReceiveSegment>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn send_like<'life0, 'async_trait>(
&'life0 self,
user_id: i64,
times: Option<i32>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_kick<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
reject_add_request: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_ban<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
duration: Option<i32>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_anonymous_ban<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
anonymous: Option<GroupMessageAnonymous>,
flag: Option<String>,
duration: Option<i32>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_whole_ban<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
enable: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_admin<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
enable: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_anonymous<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
enable: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_card<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
card: Option<String>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_name<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
group_name: String,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_leave<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
is_dismiss: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_special_title<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
special_title: Option<String>,
duration: Option<i32>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_friend_add_request<'life0, 'async_trait>(
&'life0 self,
flag: String,
approve: Option<bool>,
remark: Option<String>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_group_add_request<'life0, 'async_trait>(
&'life0 self,
flag: String,
sub_type: String,
approve: Option<bool>,
reason: Option<String>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_login_info<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<GetLoginInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_stranger_info<'life0, 'async_trait>(
&'life0 self,
user_id: i32,
no_cache: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<GetStrangerInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_friend_list<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<Vec<GetFriendListResponse>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_info<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
no_cache: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<GetGroupInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_list<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<Vec<GetGroupInfoResponse>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_member_info<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
user_id: i32,
no_cache: Option<bool>,
) -> Pin<Box<dyn Future<Output = APIResult<GetGroupMemberInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_member_list<'life0, 'async_trait>(
&'life0 self,
group_id: i32,
) -> Pin<Box<dyn Future<Output = APIResult<Vec<GetGroupMemberInfoResponse>>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_group_honor_info<'life0, 'async_trait>(
&'life0 self,
group_id: i64,
honor_type: String,
) -> Pin<Box<dyn Future<Output = APIResult<GetGroupMemberInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_csrf_token<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<i32>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_credentials<'life0, 'async_trait>(
&'life0 self,
domain: Option<String>,
) -> Pin<Box<dyn Future<Output = APIResult<GetCredentialsResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_record<'life0, 'async_trait>(
&'life0 self,
file: String,
out_format: String,
) -> Pin<Box<dyn Future<Output = APIResult<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_image<'life0, 'async_trait>(
&'life0 self,
file: String,
) -> Pin<Box<dyn Future<Output = APIResult<String>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn can_send_image<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn can_send_record<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<bool>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_status<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<GetStatusResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn get_version_info<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<GetVersionInfoResponse>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn set_restart<'life0, 'async_trait>(
&'life0 self,
delay: Option<i32>,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
fn clean_cache<'life0, 'async_trait>(
&'life0 self,
) -> Pin<Box<dyn Future<Output = APIResult<()>> + Send + 'async_trait>>where
Self: 'async_trait,
'life0: 'async_trait,
Source§impl EventReceiver<Arc<APIResponse>> for Client
impl EventReceiver<Arc<APIResponse>> for Client
fn subscribe(&self) -> PublicAPIResponseReceiver
Source§impl EventReceiver<Arc<Event>> for Client
impl EventReceiver<Arc<Event>> for Client
fn subscribe(&self) -> PublicEventReceiver
Auto Trait Implementations§
impl Freeze for Client
impl !RefUnwindSafe for Client
impl Send for Client
impl Sync for Client
impl Unpin for Client
impl UnsafeUnpin for Client
impl !UnwindSafe for Client
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more