Skip to content

Commit

Permalink
Merge branch 'release/v0.5.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
keltia committed Nov 8, 2021
2 parents 6a3b7b0 + acd2b2d commit 210fb30
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 117 deletions.
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ipify-rs"
version = "0.4.0"
version = "0.5.0"
edition = "2021"
authors = ["Ollivier Robert <[email protected]>"]
keywords = ["ipify", "api", "client"]
Expand All @@ -15,6 +15,5 @@ documentation = "https://docs.rs/ipify-rs"
[dependencies]
clap = "3.0.0-beta.5"
log = "0.4"
ureq = { version = "2", features = ["socks-proxy"] }
reqwest = { version = "0.11.6", features = ["blocking"] }
stderrlog = "0.5.1"
28 changes: 5 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,21 +32,6 @@ The four operations are specified as below:
- `OP::IPv4J` (json output)
- `Op::IPv6J` (json output)

## HTTP engine

This API can use either [ureq] or [reqwest] as HTTP client. You can select the engine with the `with()` method. The current version of `Ipify` only support the *blocking* client though.

```rs
use ipify_rs::{Engine, Ipify, Op};

let c = Ipify::new().with(Engine::Reqw).set(Op::IPv4);

println!("My IP is {}", c.call());
```

[ureq]: https://docs.rs/crate/ureq/
[reqwest]: https://docs.rs/crate/reqwest/

## Minimalistic API

