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
#[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 test<R>(client: &R, request: &TestRequest) -> Result<TestResponse, TestError<R::Error>>
where R: SlackWebRequestSender
{
let params = vec![request.error.map(|error| ("error", error)),
request.foo.map(|foo| ("foo", foo))];
let params = params.into_iter().filter_map(|x| x).collect::<Vec<_>>();
let url = ::get_slack_url_for_method("api.test");
client
.send(&url, ¶ms[..])
.map_err(|err| TestError::Client(err))
.and_then(|result| {
serde_json::from_str::<TestResponse>(&result)
.map_err(|e| TestError::MalformedResponse(e))
})
.and_then(|o| o.into())
}
#[derive(Clone, Default, Debug)]
pub struct TestRequest<'a> {
pub error: Option<&'a str>,
pub foo: Option<&'a str>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct TestResponse {
pub args: Option<HashMap<String, bool>>,
error: Option<String>,
#[serde(default)]
ok: bool,
}
impl<E: Error> Into<Result<TestResponse, TestError<E>>> for TestResponse {
fn into(self) -> Result<TestResponse, TestError<E>> {
if self.ok {
Ok(self)
} else {
Err(self.error
.as_ref()
.map(String::as_ref)
.unwrap_or("")
.into())
}
}
}
#[derive(Debug)]
pub enum TestError<E: Error> {
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 TestError<E> {
fn from(s: &'a str) -> Self {
match s {
"invalid_arg_name" => TestError::InvalidArgName,
"invalid_array_arg" => TestError::InvalidArrayArg,
"invalid_charset" => TestError::InvalidCharset,
"invalid_form_data" => TestError::InvalidFormData,
"invalid_post_type" => TestError::InvalidPostType,
"missing_post_type" => TestError::MissingPostType,
"team_added_to_org" => TestError::TeamAddedToOrg,
"request_timeout" => TestError::RequestTimeout,
_ => TestError::Unknown(s.to_owned()),
}
}
}
impl<E: Error> fmt::Display for TestError<E> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.description())
}
}
impl<E: Error> Error for TestError<E> {
fn description(&self) -> &str {
match self {
&TestError::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.",
&TestError::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.",
&TestError::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.",
&TestError::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.",
&TestError::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.",
&TestError::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.",
&TestError::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.",
&TestError::RequestTimeout => "request_timeout: The method was called via a POST request, but the POST data was either missing or truncated.",
&TestError::MalformedResponse(ref e) => e.description(),
&TestError::Unknown(ref s) => s,
&TestError::Client(ref inner) => inner.description(),
}
}
fn cause(&self) -> Option<&Error> {
match self {
&TestError::MalformedResponse(ref e) => Some(e),
&TestError::Client(ref inner) => Some(inner),
_ => None,
}
}
}