Skip to content

Commit

Permalink
Add some headers by default
Browse files Browse the repository at this point in the history
  • Loading branch information
DoumanAsh committed Jan 26, 2018
1 parent 4f2d91a commit c4efd38
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "fie"
version = "0.6.2"
version = "0.6.3"
authors = ["Douman <[email protected]>"]
repository = "https://github.com/DoumanAsh/fie"
description = "Small and cute twitter app."
Expand Down
8 changes: 6 additions & 2 deletions src/api/gab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ::serde_json;
use super::http;
use self::http::{
MultipartBody,
DefaultHeaders
};

use ::config;
Expand Down Expand Up @@ -86,8 +87,10 @@ impl<'a> Client<'a> {

///Uploads image to gab.ai.
pub fn upload_image(&self, image: &Image) -> http::FutureResponse {
let mut req = http::Request::new(http::Method::Post, IMAGES_URL.parse().unwrap());
req.headers_mut().set(self.auth());
let uri = format!("{}?token={}", IMAGES_URL, &self.config.token);
let mut req = http::Request::new(http::Method::Post, uri.parse().unwrap());
req.set_default_headers();
req.headers_mut().set(http::Referer::new(uri));
req.set_multipart_body("-fie", &image.name, &image.mime, &image.content);

self.http.request(req)
Expand All @@ -98,6 +101,7 @@ impl<'a> Client<'a> {
let message = payload::Post::new(message, images, flags);

let mut req = http::Request::new(http::Method::Post, POST_URL.parse().unwrap());
req.set_default_headers();
req.headers_mut().set(http::ContentType::json());
req.headers_mut().set(self.auth());
req.set_body(serde_json::to_string(&message).unwrap());
Expand Down
13 changes: 12 additions & 1 deletion src/api/http.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
pub use ::hyper::{Client, Request, Method, Response, StatusCode};
pub use ::hyper::header::{Accept, ContentType, ContentLength, Authorization, Bearer};
pub use ::hyper::header::{Accept, Referer, ContentType, ContentLength, Authorization, Bearer, UserAgent};
pub use ::hyper::client::{HttpConnector, FutureResponse};
pub use ::hyper_tls::{HttpsConnector};
pub use ::hyper::mime;
use ::tokio_core::reactor::{
Handle
};

pub trait DefaultHeaders {
fn set_default_headers(&mut self);
}

impl DefaultHeaders for Request {
fn set_default_headers(&mut self) {
self.headers_mut().set(Accept::json());
self.headers_mut().set(UserAgent::new("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.87 Safari/537.36"));
}
}

pub trait MultipartBody {
fn set_multipart_body(&mut self, boundary: &str, file_name: &str, mime: &mime::Mime, data: &[u8]);
}
Expand Down
5 changes: 4 additions & 1 deletion src/api/minds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use ::utils::{
use super::http;
use self::http::{
MultipartBody,
DefaultHeaders
};

use ::cli::PostFlags;
Expand Down Expand Up @@ -98,7 +99,7 @@ impl<'a> Client<'a> {
pub fn new(http: &'a http::HttpClient, core: &mut Core, config: config::Minds) -> Result<Self, String> {
let oauth2 = {
let mut req = http::Request::new(http::Method::Post, OAUTH2_URL.parse().unwrap());
req.headers_mut().set(http::Accept::json());
req.set_default_headers();
req.headers_mut().set(http::ContentType::json());
let auth_body = payload::Auth::new(config.username, config.password);
req.set_body(serde_json::to_string(&auth_body).unwrap());
Expand All @@ -124,6 +125,7 @@ impl<'a> Client<'a> {
///NOTE: Minds.com allows only one attachment
pub fn upload_image(&self, image: &Image) -> http::FutureResponse {
let mut req = http::Request::new(http::Method::Post, IMAGES_URL.parse().unwrap());
req.set_default_headers();
req.headers_mut().set(self.auth());
req.set_multipart_body("-fie", &image.name, &image.mime, &image.content);

Expand All @@ -135,6 +137,7 @@ impl<'a> Client<'a> {
let message = payload::Post::new(message, attachment_guid, flags);

let mut req = http::Request::new(http::Method::Post, POST_URL.parse().unwrap());
req.set_default_headers();
req.headers_mut().set(http::ContentType::json());
req.headers_mut().set(self.auth());
req.set_body(serde_json::to_string(&message).unwrap());
Expand Down

0 comments on commit c4efd38

Please sign in to comment.