add DingTalk::from_token

This commit is contained in:
2020-05-04 21:29:29 +08:00
parent 825de10df7
commit bf3c5debae
3 changed files with 24 additions and 1 deletions

View File

@@ -183,6 +183,27 @@ impl <'a> DingTalkMessage<'a> {
impl <'a> DingTalk<'a> {
/// Create `DingTalk` from token:
/// wechatwork:access_token
/// dingtalk:access_token?sec_token
pub fn from_token(token: &str) -> XResult<Self> {
if token.starts_with("dingtalk:") {
let token_and_or_sec = &token["dingtalk:".len()..];
let mut token_and_or_sec_vec = token_and_or_sec.split('?');
let access_token = match token_and_or_sec_vec.next() {
Some(t) => t, None => token_and_or_sec,
};
let sec_token = match token_and_or_sec_vec.next() {
Some(t) => t, None => "",
};
Ok(Self::new(Self::string_to_a_str(access_token), Self::string_to_a_str(sec_token)))
} else if token.starts_with("wechatwork:") {
Ok(Self::new_wechat(Self::string_to_a_str(&token["wechatwork:".len()..])))
} else {
Err(Box::new(Error::new(ErrorKind::Other, format!("Tokne format erorr: {}", token))))
}
}
/// Create `DingTalk` from file
///
/// Format see `DingTalk::from_json(json: &str)`