If you only care about the default (plain text, IPv6 query) and don't want to reuse anything later, then `myip()` is what you want:
Expand All @@ -63,7 +48,7 @@ fn main() {

There is a CLI utility bundled with the API called `ipify-cli`.
```
ipify-cli 0.2.0
ipify-cli 0.4.0
Ollivier Robert <[email protected]>
Expand All @@ -84,7 +69,8 @@ There is a CLI utility bundled with the API called `ipify-cli`.
You can see both API & CLI versions:
```
$ ipify-cli -V
Running API ipify-rs/0.2.0 CLI ipify-cli/0.1.0
CLI ipify-cli/0.4.0 using API ipify-rs/0.5.0
```

## Example
Expand All @@ -96,14 +82,10 @@ The file `showall.rs` inside `examples` show almost all parameters for the API.
INFO - Start
INFO - Using default, minimal API
IP=aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh
INFO - Using defaults (ureq, ipv6)
INFO - Using defaults (ipv6)
IP=aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh
INFO - Using defaults, get json
IP={"ip":"aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh"}
INFO - Using ureq
IP4="A.B.C.D"
IP6="aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh"
INFO - Using reqwest
IP4="A.B.C.D"
IP6="aaaa:bbbb:cccc:dddd:eeee:ffff:gggg:hhhh"
```
Expand All @@ -115,7 +97,7 @@ to your `Cargo.toml`:

``` toml
[dependencies]
ipify-rs = "0.2.0"
ipify-rs = "0.5.0"
```
then you can use it in your own crates.

Expand Down
11 changes: 3 additions & 8 deletions examples/showall.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use ipify_rs::*;
use log::info;

fn doit(e: Engine) {
fn doit() {
let ip = Ipify::new();
ip.with(e);

println!("IP4={:?}", ip.set(Op::IPv4).call());
println!("IP6={:?}", ip.set(Op::IPv6).call());
Expand All @@ -20,15 +19,11 @@ fn main() {
info!("Using default, minimal API");
println!("IP={}", myip());

info!("Using defaults (ureq, ipv6)");
info!("Using defaults (ipv6)");
println!("IP={}", Ipify::new().call());

info!("Using defaults, get json");
println!("IP={}", Ipify::new().set(Op::IPv6J).call());

info!("Using ureq");
doit(Engine::Reqw);

info!("Using reqwest");
doit(Engine::Reqw);
doit();
}
14 changes: 3 additions & 11 deletions src/bin/ipify-cli/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use clap::{crate_authors, crate_name, crate_version, AppSettings, Parser};
use ipify_rs::{Engine, Ipify, Op};
use ipify_rs::{Ipify, Op};

/// Binary name
pub(crate) const NAME: &str = "ipify-cli";
/// Binary version, different from the API itself represented the crate.
pub(crate) const VERSION: &str = "0.3.0";
pub(crate) const VERSION: &str = "0.4.0";

/// Help message
#[derive(Debug, Parser)]
Expand All @@ -27,9 +27,6 @@ struct Opts {
/// Request JSON output
#[clap(short = 'J', long = "json")]
json: bool,
/// Request other engine
#[clap(short = 'E', long = "engine", default_value = "ureq")]
engine: String,
}

fn banner() -> String {
Expand Down Expand Up @@ -57,7 +54,6 @@ fn main() {

// Start with defaults
let mut op = Op::IPv6;
let mut e = Engine::Ureq;

if opts.ipv4 {
op = Op::IPv4;
Expand All @@ -74,12 +70,8 @@ fn main() {
};
}

if opts.engine == "reqw" {
e = Engine::Reqw;
}

let c = Ipify::new();
let r = c.set(op).with(e).call();
let r = c.set(op).call();
if v {
println!("My IP = {}", r);
} else {
Expand Down
105 changes: 32 additions & 73 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
//! ```
//! use ipify_rs::myip;
//!
//! fn main() {
//! println!("My IP is: {}", myip());
//! }
//! println!("My IP is: {}", myip());
//! ```
//!
//! The full API is described below.
Expand All @@ -30,24 +28,13 @@ const ENDPOINT6J: &str = "https://api64.ipify.org?format=json";
/// ```
/// use ipify_rs::myip;
///
/// fn main() {
/// println!("{}", myip())
/// }
/// println!("{}", myip())
/// ```
///
pub fn myip() -> String {
Ipify::new().call()
}

/// Describe the available HTTP engines
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Engine {
// ureq
Ureq,
// reqwest
Reqw,
}

/// The current set of operations
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Op {
Expand All @@ -64,68 +51,49 @@ pub enum Op {
/// The main API struct
#[derive(Clone, Copy, Debug)]
pub struct Ipify<'a> {
/// HTTP Engine
pub e: Engine,
/// Current type of operation
pub t: Op,
/// Endpoint, different for every operation
pub endp: &'a str,
}

/// Impl. default values.
impl<'a> Default for Ipify<'a> {
fn default() -> Self {
Self::new()
}
}

/// API Implementation
impl<'a> Ipify<'a> {
/// Create a new API instance client with the defaults
///
/// Examples:
/// Example:
/// ```
/// use ipify_rs::*;
///
/// fn main() {
/// let mut a = Ipify::new();
/// let mut a = Ipify::new();
///
/// println!("{}", a.call());
/// }
/// println!("{}", a.call());
/// ```
///
pub fn new() -> Self {
Ipify {
e: Engine::Ureq,
t: Op::IPv6,
endp: ENDPOINT6,
}
}

/// Use the specified HTTP engine
///
/// Examples:
/// ```
/// use ipify_rs::{Ipify, Engine};
///
/// fn main() {
/// let mut a = Ipify::new();
/// a.with(Engine::Reqw);
///
/// println!("{}", a.call());
/// }
/// ```
///
pub fn with(mut self, e: Engine) -> Self {
self.e = e;
self
}

/// Specify the subsequent operation to perform on `call()`
///
/// Examples:
/// ```
/// use ipify_rs::{Ipify, Op};
///
/// fn main() {
/// let mut a = Ipify::new();
/// a.set(Op::IPv6J);
/// let mut a = Ipify::new();
/// a.set(Op::IPv6J);
///
/// println!("{}", a.call());
/// }
/// println!("{}", a.call());
/// ```
///
pub fn set(mut self, op: Op) -> Self {
Expand All @@ -140,25 +108,22 @@ impl<'a> Ipify<'a> {
}

/// Actually perform the API call
///
/// Example:
/// ```
/// use ipify_rs::Ipify;
///
/// let r = Ipify::new().call();
///
/// println!("my ip = {}", r);
/// ```
///
pub fn call(self) -> String {
match self.e {
Engine::Ureq => {
let pe = env!("http_proxy");
let p = ureq::Proxy::new(pe).unwrap();
let c = ureq::AgentBuilder::new()
.user_agent("ipify-cli/1.0.0")
.proxy(p)
.build();
return c.get(&self.endp).call().unwrap().into_string().unwrap();
}
Engine::Reqw => {
let c = reqwest::blocking::ClientBuilder::new()
.user_agent("ipify-cli/1.0.0")
.build()
.unwrap();
return c.get(self.endp).send().unwrap().text().unwrap();
}
}
let c = reqwest::blocking::ClientBuilder::new()
.user_agent("ipify-cli/1.0.0")
.build()
.unwrap();
c.get(self.endp).send().unwrap().text().unwrap()
}
}

Expand Down Expand Up @@ -188,23 +153,17 @@ mod tests {
let c = Ipify::new();

assert_eq!(Op::IPv6, c.t);
let c = c.with(Engine::Reqw);
assert_eq!(Engine::Reqw, c.e);
let c = c.with(Engine::Ureq);
assert_eq!(Engine::Ureq, c.e);
}

#[test]
fn test_with_set() {
let c = Ipify::new();

assert_eq!(Op::IPv6, c.t);
let c = c.with(Engine::Reqw).set(Op::IPv4);
assert_eq!(Engine::Reqw, c.e);
let c = c.set(Op::IPv4);
assert_eq!(Op::IPv4, c.t);

let c = c.set(Op::IPv4J).with(Engine::Ureq);
assert_eq!(Engine::Ureq, c.e);
let c = c.set(Op::IPv4J);
assert_eq!(Op::IPv4J, c.t);
}
}

0 comments on commit 210fb30

Please sign in to comment.