Update code to be compatible with reqwest 0.9

master
Paul Woolcock 6 years ago
parent 7f0d57038f
commit 2d8958e0d9
  1. 1
      Cargo.toml
  2. 2
      src/http_send.rs
  3. 23
      src/lib.rs
  4. 8
      src/macros.rs
  5. 13
      src/page.rs
  6. 6
      src/registration.rs

@ -22,6 +22,7 @@ url = "1"
tap-reader = "1" tap-reader = "1"
try_from = "0.2.2" try_from = "0.2.2"
toml = { version = "0.4.6", optional = true } toml = { version = "0.4.6", optional = true }
hyper-old-types = "0.11.0"
[dependencies.chrono] [dependencies.chrono]
version = "0.4" version = "0.4"

@ -9,7 +9,7 @@ pub trait HttpSend: Clone + Debug {
/// Convenience method so that .build() doesn't have to be called at every /// Convenience method so that .build() doesn't have to be called at every
/// call site /// call site
fn send(&self, client: &Client, builder: &mut RequestBuilder) -> Result<Response> { fn send(&self, client: &Client, builder: RequestBuilder) -> Result<Response> {
let request = builder.build()?; let request = builder.build()?;
self.execute(client, request) self.execute(client, request)
} }

@ -46,6 +46,7 @@
extern crate serde_derive; extern crate serde_derive;
#[macro_use] #[macro_use]
extern crate doc_comment; extern crate doc_comment;
extern crate hyper_old_types;
extern crate isolang; extern crate isolang;
#[macro_use] #[macro_use]
extern crate serde_json; extern crate serde_json;
@ -72,12 +73,7 @@ extern crate indoc;
use std::{borrow::Cow, ops}; use std::{borrow::Cow, ops};
use reqwest::{ use reqwest::{Client, RequestBuilder, Response};
header::{Authorization, Bearer, Headers},
Client,
RequestBuilder,
Response,
};
use tap_reader::Tap; use tap_reader::Tap;
use entities::prelude::*; use entities::prelude::*;
@ -139,7 +135,6 @@ pub mod prelude {
pub struct Mastodon<H: HttpSend = HttpSender> { pub struct Mastodon<H: HttpSend = HttpSender> {
client: Client, client: Client,
http_sender: H, http_sender: H,
headers: Headers,
/// Raw data about your mastodon instance. /// Raw data about your mastodon instance.
pub data: Data, pub data: Data,
} }
@ -151,10 +146,10 @@ impl<H: HttpSend> Mastodon<H> {
format!("{}{}", self.base, url) format!("{}{}", self.base, url)
} }
pub(crate) fn send(&self, req: &mut RequestBuilder) -> Result<Response> { pub(crate) fn send(&self, req: RequestBuilder) -> Result<Response> {
Ok(self Ok(self
.http_sender .http_sender
.send(&self.client, req.headers(self.headers.clone()))?) .send(&self.client, req.bearer_auth(&self.token))?)
} }
} }
@ -361,7 +356,7 @@ impl<H: HttpSend> MastodonClient<H> for Mastodon<H> {
url = format!("{}{}", url, request.to_querystring()); url = format!("{}{}", url, request.to_querystring());
} }
let response = self.send(&mut self.client.get(&url))?; let response = self.send(self.client.get(&url))?;
Page::new(self, response) Page::new(self, response)
} }
@ -383,7 +378,7 @@ impl<H: HttpSend> MastodonClient<H> for Mastodon<H> {
url.pop(); url.pop();
} }
let response = self.send(&mut self.client.get(&url))?; let response = self.send(self.client.get(&url))?;
Page::new(self, response) Page::new(self, response)
} }
@ -449,15 +444,9 @@ impl<H: HttpSend> MastodonBuilder<H> {
pub fn build(self) -> Result<Mastodon<H>> { pub fn build(self) -> Result<Mastodon<H>> {
Ok(if let Some(data) = self.data { Ok(if let Some(data) = self.data {
let mut headers = Headers::new();
headers.set(Authorization(Bearer {
token: (*data.token).to_owned(),
}));
Mastodon { Mastodon {
client: self.client.unwrap_or_else(|| Client::new()), client: self.client.unwrap_or_else(|| Client::new()),
http_sender: self.http_sender, http_sender: self.http_sender,
headers,
data, data,
} }
} else { } else {

@ -5,7 +5,7 @@ macro_rules! methods {
-> Result<T> -> Result<T>
{ {
let response = self.send( let response = self.send(
&mut self.client.$method(&url) self.client.$method(&url)
)?; )?;
deserialise(response) deserialise(response)
@ -43,7 +43,7 @@ macro_rules! paged_routes {
fn $name(&self) -> Result<Page<$ret, H>> { fn $name(&self) -> Result<Page<$ret, H>> {
let url = self.route(concat!("/api/v1/", $url)); let url = self.route(concat!("/api/v1/", $url));
let response = self.send( let response = self.send(
&mut self.client.$method(&url) self.client.$method(&url)
)?; )?;
Page::new(self, response) Page::new(self, response)
@ -88,7 +88,7 @@ macro_rules! paged_routes {
let url = format!(concat!("/api/v1/", $url, "?{}"), &qs); let url = format!(concat!("/api/v1/", $url, "?{}"), &qs);
let response = self.send( let response = self.send(
&mut self.client.get(&url) self.client.get(&url)
)?; )?;
Page::new(self, response) Page::new(self, response)
@ -350,7 +350,7 @@ macro_rules! paged_routes_with_id {
fn $name(&self, id: &str) -> Result<Page<$ret, H>> { fn $name(&self, id: &str) -> Result<Page<$ret, H>> {
let url = self.route(&format!(concat!("/api/v1/", $url), id)); let url = self.route(&format!(concat!("/api/v1/", $url), id));
let response = self.send( let response = self.send(
&mut self.client.$method(&url) self.client.$method(&url)
)?; )?;
Page::new(self, response) Page::new(self, response)

@ -1,9 +1,7 @@
use super::{deserialise, Mastodon, Result}; use super::{deserialise, Mastodon, Result};
use entities::itemsiter::ItemsIter; use entities::itemsiter::ItemsIter;
use reqwest::{ use hyper_old_types::header::{parsing, Link, RelationType};
header::{Link, RelationType}, use reqwest::{header::LINK, Response};
Response,
};
use serde::Deserialize; use serde::Deserialize;
use url::Url; use url::Url;
@ -32,7 +30,7 @@ macro_rules! pages {
}; };
let response = self.mastodon.send( let response = self.mastodon.send(
&mut self.mastodon.client.get(url) self.mastodon.client.get(url)
)?; )?;
let (prev, next) = get_links(&response)?; let (prev, next) = get_links(&response)?;
@ -108,7 +106,10 @@ fn get_links(response: &Response) -> Result<(Option<Url>, Option<Url>)> {
let mut prev = None; let mut prev = None;
let mut next = None; let mut next = None;
if let Some(link_header) = response.headers().get::<Link>() { if let Some(link_header) = response.headers().get(LINK) {
let link_header = link_header.to_str().unwrap();
let link_header = link_header.as_bytes();
let link_header: Link = parsing::from_raw_str(&link_header).unwrap();
for value in link_header.values() { for value in link_header.values() {
if let Some(relations) = value.rel() { if let Some(relations) = value.rel() {
if relations.contains(&RelationType::Next) { if relations.contains(&RelationType::Next) {

@ -99,7 +99,7 @@ impl<'a, H: HttpSend> Registration<'a, H> {
self self
} }
fn send(&self, req: &mut RequestBuilder) -> Result<Response> { fn send(&self, req: RequestBuilder) -> Result<Response> {
Ok(self.http_sender.send(&self.client, req)?) Ok(self.http_sender.send(&self.client, req)?)
} }
@ -184,7 +184,7 @@ impl<'a, H: HttpSend> Registration<'a, H> {
} }
impl<H: HttpSend> Registered<H> { impl<H: HttpSend> Registered<H> {
fn send(&self, req: &mut RequestBuilder) -> Result<Response> { fn send(&self, req: RequestBuilder) -> Result<Response> {
Ok(self.http_sender.send(&self.client, req)?) Ok(self.http_sender.send(&self.client, req)?)
} }
@ -213,7 +213,7 @@ impl<H: HttpSend> Registered<H> {
self.redirect self.redirect
); );
let token: AccessToken = self.send(&mut self.client.post(&url))?.json()?; let token: AccessToken = self.send(self.client.post(&url))?.json()?;
let data = Data { let data = Data {
base: self.base.into(), base: self.base.into(),

Loading…
Cancel
Save