Skip to content

Commit

Permalink
feat(lib): add language selection
Browse files Browse the repository at this point in the history
- update lib.rs
- edit README
  • Loading branch information
Mixerou committed Apr 6, 2022
1 parent d484497 commit 869ad81
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "ip-api-client"
version = "0.2.0" # don't forget edit version in README.md
version = "0.3.0" # don't forget edit version in README.md
authors = ["Ivan <[email protected]>"]
edition = "2021"
description = "The client (based on ip-api.com api) allows you to get information about the IP address"
Expand Down
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,31 @@ Add to project

```toml
[dependencies]
ip-api-client = "0.2.0"
ip-api-client = "0.3.0"
```

Write some Rust

```rust
use ip_api_client as Client;
use ip_api_client::IpData;
use ip_api_client::{IpApiLanguage, IpData};

fn main() {
// You can
// `generate_empty_config` (to create your own config from scratch)
// `generate_minimum_config` (that includes only important fields)
// `generate_maximum_config` (that includes all fields)
let ip_data: IpData = Client::generate_empty_config()
// or `exclude_country` if this field is already included in the generated config
.include_country()
// or `exclude_currency` if this field is already included in the generated config
.include_currency()
// `make_request` takes "ip"/"domain"/"empty string (if you want to request your ip)"
.make_request("1.1.1.1").unwrap();

println!("{}'s national currency is {}", ip_data.country.unwrap(), ip_data.currency.unwrap());
// You can
// `generate_empty_config` (to create your own config from scratch)
// `generate_minimum_config` (that includes only important fields)
// `generate_maximum_config` (that includes all fields)
let ip_data: IpData = Client::generate_empty_config()
// or `exclude_country` if this field is already included in the generated config
.include_country()
// or `exclude_currency` if this field is already included in the generated config
.include_currency()
// available languages: de/en (default)/es/fr/ja/pt-Br/ru/zh-CN
.set_language(IpApiLanguage::De)
// `make_request` takes "ip"/"domain"/"empty string (if you want to request your ip)"
.make_request("1.1.1.1").unwrap();

println!("{}'s national currency is {}", ip_data.country.unwrap(), ip_data.currency.unwrap());
}
```

Expand All @@ -44,6 +46,7 @@ fn main() {
# Development Progress

- [x] Request IP address information with a configuration structure that allows you to customize the requested fields in the request to save traffic.
- [x] Get information about Ip in different languages
- [ ] Block all requests until the end of the limit if the last request was rate-limited.
- [ ] Ability to cache all responses with automatic removal of old ip-data when the maximum cache size is reached.

Expand Down
59 changes: 57 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//!
//! ```rust
//! use ip_api_client as Client;
//! use ip_api_client::IpData;
//! use ip_api_client::{IpApiLanguage, IpData};
//!
//! // You can
//! // `generate_empty_config` (to create your own config from scratch)
Expand All @@ -15,6 +15,8 @@
//! .include_country()
//! // or `exclude_currency` if this field is already included in the generated config
//! .include_currency()
//! // available languages: de/en (default)/es/fr/ja/pt-Br/ru/zh-CN
//! .set_language(IpApiLanguage::De)
//! // `make_request` takes "ip"/"domain"/"empty string (if you want to request your ip)"
//! .make_request("1.1.1.1").unwrap();
//!
Expand Down Expand Up @@ -73,6 +75,33 @@ pub enum IpApiError {
UnexpectedError(Option<String>),
}

/// Represents all available languages for [`IpData`]
pub enum IpApiLanguage {
/// Deutsch (German)
De,

/// English (default)
En,

/// Español (Spanish)
Es,

/// Français (French)
Fr,

/// 日本語 (Japanese)
Ja,

/// Português - Brasil (Portuguese - Brasil)
PtBr,

/// Русский (Russian)
Ru,

/// 中国 (Chinese)
ZhCn,
}

#[derive(Deserialize)]
struct IpApiMessage {
message: Option<String>,
Expand Down Expand Up @@ -218,6 +247,7 @@ pub struct IpApiConfig {
is_proxy_included: bool,
is_hosting_included: bool,
is_query_included: bool,
language: IpApiLanguage,
}

impl IpApiConfig {
Expand All @@ -228,7 +258,22 @@ impl IpApiConfig {
pub async fn make_request(self, target: &str) -> Result<IpData, IpApiError> {
let client = Client::new();

let mut response = client.get(format!("http://ip-api.com/json/{}?fields={}", target, self.numeric_field).parse().unwrap()).await.unwrap();
let uri = format!("http://ip-api.com/json/{}?fields={}{}",
target,
self.numeric_field,
match self.language {
IpApiLanguage::De => "&lang=de",
IpApiLanguage::Es => "&lang=es",
IpApiLanguage::Fr => "&lang=fr",
IpApiLanguage::Ja => "&lang=ja",
IpApiLanguage::PtBr => "&lang=pt-BR",
IpApiLanguage::Ru => "&lang=ru",
IpApiLanguage::ZhCn => "&lang=zh-CN",
_ => "",
}
);

let mut response = client.get(uri.parse().unwrap()).await.unwrap();

if response.status() == 429 {
return Err(IpApiError::RateLimit(response.headers().get("X-Ttl").unwrap().to_str().unwrap().parse().unwrap()));
Expand Down Expand Up @@ -716,6 +761,13 @@ impl IpApiConfig {

self
}

/// Set custom language for [`IpData`]
pub fn set_language(mut self, language: IpApiLanguage) -> Self {
self.language = language;

self
}
}

/// Create an empty config to create your own from scratch
Expand Down Expand Up @@ -745,6 +797,7 @@ pub fn generate_empty_config() -> IpApiConfig {
is_proxy_included: false,
is_hosting_included: false,
is_query_included: false,
language: IpApiLanguage::En,
}
}

Expand Down Expand Up @@ -775,6 +828,7 @@ pub fn generate_minimum_config() -> IpApiConfig {
is_proxy_included: false,
is_hosting_included: false,
is_query_included: false,
language: IpApiLanguage::En,
}
}

Expand Down Expand Up @@ -805,5 +859,6 @@ pub fn generate_maximum_config() -> IpApiConfig {
is_proxy_included: true,
is_hosting_included: true,
is_query_included: true,
language: IpApiLanguage::En,
}
}

0 comments on commit 869ad81

Please sign in to comment.