Skip to content

Commit

Permalink
Moved old client into separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaspustina committed May 17, 2019
1 parent 6b3fa8c commit 16a6f62
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 37 deletions.
6 changes: 3 additions & 3 deletions examples/old_search.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use centerdevice::old_client::search::NamedSearches;
use centerdevice::old_client::{self, CenterDeviceConfig};
use centerdevice::old::client::search::NamedSearches;
use centerdevice::old::client::{self, CenterDeviceConfig};
use std::env;

fn main() {
Expand All @@ -18,7 +18,7 @@ fn main() {
api_base_url: "centerdevice.de".to_string(),
};

let search_results = old_client::search::search_documents(
let search_results = client::search::search_documents(
&config.api_base_url,
&config.access_token,
None,
Expand Down
20 changes: 13 additions & 7 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ use reqwest;
use reqwest::IntoUrl;


pub trait CenterDevice {
fn refresh_access_token(&self) -> Result<Token>;
}

pub struct Client {}

impl Client {
Expand Down Expand Up @@ -70,8 +74,10 @@ impl AuthorizedClient {
pub fn token(&self) -> &Token {
&self.token
}
}

pub fn refresh_access_token(&self) -> Result<Token> {
impl CenterDevice for AuthorizedClient {
fn refresh_access_token(&self) -> Result<Token> {
auth::refresh_access_token(self)
}
}
Expand Down Expand Up @@ -206,16 +212,16 @@ mod auth {
Ok(token)
}

pub fn refresh_access_token(client: &AuthorizedClient) -> Result<Token> {
let url = format!("https://auth.{}/token", client.base_url);
let params = [("grant_type", "refresh_token"), ("refresh_token", &client.token.refresh_token)];
pub fn refresh_access_token(authorized_client: &AuthorizedClient) -> Result<Token> {
let url = format!("https://auth.{}/token", authorized_client.base_url);
let params = [("grant_type", "refresh_token"), ("refresh_token", &authorized_client.token.refresh_token)];

let token = client
let token = authorized_client
.http_client
.post(&url)
.basic_auth(
&client.client_credentials.client_id,
Some(&client.client_credentials.client_secret),
&authorized_client.client_credentials.client_id,
Some(&authorized_client.client_credentials.client_secret),
)
.form(&params)
.send()
Expand Down
18 changes: 1 addition & 17 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,2 @@
pub mod client;
pub(crate) mod net;
pub mod old_client;
pub(crate) mod utils;

use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct GeneralConfig {
pub output_format: bool,
pub verbosity: bool,
}

#[derive(Debug, Deserialize)]
pub struct Config {
pub general: GeneralConfig,
pub centerdevice: old_client::CenterDeviceConfig,
}
pub mod old;
14 changes: 7 additions & 7 deletions src/old_client.rs → src/old/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn prepare_request<'a, 'b>(client: &'a Client, method: Method, url: &'b str, tok

pub mod collections {
use super::prepare_request;
use crate::net::http::tls_client;
use crate::old::net::http::tls_client;

use error_chain::*;
use hyper::method::Method;
Expand Down Expand Up @@ -124,7 +124,7 @@ pub mod collections {

mod delete {
use super::prepare_request;
use crate::net::http::tls_client;
use crate::old::net::http::tls_client;

use error_chain::*;
use hyper::method::Method;
Expand Down Expand Up @@ -190,8 +190,8 @@ mod delete {

mod download {
use super::prepare_request;
use crate::net::http::tls_client;
use crate::utils::io::ReadWithProgress;
use crate::old::net::http::tls_client;
use crate::old::utils::io::ReadWithProgress;

use error_chain::*;
use hyper::client::Response;
Expand Down Expand Up @@ -313,7 +313,7 @@ mod download {
}

mod refresh_token {
use crate::net::http::tls_client;
use crate::old::net::http::tls_client;

use error_chain::*;
use hyper::header::{ContentType, Authorization, Basic};
Expand Down Expand Up @@ -356,7 +356,7 @@ mod refresh_token {

pub mod search {
use super::prepare_request;
use crate::net::http::tls_client;
use crate::old::net::http::tls_client;

use error_chain::*;
use hyper::header::{ContentType, Accept, qitem};
Expand Down Expand Up @@ -482,7 +482,7 @@ pub mod search {
}

mod upload {
use crate::utils::io::WriteWithProgress;
use crate::old::utils::io::WriteWithProgress;

use crypto::digest::Digest;
use crypto::sha2::Sha256;
Expand Down
17 changes: 17 additions & 0 deletions src/old/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
mod net;
pub mod client;
mod utils;

use serde::Deserialize;

#[derive(Debug, Deserialize)]
pub struct GeneralConfig {
pub output_format: bool,
pub verbosity: bool,
}

#[derive(Debug, Deserialize)]
pub struct Config {
pub general: GeneralConfig,
pub centerdevice: client::CenterDeviceConfig,
}
File renamed without changes.
1 change: 0 additions & 1 deletion src/net/mod.rs → src/old/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod oauth;

pub mod http;

4 changes: 2 additions & 2 deletions src/net/oauth.rs → src/old/net/oauth.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::Config;
use crate::net::http::tls_client;
use crate::old::Config;
use crate::old::net::http::tls_client;

use base64;
use error_chain::*;
Expand Down
File renamed without changes.

0 comments on commit 16a6f62

Please sign in to comment.