1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#[allow(unused_imports)]
use std::collections::HashMap;
use std::convert::From;
use std::error::Error;
use std::fmt;
use serde_json;
use requests::SlackWebRequestSender;
pub fn info<R>(client: &R,
token: &str,
request: &InfoRequest)
-> Result<InfoResponse, InfoError<R::Error>>
where R: SlackWebRequestSender
{
let params = vec![Some(("token", token)), request.bot.map(|bot| ("bot", bot))];
let params = params.into_iter().filter_map(|x| x).collect::<Vec<_>>();
let url = ::get_slack_url_for_method("bots.info");
client
.send(&url, ¶ms[..])
.map_err(|err| InfoError::Client(err))
.and_then(|result| {
serde_json::from_str::<InfoResponse>(&result)
.map_err(|e| InfoError::MalformedResponse(e))
})
.and_then(|o| o.into())
}
#[derive(Clone, Default, Debug)]
pub struct InfoRequest<'a> {
pub bot: Option<&'a str>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct InfoResponse {
pub bot: Option<InfoResponseBot>,
error: Option<String>,
#[serde(default)]
ok: bool,
}
#[derive(Clone, Debug, Deserialize)]
pub struct InfoResponseBot {
pub app_id: Option<String>,
pub deleted: Option<bool>,
pub icons: Option<InfoResponseBotIcons>,
pub id: Option<String>,
pub name: Option<String>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct InfoResponseBotIcons {
pub image_36: Option<String>,
pub image_48: Option<String>,
pub image_72: Option<String>,
}
impl<E: Error> Into<Result<InfoResponse, InfoError<E>>> for InfoResponse {
fn into(self) -> Result<InfoResponse, InfoError<E>> {
if self.ok {
Ok(self)
} else {
Err(self.error
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.into())
}
}
}
#[derive(Debug)]
pub enum InfoError<E: Error> {
BotNotFound,
NotAuthed,
InvalidAuth,
AccountInactive,
UserIsBot,
InvalidArgName,
InvalidArrayArg,
InvalidCharset,
InvalidFormData,
InvalidPostType,
MissingPostType,
TeamAddedToOrg,
RequestTimeout,
MalformedResponse(serde_json::error::Error),
Unknown(String),
Client(E),
}
impl<'a, E: Error> From<&'a str> for InfoError<E> {
fn from(s: &'a str) -> Self {
match s {
"bot_not_found" => InfoError::BotNotFound,
"not_authed" => InfoError::NotAuthed,
"invalid_auth" => InfoError::InvalidAuth,
"account_inactive" => InfoError::AccountInactive,
"user_is_bot" => InfoError::UserIsBot,
"invalid_arg_name" => InfoError::InvalidArgName,
"invalid_array_arg" => InfoError::InvalidArrayArg,
"invalid_charset" => InfoError::InvalidCharset,
"invalid_form_data" => InfoError::InvalidFormData,
"invalid_post_type" => InfoError::InvalidPostType,
"missing_post_type" => InfoError::MissingPostType,
"team_added_to_org" => InfoError::TeamAddedToOrg,
"request_timeout" => InfoError::RequestTimeout,
_ => InfoError::Unknown(s.to_owned()),
}
}
}
impl<E: Error> fmt::Display for InfoError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl<E: Error> Error for InfoError<E> {
fn description(&self) -> &str {
match self {
&InfoError::BotNotFound => "bot_not_found: Value passed for bot was invalid.",
&InfoError::NotAuthed => "not_authed: No authentication token provided.",
&InfoError::InvalidAuth => "invalid_auth: Invalid authentication token.",
&InfoError::AccountInactive => "account_inactive: Authentication token is for a deleted user or team.",
&InfoError::UserIsBot => "user_is_bot: This method cannot be called by a bot user.",
&InfoError::InvalidArgName => "invalid_arg_name: The method was passed an argument whose name falls outside the bounds of common decency. This includes very long names and names with non-alphanumeric characters other than _. If you get this error, it is typically an indication that you have made a very malformed API call.",
&InfoError::InvalidArrayArg => "invalid_array_arg: The method was passed a PHP-style array argument (e.g. with a name like foo[7]). These are never valid with the Slack API.",
&InfoError::InvalidCharset => "invalid_charset: The method was called via a POST request, but the charset specified in the Content-Type header was invalid. Valid charset names are: utf-8 iso-8859-1.",
&InfoError::InvalidFormData => "invalid_form_data: The method was called via a POST request with Content-Type application/x-www-form-urlencoded or multipart/form-data, but the form data was either missing or syntactically invalid.",
&InfoError::InvalidPostType => "invalid_post_type: The method was called via a POST request, but the specified Content-Type was invalid. Valid types are: application/x-www-form-urlencoded multipart/form-data text/plain.",
&InfoError::MissingPostType => "missing_post_type: The method was called via a POST request and included a data payload, but the request did not include a Content-Type header.",
&InfoError::TeamAddedToOrg => "team_added_to_org: The team associated with your request is currently undergoing migration to an Enterprise Organization. Web API and other platform operations will be intermittently unavailable until the transition is complete.",
&InfoError::RequestTimeout => "request_timeout: The method was called via a POST request, but the POST data was either missing or truncated.",
&InfoError::MalformedResponse(ref e) => e.description(),
&InfoError::Unknown(ref s) => s,
&InfoError::Client(ref inner) => inner.description(),
}
}
fn cause(&self) -> Option<&Error> {
match self {
&InfoError::MalformedResponse(ref e) => Some(e),
&InfoError::Client(ref inner) => Some(inner),
_ => None,
}
}
}