Skip to content

Instantly share code, notes, and snippets.

View allengeorge's full-sized avatar

Allen George allengeorge

  • New York, NY, USA
View GitHub Profile
@allengeorge
allengeorge / structure
Created September 16, 2016 11:36
Structure
+-- lib.rs (export ApiWrapper)
+-- wrapper.rs (defines ApiWrapper struct)
+-- posts.rs (writes impl block with posts functions)
+-- auth.rs (writes impl block for auth functions)
pub fn build_post_body(message: &Message) -> String {
let mut encoded = form_urlencoded::Serializer::new(String::new());
message.text.as_ref().map(|f| encoded.append_pair("text", f));
message.text.as_ref().map(|f| encoded.append_pair("parse", &f.to_string()));
encoded.finish()
}
#[derive(Serialize, Deserialize)]
pub enum MessageParseBehavior {
Full,
pub fn build_post_body(message: &Message) -> Body {
let mut encoded = form_urlencoded::Serializer::new(String::new());
message.text.as_ref().map(|t| { encoded.append_pair("text", t)});
let query_string = encoded.finish();
let query_string_bytes = query_string.to_owned().as_bytes();
Body::BufBody(query_string_bytes, query_string_bytes.len())
}
/*
let request = self.client.post(api_url); // interesting. I can't build a request without a URL. seems odd
let serialized = try!(serde_json::to_string(message_params));
let request = request.body(&serialized);
X--- error message follows
error: `serialized` does not live long enough
--> src/client.rs:76:37
|>
76 |> let request = request.body(&serialized);
#[derive(Serialize, Deserialize, Debug)]
pub struct ChatPostMessageResponse {
/// `true` if the request was successful, `false` otherwise.
pub ok: bool,
/// Only populated if there is an error.
#[serde(default)]
pub error: String,
/// Only populated if there is a warning.
pub fn deserialize<T>(response: &mut Response) -> SlackResult<T> where T: serde::Deserialize {
let mut body = String::with_capacity(INITIAL_RESPONSE_BODY_SIZE);
try!(response.read_to_string(&mut body));
print!(">>> {:?}", body);
let deserialized = try!(serde_json::from_str::<T>(&body));
Ok(deserialized) // FIXME: return from above directly
}
mod base {
pub struct Foo {
}
impl Foo {
pub fn new() -> Foo {
Foo {}
}
}
}
allen@mrwiggles ~/s/r/slacker> cargo test
Compiling slacker v0.1.0 (file:///Users/allen/src/rust_projects/slacker)
src/errors.rs:65:38: 65:50 error: mismatched types [E0308]
src/errors.rs:65 _ => SlackError::Unknown(error_string)
^~~~~~~~~~~~
src/errors.rs:65:38: 65:50 help: run `rustc --explain E0308` to see a detailed explanation
src/errors.rs:65:38: 65:50 note: expected type `std::string::String`
src/errors.rs:65:38: 65:50 note: found type `&std::string::String`
src/errors.rs:65:38: 65:50 error: mismatched types [E0308]
src/errors.rs:65 _ => SlackError::Unknown(error_string)
pub fn channels_list(&mut self, exclude_archived: bool) -> ParseResult<Vec<Channel>> {
let method_url = format!("{}/{}", SLACK_BASE_API_URL, "channels.list");
let mut url = Url::parse(&method_url).unwrap(); // FIXME
url.query_pairs_mut()
.append_pair("token", &self.access_token)
.append_pair("exclude_archived", &exclude_archived.to_string());
let url = url;
match self.client.get(url).send() {
match self.client.get(url).send() { // send() returns a ::Result<Response>
Ok(response) => { parse_response(response) }
Err(e) => { parse_send_failure(e) }
}
fn parse_response(mut response: Response) -> ParseResult<Channel> { // how do I get this to take a reference?
// ...
}