Skip to content

Commit

Permalink
refactor: change trait name SendRequest -> HttpClient (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
mgrachev authored Feb 17, 2023
1 parent 9b4c009 commit 5ca5b0b
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 49 deletions.
12 changes: 5 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
[update-informer]: https://evrone.com/update-informer?utm_source=github&utm_campaign=update-informer
[Evrone]: https://evrone.com/?utm_source=github&utm_campaign=update-informer
[turbo]: https://github.com/vercel/turbo
[ruff]: https://github.com/charliermarsh/ruff
[reqwest]: https://github.com/seanmonstar/reqwest

[![CI][ci-badge]][ci-url]
Expand Down Expand Up @@ -147,7 +146,7 @@ let _ = informer.check_version();
You can implement your own registry to check updates. For example:

```rust
use update_informer::{http_client::{HttpClient, SendRequest}, registry, Check, Package, Registry, Result};
use update_informer::{http_client::{GenericHttpClient, HttpClient}, registry, Check, Package, Registry, Result};

#[derive(serde::Deserialize)]
struct Response {
Expand All @@ -158,15 +157,15 @@ struct YourOwnRegistry;
impl Registry for YourOwnRegistry {
const NAME: &'static str = "your_own_registry";

fn get_latest_version<T: SendRequest>(http_client: HttpClient<T>, pkg: &Package) -> Result<Option<String>> {
fn get_latest_version<T: HttpClient>(http_client: GenericHttpClient<T>, pkg: &Package) -> Result<Option<String>> {
let url = "https://turbo.build/api/binaries/version";
let resp = http_client.get::<Response>(&url)?;

Ok(Some(resp.version))
}
}

let informer = update_informer::new(YourOwnRegistry, "package_name", "0.1.0");
let informer = update_informer::new(YourOwnRegistry, "turbo", "0.1.0");
informer.check_version();
```

Expand All @@ -178,11 +177,11 @@ You can use your own HTTP client to check updates. For example:
use isahc::ReadResponseExt;
use std::time::Duration;
use serde::de::DeserializeOwned;
use update_informer::{http_client::SendRequest, registry, Check};
use update_informer::{http_client::HttpClient, registry, Check};

struct YourOwnHttpClient;

impl SendRequest for YourOwnHttpClient {
impl HttpClient for YourOwnHttpClient {
fn get<T: DeserializeOwned>(
url: &str,
_timeout: Duration,
Expand Down Expand Up @@ -249,7 +248,6 @@ let _ = informer.check_version();
- [git-cliff]
- [dotenv-linter]
- [turbo]
- [ruff]

## MSRV

Expand Down
4 changes: 2 additions & 2 deletions examples/fake.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use serde::de::DeserializeOwned;
use std::time::Duration;
use update_informer::{http_client::SendRequest, registry, Check};
use update_informer::{http_client::HttpClient, registry, Check};

struct YourOwnHttpClient;

impl SendRequest for YourOwnHttpClient {
impl HttpClient for YourOwnHttpClient {
fn get<T: DeserializeOwned>(
_url: &str,
_timeout: Duration,
Expand Down
4 changes: 2 additions & 2 deletions examples/http_client.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use isahc::ReadResponseExt;
use serde::de::DeserializeOwned;
use std::time::Duration;
use update_informer::{http_client::SendRequest, registry, Check};
use update_informer::{http_client::HttpClient, registry, Check};

struct YourOwnHttpClient;

impl SendRequest for YourOwnHttpClient {
impl HttpClient for YourOwnHttpClient {
fn get<T: DeserializeOwned>(
url: &str,
_timeout: Duration,
Expand Down
6 changes: 3 additions & 3 deletions examples/registry.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::time::Duration;
use update_informer::{
http_client::{HttpClient, SendRequest},
http_client::{GenericHttpClient, HttpClient},
Check, Package, Registry, Result,
};

Expand All @@ -13,8 +13,8 @@ struct YourOwnRegistry;
impl Registry for YourOwnRegistry {
const NAME: &'static str = "your_own_registry";

fn get_latest_version<T: SendRequest>(
http_client: HttpClient<T>,
fn get_latest_version<T: HttpClient>(
http_client: GenericHttpClient<T>,
_pkg: &Package,
) -> Result<Option<String>> {
let url = "https://turbo.build/api/binaries/version";
Expand Down
Binary file removed images/example.png
Binary file not shown.
10 changes: 5 additions & 5 deletions src/http_client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,21 @@ pub use crate::http_client::undefined::UndefinedHttpClient;
pub type DefaultHttpClient = UndefinedHttpClient;

/// An HTTP client to send requests to the registry.
pub struct HttpClient<'a, T: SendRequest> {
pub struct GenericHttpClient<'a, T: HttpClient> {
_inner: T,
timeout: Duration,
headers: Option<(&'a str, &'a str)>,
}

pub(crate) fn new<'a, T: SendRequest>(client: T, timeout: Duration) -> HttpClient<'a, T> {
HttpClient {
pub(crate) fn new<'a, T: HttpClient>(client: T, timeout: Duration) -> GenericHttpClient<'a, T> {
GenericHttpClient {
_inner: client,
timeout,
headers: None,
}
}

impl<'a, T: SendRequest> HttpClient<'a, T> {
impl<'a, T: HttpClient> GenericHttpClient<'a, T> {
pub fn headers(self, headers: (&'a str, &'a str)) -> Self {
Self {
headers: Some(headers),
Expand All @@ -51,7 +51,7 @@ impl<'a, T: SendRequest> HttpClient<'a, T> {
}
}

pub trait SendRequest {
pub trait HttpClient {
fn get<T: DeserializeOwned>(
url: &str,
timeout: Duration,
Expand Down
4 changes: 2 additions & 2 deletions src/http_client/reqwest.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{http_client::SendRequest, Result};
use crate::{http_client::HttpClient, Result};
use serde::de::DeserializeOwned;
use std::time::Duration;

pub struct ReqwestHttpClient;

impl SendRequest for ReqwestHttpClient {
impl HttpClient for ReqwestHttpClient {
fn get<T: DeserializeOwned>(
url: &str,
timeout: Duration,
Expand Down
4 changes: 2 additions & 2 deletions src/http_client/undefined.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{http_client::SendRequest, Result};
use crate::{http_client::HttpClient, Result};
use serde::de::DeserializeOwned;
use std::time::Duration;

pub struct UndefinedHttpClient;

impl SendRequest for UndefinedHttpClient {
impl HttpClient for UndefinedHttpClient {
fn get<T: DeserializeOwned>(
_url: &str,
_timeout: Duration,
Expand Down
4 changes: 2 additions & 2 deletions src/http_client/ureq.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::{http_client::SendRequest, Result};
use crate::{http_client::HttpClient, Result};
use serde::de::DeserializeOwned;
use std::time::Duration;

pub struct UreqHttpClient;

impl SendRequest for UreqHttpClient {
impl HttpClient for UreqHttpClient {
fn get<T: DeserializeOwned>(
url: &str,
timeout: Duration,
Expand Down
18 changes: 9 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![doc = include_str!("../README.md")]

use crate::{
http_client::{DefaultHttpClient, SendRequest},
http_client::{DefaultHttpClient, HttpClient},
version_file::VersionFile,
};
use std::time::Duration;
Expand Down Expand Up @@ -41,7 +41,7 @@ pub struct UpdateInformer<
R: Registry,
N: AsRef<str>,
V: AsRef<str>,
H: SendRequest = DefaultHttpClient,
H: HttpClient = DefaultHttpClient,
> {
_registry: R,
name: N,
Expand Down Expand Up @@ -89,7 +89,7 @@ where
R: Registry,
N: AsRef<str>,
V: AsRef<str>,
H: SendRequest,
H: HttpClient,
{
/// Sets an interval how often to check for a new version.
///
Expand Down Expand Up @@ -137,19 +137,19 @@ where
///
/// # Arguments
///
/// * `http_client` - A type that implements the `SendRequest` trait.
/// * `http_client` - A type that implements the `HttpClient` trait.
///
/// # Examples
///
/// ```rust
/// use isahc::ReadResponseExt;
/// use std::time::Duration;
/// use serde::de::DeserializeOwned;
/// use update_informer::{http_client::SendRequest, registry, Check};
/// use update_informer::{http_client::HttpClient, registry, Check};
///
/// struct YourOwnHttpClient;
///
/// impl SendRequest for YourOwnHttpClient {
/// impl HttpClient for YourOwnHttpClient {
/// fn get<T: DeserializeOwned>(
/// url: &str,
/// _timeout: Duration,
Expand All @@ -163,7 +163,7 @@ where
/// let informer = update_informer::new(registry::Crates, "crate_name", "0.1.0").http_client(YourOwnHttpClient);
/// let _ = informer.check_version();
/// ```
pub fn http_client<C: SendRequest>(self, http_client: C) -> UpdateInformer<R, N, V, C> {
pub fn http_client<C: HttpClient>(self, http_client: C) -> UpdateInformer<R, N, V, C> {
UpdateInformer {
_registry: self._registry,
name: self.name,
Expand All @@ -180,7 +180,7 @@ where
R: Registry,
N: AsRef<str>,
V: AsRef<str>,
H: SendRequest,
H: HttpClient,
{
/// Checks for a new version in the registry.
///
Expand Down Expand Up @@ -275,7 +275,7 @@ impl<V: AsRef<str>> FakeUpdateInformer<V> {
self
}

pub fn http_client<C: SendRequest>(self, _http_client: C) -> Self {
pub fn http_client<C: HttpClient>(self, _http_client: C) -> Self {
self
}
}
Expand Down
6 changes: 3 additions & 3 deletions src/registry/crates.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
http_client::{HttpClient, SendRequest},
http_client::{GenericHttpClient, HttpClient},
Package, Registry, Result,
};
use serde::Deserialize;
Expand Down Expand Up @@ -36,8 +36,8 @@ fn get_base_url() -> String {
impl Registry for Crates {
const NAME: &'static str = "crates";

fn get_latest_version<T: SendRequest>(
http_client: HttpClient<T>,
fn get_latest_version<T: HttpClient>(
http_client: GenericHttpClient<T>,
pkg: &Package,
) -> Result<Option<String>> {
let url = format!("{}/{}/versions", get_base_url(), pkg);
Expand Down
6 changes: 3 additions & 3 deletions src/registry/github.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
http_client::{HttpClient, SendRequest},
http_client::{GenericHttpClient, HttpClient},
Package, Registry, Result,
};
use serde::Deserialize;
Expand Down Expand Up @@ -31,8 +31,8 @@ fn get_base_url() -> String {
impl Registry for GitHub {
const NAME: &'static str = "github";

fn get_latest_version<T: SendRequest>(
http_client: HttpClient<T>,
fn get_latest_version<T: HttpClient>(
http_client: GenericHttpClient<T>,
pkg: &Package,
) -> Result<Option<String>> {
let url = format!("{}/{}/releases/latest", get_base_url(), pkg);
Expand Down
6 changes: 3 additions & 3 deletions src/registry/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
http_client::{HttpClient, SendRequest},
http_client::{GenericHttpClient, HttpClient},
Package, Result,
};

Expand Down Expand Up @@ -35,8 +35,8 @@ pub trait Registry {
/// * `http_client` - An HTTP client to send requests to the registry.
/// * `pkg` - A `Package` struct.
/// * `current_version` - The current version of the package.
fn get_latest_version<T: SendRequest>(
http_client: HttpClient<T>,
fn get_latest_version<T: HttpClient>(
http_client: GenericHttpClient<T>,
pkg: &Package,
) -> Result<Option<String>>;
}
6 changes: 3 additions & 3 deletions src/registry/npm.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
http_client::{HttpClient, SendRequest},
http_client::{GenericHttpClient, HttpClient},
Package, Registry, Result,
};
use serde::Deserialize;
Expand Down Expand Up @@ -28,8 +28,8 @@ fn get_base_url() -> String {
impl Registry for Npm {
const NAME: &'static str = "npm";

fn get_latest_version<T: SendRequest>(
http_client: HttpClient<T>,
fn get_latest_version<T: HttpClient>(
http_client: GenericHttpClient<T>,
pkg: &Package,
) -> Result<Option<String>> {
let url = format!("{}/{}/latest", get_base_url(), pkg);
Expand Down
6 changes: 3 additions & 3 deletions src/registry/pypi.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
http_client::{HttpClient, SendRequest},
http_client::{GenericHttpClient, HttpClient},
Package, Registry, Result,
};
use serde::Deserialize;
Expand Down Expand Up @@ -37,8 +37,8 @@ fn get_base_url() -> String {
impl Registry for PyPI {
const NAME: &'static str = "pypi";

fn get_latest_version<T: SendRequest>(
http_client: HttpClient<T>,
fn get_latest_version<T: HttpClient>(
http_client: GenericHttpClient<T>,
pkg: &Package,
) -> Result<Option<String>> {
let url = format!("{}/{}/json", get_base_url(), pkg);
Expand Down

0 comments on commit 5ca5b0b

Please sign in to comment.