Skip to content

Commit

Permalink
Can use proxy
Browse files Browse the repository at this point in the history
Signed-off-by: Thibault Meyer <meyer.thibault@gmail.com>
  • Loading branch information
thibaultmeyer committed Jul 25, 2023
1 parent eece335 commit b6b657d
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 7 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ Once the configuration file has been checked. Run the following command to chang
```


## Configuration file

* `image_dimension_width` The "width" dimension of the wallpaper
* `image_dimension_height` The "height" dimension of the wallpaper
* `target_filename` The location where is stored the wallpaper
* `proxy_url` (OPTIONAL) The proxy URL (ie: http://127.0.0.1:8080)

**Note:** You can use "#" to comment a line



## Change wallpaper automatically

Expand Down
31 changes: 25 additions & 6 deletions src/bingwallpaper/bingapiclient.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use std::fs::File;
use std::io::Cursor;
use std::time::{SystemTime, UNIX_EPOCH};
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use reqwest::blocking::Client;
use serde_derive::Deserialize;

/// Bing API HTTP client.
pub struct BingAPIClient {
api_endpoint: String,
http_client: Client,
}

/// Bing API "Images Archives": root object.
Expand Down Expand Up @@ -38,16 +40,33 @@ pub struct BingAPIImagesArchiveImage {
impl BingAPIClient {
/// Creates a new instance.
///
/// # Arguments
/// * `proxy_url` - URL to the proxy to use (ie: http://proxy-ip:8080)
///
/// # Examples
///
/// ```
/// use bingwallpaper::BingAPIClient;
/// let instance = BingAPIClient::new();
/// ```
#[must_use]
pub fn new() -> BingAPIClient {
pub fn new(proxy_url: Option<String>) -> BingAPIClient {
// Configures HTTP client
let mut client_builder = Client::builder()
.timeout(Duration::from_secs(15))
.connect_timeout(Duration::from_secs(10))
.pool_idle_timeout(Duration::from_secs(35))
.pool_max_idle_per_host(1);

if let Some(value) = proxy_url {
let proxy = reqwest::Proxy::all(value).unwrap();
client_builder = client_builder.proxy(proxy)
}

// Creates new instance
BingAPIClient {
api_endpoint: String::from("https://www.bing.com")
api_endpoint: String::from("https://www.bing.com"),
http_client: client_builder.build().unwrap(),
}
}

Expand Down Expand Up @@ -76,7 +95,7 @@ impl BingAPIClient {
img_dimension_height);

// Call Bing API
let http_response = match reqwest::blocking::get(image_archive_api_uri) {
let http_response = match self.http_client.get(image_archive_api_uri).send() {
Err(error) => return Err(format!("Can't fetch Bing API: {:?}", error)),
Ok(http_response) => http_response,
};
Expand All @@ -90,7 +109,7 @@ impl BingAPIClient {
// Returns the latest image
let image = image_archive.images.get(0).unwrap();

Ok(BingAPIImagesArchiveImage { // TODO: possible to implement ".clone()" or ".copy" method ????
Ok(BingAPIImagesArchiveImage {
url: image.url.clone(),
title: image.title.clone(),
copyright: image.copyright.clone(),
Expand All @@ -115,7 +134,7 @@ impl BingAPIClient {
/// ```
pub fn download_image(&self, image: &BingAPIImagesArchiveImage, target: &String) -> Result<(), String> {
let image_content_uri: String = format!("{0}{1}", self.api_endpoint, image.url);
let image_response = reqwest::blocking::get(image_content_uri).unwrap();
let image_response = self.http_client.get(image_content_uri).send().unwrap();
let mut output_file = File::create(target).unwrap();
let mut image_content = Cursor::new(image_response.bytes().unwrap());

Expand Down
5 changes: 4 additions & 1 deletion src/bingwallpaper/bingwallpaperchanger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use std::process::Command;
use std::time::SystemTime;

use chrono::{DateTime, Utc};

#[cfg(target_os = "windows")]
use winapi::ctypes::c_void;
#[cfg(target_os = "windows")]
Expand Down Expand Up @@ -47,9 +48,11 @@ impl BingWallpaperChanger {
/// ```
#[must_use]
pub fn new(configuration: BingWallpaperConfiguration) -> BingWallpaperChanger {
let proxy_url = configuration.proxy_url.clone();

BingWallpaperChanger {
configuration,
bing_api_client: BingAPIClient::new(),
bing_api_client: BingAPIClient::new(proxy_url),
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/bingwallpaper/configuration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ pub struct BingWallpaperConfiguration {
pub(crate) image_dimension_width: u32,
pub(crate) image_dimension_height: u32,
pub(crate) target_filename: String,
pub(crate) proxy_url: Option<String>,
}


Expand All @@ -22,6 +23,7 @@ impl Default for BingWallpaperConfiguration {
image_dimension_height: 1080,
image_dimension_width: 1920,
target_filename: "/tmp/bingwallpaper.png".into(),
proxy_url: None,
}
}
}
Expand Down

0 comments on commit b6b657d

Please sign in to comment.