1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
//! The client (based on [ip-api.com API](https://ip-api.com/docs/api:json))
//! allows you to get information about the IP address
//!
//! # Example
//!
//! ```rust
//! use ip_api_client as Client;
//! use ip_api_client::{IpApiLanguage, IpData};
//!
//! // 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(),
//! );
//!
//! // If you want to request more than one ip, you can use `make_batch_request`
//! let ip_batch_data: Vec<IpData> = Client::generate_empty_config()
//!     .include_isp()
//! // `make_batch_request` takes "IPv4"/"IPv6"
//! .make_batch_request(vec!["1.1.1.1", "8.8.8.8"]).unwrap();
//!
//! println!(
//!     "1.1.1.1 belongs to `{}` and 8.8.8.8 belongs to `{}`",
//!     ip_batch_data.get(0).unwrap().isp.as_ref().unwrap(),
//!     ip_batch_data.get(1).unwrap().isp.as_ref().unwrap(),
//! );
//! ```

#![deny(missing_docs)]

use hyper::body::HttpBody;
use hyper::{Body, Client, Method, Request, Response};
use serde::Deserialize;
use serde_json::json;

#[cfg(test)]
mod tests {
    use crate::{generate_empty_config, IpData};

    #[test]
    fn make_request() {
        assert_eq!(
            generate_empty_config()
                .include_query()
                .make_request("1.1.1.1").unwrap()
                .query.unwrap(),
            String::from("1.1.1.1")
        );
    }

    #[test]
    fn make_batch_request() {
        let ips: Vec<IpData> = generate_empty_config()
            .include_query()
            .make_batch_request(vec!["1.1.1.1", "8.8.8.8"]).unwrap();

        assert_eq!(ips.get(0).unwrap().query, Some(String::from("1.1.1.1")));
        assert_eq!(ips.get(1).unwrap().query, Some(String::from("8.8.8.8")))
    }
}

/// Represents all the ways that a request can fail
#[derive(Debug)]
pub enum IpApiError {
    /// Incorrect IP address or non-existent domain
    ///
    /// # Example
    ///
    /// 1.1.1.one **OR** test.google.com
    InvalidQuery,

    /// IPs in your network
    ///
    /// # Example
    ///
    /// 192.168.1.1
    PrivateRange,

    /// [ip-api.com API](https://ip-api.com/docs/api:json) is limited to 45 requests per minute
    /// from one IP address
    ///
    /// Contains the remaining time before a possible re-request in seconds
    RateLimit(u8),

    /// Reserved Range
    ///
    /// # Example
    ///
    /// 127.0.0.1 **OR** localhost
    ReservedRange,

    /// Unexpected Error
    ///
    /// May contain additional information
    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>,
}

/// The data that will be received after the making a request
///
/// # Example response
///
/// ```rust
/// # use ip_api_client::IpData;
/// #
/// IpData {
///     continent: Some("Oceania".to_string()),
///     continent_code: Some("OC".to_string()),
///     country: Some("Australia".to_string()),
///     country_code: Some("AU".to_string()),
///     region: Some("QLD".to_string()),
///     region_name: Some("Queensland".to_string()),
///     city: Some("South Brisbane".to_string()),
///     district: Some("".to_string()),
///     zip: Some("4101".to_string()),
///     lat: Some(-27.4766),
///     lon: Some(153.0166),
///     timezone: Some("Australia/Brisbane".to_string()),
///     offset: Some(36000),
///     currency: Some("AUD".to_string()),
///     isp: Some("Cloudflare, Inc".to_string()),
///     org: Some("APNIC and Cloudflare DNS Resolver project".to_string()),
///     as_field: Some("AS13335 Cloudflare, Inc.".to_string()),
///     asname: Some("CLOUDFLARENET".to_string()),
///     reverse: Some("one.one.one.one".to_string()),
///     mobile: Some(false),
///     proxy: Some(false),
///     hosting: Some(true),
///     query: Some("1.1.1.1".to_string()),
/// };
/// ```
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct IpData {
    /// Continent name
    pub continent: Option<String>,

    /// Two-letter continent code
    pub continent_code: Option<String>,

    /// Country name
    pub country: Option<String>,

    /// Two-letter country code
    /// [ISO 3166-1 alpha-2](https://en.wikipedia.org/wiki/ISO_3166-1_alpha-2)
    pub country_code: Option<String>,

    /// Region/state short code (FIPS or ISO)
    pub region: Option<String>,

    /// Region/state
    pub region_name: Option<String>,

    /// City
    pub city: Option<String>,

    /// District (subdivision of city)
    pub district: Option<String>,

    /// Zip code
    pub zip: Option<String>,

    /// Latitude
    pub lat: Option<f32>,

    /// Longitude
    pub lon: Option<f32>,

    /// Timezone (tz)
    pub timezone: Option<String>,

    /// Timezone UTC DST offset in seconds
    pub offset: Option<i32>,

    /// National currency
    pub currency: Option<String>,

    /// ISP name
    pub isp: Option<String>,

    /// Organization name
    pub org: Option<String>,

    /// AS number and organization, separated by space (RIR).
    /// Empty for IP blocks not being announced in BGP tables.
    ///
    /// # Notice
    ///
    /// We use `as_field` instead of `as`
    /// (As stated in the [ip-api.com API documentation](https://ip-api.com/docs/api:json#as))
    /// since it's a
    /// [strict keyword](https://doc.rust-lang.org/reference/keywords.html#strict-keywords) in rust,
    /// such as `pub`, `impl` or `struct`.
    #[serde(rename = "as")]
    pub as_field: Option<String>,

    /// AS name (RIR). Empty for IP blocks not being announced in BGP tables.
    pub asname: Option<String>,

    /// Reverse DNS of the IP (can delay response)
    pub reverse: Option<String>,

    /// Mobile (cellular) connection
    pub mobile: Option<bool>,

    /// Proxy, VPN or Tor exit address
    pub proxy: Option<bool>,

    /// Hosting, colocated or data center
    pub hosting: Option<bool>,

    /// IP/Domain used for the query
    pub query: Option<String>,
}

/// Configuration structure allows you to customize the requested fields in the request
/// to save traffic
pub struct IpApiConfig {
    numeric_field: u32,
    is_continent_included: bool,
    is_continent_code_included: bool,
    is_country_included: bool,
    is_country_code_included: bool,
    is_region_included: bool,
    is_region_name_included: bool,
    is_city_included: bool,
    is_district_included: bool,
    is_zip_included: bool,
    is_lat_included: bool,
    is_lon_included: bool,
    is_timezone_included: bool,
    is_offset_included: bool,
    is_currency_included: bool,
    is_isp_included: bool,
    is_org_included: bool,
    is_as_field_included: bool,
    is_asname_included: bool,
    is_reverse_included: bool,
    is_mobile_included: bool,
    is_proxy_included: bool,
    is_hosting_included: bool,
    is_query_included: bool,
    language: IpApiLanguage,
}

impl IpApiConfig {
    fn build_uri(
        resource: &str,
        target: Option<&str>,
        fields: u32,
        language: IpApiLanguage,
    ) -> String {
        format!("http://ip-api.com/{}/{}?fields={}{}",
                resource,
                target.unwrap_or(""),
                fields,
                match 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",
                    _ => "",
                }
        )
    }

    fn check_response(response: &Response<Body>) -> Result<(), IpApiError> {
        if response.status() == 429 {
            return Err(IpApiError::RateLimit(
                response
                    .headers().get("X-Ttl").unwrap()
                    .to_str().unwrap()
                    .parse().unwrap()
            ));
        }

        Ok(())
    }

    fn check_error_message(message: Option<String>) -> Result<(), IpApiError> {
        if let Some(message) = message {
            return match message.as_str() {
                "invalid query" => Err(IpApiError::InvalidQuery),
                "private range" => Err(IpApiError::PrivateRange),
                "reserved range" => Err(IpApiError::ReservedRange),
                message => Err(IpApiError::UnexpectedError(Some(message.into()))),
            };
        }

        Ok(())
    }

    async fn parse_response_body(response: &mut Response<Body>) -> String {
        let body = response.body_mut().data().await;
        let body = body.unwrap().unwrap().to_vec();
        let body = std::str::from_utf8(&body).unwrap();

        body.into()
    }

    /// Making a request to [ip-api.com API](https://ip-api.com/docs/api:json)
    ///
    /// `target` can be "ip"/"domain"/"empty string (if you want to request your ip)"
    #[tokio::main]
    pub async fn make_request(self, target: &str) -> Result<IpData, IpApiError> {
        let uri = Self::build_uri(
            "json",
            Some(target),
            self.numeric_field,
            self.language,
        );

        let client = Client::new();
        let response = &mut client.get(uri.parse().unwrap()).await.unwrap();

        Self::check_response(response)?;

        let body = Self::parse_response_body(response).await;
        let ip_data: IpApiMessage = serde_json::from_str(body.as_str()).unwrap();

        Self::check_error_message(ip_data.message)?;

        let ip_data: IpData = serde_json::from_str(body.as_str()).unwrap();

        Ok(ip_data)
    }

    /// Making a batch request to [ip-api.com API](https://ip-api.com/docs/api:batch)
    ///
    /// `target` can be "IPv4"/"IPv6"
    #[tokio::main]
    pub async fn make_batch_request(self, targets: Vec<&str>) -> Result<Vec<IpData>, IpApiError> {
        let uri = Self::build_uri(
            "batch",
            None,
            self.numeric_field,
            self.language,
        );

        let request = Request::builder()
            .method(Method::POST)
            .uri(uri)
            .header("content-type", "application/json")
            .body(Body::from(json!(targets).to_string())).unwrap();

        let client = Client::new();
        let response = &mut client.request(request).await.unwrap();

        Self::check_response(response)?;

        let body = Self::parse_response_body(response).await;
        let ip_batch_data: Vec<IpApiMessage> = serde_json::from_str(body.as_str()).unwrap();

        for ip_data in ip_batch_data {
            Self::check_error_message(ip_data.message)?;
        }

        let ip_batch_data: Vec<IpData> = serde_json::from_str(body.as_str()).unwrap();

        Ok(ip_batch_data)
    }

    /// Include [`continent`](struct.IpData.html#structfield.continent) in request
    pub fn include_continent(mut self) -> Self {
        if !self.is_continent_included {
            self.is_continent_included = true;
            self.numeric_field += 1048576;
        }

        self
    }

    /// Include [`continent_code`](struct.IpData.html#structfield.continent_code) in request
    pub fn include_continent_code(mut self) -> Self {
        if !self.is_continent_code_included {
            self.is_continent_code_included = true;
            self.numeric_field += 2097152;
        }

        self
    }

    /// Include [`country`](struct.IpData.html#structfield.country) in request
    pub fn include_country(mut self) -> Self {
        if !self.is_country_included {
            self.is_country_included = true;
            self.numeric_field += 1;
        }

        self
    }

    /// Include [`country_code`](struct.IpData.html#structfield.country_code) in request
    pub fn include_country_code(mut self) -> Self {
        if !self.is_country_code_included {
            self.is_country_code_included = true;
            self.numeric_field += 2;
        }

        self
    }

    /// Include [`region`](struct.IpData.html#structfield.region) in request
    pub fn include_region(mut self) -> Self {
        if !self.is_region_included {
            self.is_region_included = true;
            self.numeric_field += 4;
        }

        self
    }

    /// Include [`region_name`](struct.IpData.html#structfield.region_name) in request
    pub fn include_region_name(mut self) -> Self {
        if !self.is_region_name_included {
            self.is_region_name_included = true;
            self.numeric_field += 8;
        }

        self
    }

    /// Include [`city`](struct.IpData.html#structfield.city) in request
    pub fn include_city(mut self) -> Self {
        if !self.is_city_included {
            self.is_city_included = true;
            self.numeric_field += 16;
        }

        self
    }

    /// Include [`district`](struct.IpData.html#structfield.district) in request
    pub fn include_district(mut self) -> Self {
        if !self.is_district_included {
            self.is_district_included = true;
            self.numeric_field += 524288;
        }

        self
    }

    /// Include [`zip`](struct.IpData.html#structfield.zip) in request
    pub fn include_zip(mut self) -> Self {
        if !self.is_zip_included {
            self.is_zip_included = true;
            self.numeric_field += 32;
        }

        self
    }

    /// Include [`lat`](struct.IpData.html#structfield.lat) in request
    pub fn include_lat(mut self) -> Self {
        if !self.is_lat_included {
            self.is_lat_included = true;
            self.numeric_field += 64;
        }

        self
    }

    /// Include [`lon`](struct.IpData.html#structfield.lon) in request
    pub fn include_lon(mut self) -> Self {
        if !self.is_lon_included {
            self.is_lon_included = true;
            self.numeric_field += 128;
        }

        self
    }

    /// Include [`timezone`](struct.IpData.html#structfield.timezone) in request
    pub fn include_timezone(mut self) -> Self {
        if !self.is_timezone_included {
            self.is_timezone_included = true;
            self.numeric_field += 256;
        }

        self
    }

    /// Include [`offset`](struct.IpData.html#structfield.offset) in request
    pub fn include_offset(mut self) -> Self {
        if !self.is_offset_included {
            self.is_offset_included = true;
            self.numeric_field += 33554432;
        }

        self
    }

    /// Include [`currency`](struct.IpData.html#structfield.currency) in request
    pub fn include_currency(mut self) -> Self {
        if !self.is_currency_included {
            self.is_currency_included = true;
            self.numeric_field += 8388608;
        }

        self
    }

    /// Include [`isp`](struct.IpData.html#structfield.isp) in request
    pub fn include_isp(mut self) -> Self {
        if !self.is_isp_included {
            self.is_isp_included = true;
            self.numeric_field += 512;
        }

        self
    }

    /// Include [`org`](struct.IpData.html#structfield.org) in request
    pub fn include_org(mut self) -> Self {
        if !self.is_org_included {
            self.is_org_included = true;
            self.numeric_field += 1024;
        }

        self
    }

    /// Include [`as_field`](struct.IpData.html#structfield.as_field) in request
    pub fn include_as_field(mut self) -> Self {
        if !self.is_as_field_included {
            self.is_as_field_included = true;
            self.numeric_field += 2048;
        }

        self
    }

    /// Include [`asname`](struct.IpData.html#structfield.asname) in request
    pub fn include_asname(mut self) -> Self {
        if !self.is_asname_included {
            self.is_asname_included = true;
            self.numeric_field += 4194304;
        }

        self
    }

    /// Include [`reverse`](struct.IpData.html#structfield.reverse) in request
    pub fn include_reverse(mut self) -> Self {
        if !self.is_reverse_included {
            self.is_reverse_included = true;
            self.numeric_field += 4096;
        }

        self
    }

    /// Include [`mobile`](struct.IpData.html#structfield.mobile) in request
    pub fn include_mobile(mut self) -> Self {
        if !self.is_mobile_included {
            self.is_mobile_included = true;
            self.numeric_field += 65536;
        }

        self
    }

    /// Include [`proxy`](struct.IpData.html#structfield.proxy) in request
    pub fn include_proxy(mut self) -> Self {
        if !self.is_proxy_included {
            self.is_proxy_included = true;
            self.numeric_field += 131072;
        }

        self
    }

    /// Include [`hosting`](struct.IpData.html#structfield.hosting) in request
    pub fn include_hosting(mut self) -> Self {
        if !self.is_hosting_included {
            self.is_hosting_included = true;
            self.numeric_field += 16777216;
        }

        self
    }

    /// Include [`query`](struct.IpData.html#structfield.query) in request
    pub fn include_query(mut self) -> Self {
        if !self.is_query_included {
            self.is_query_included = true;
            self.numeric_field += 8192;
        }

        self
    }

    /// Exclude [`continent`](struct.IpData.html#structfield.continent) from request
    pub fn exclude_continent(mut self) -> Self {
        if self.is_continent_included {
            self.is_continent_included = false;
            self.numeric_field -= 1048576;
        }

        self
    }

    /// Exclude [`continent_code`](struct.IpData.html#structfield.continent_code) from request
    pub fn exclude_continent_code(mut self) -> Self {
        if self.is_continent_code_included {
            self.is_continent_code_included = false;
            self.numeric_field -= 2097152;
        }

        self
    }

    /// Exclude [`country`](struct.IpData.html#structfield.country) from request
    pub fn exclude_country(mut self) -> Self {
        if self.is_country_included {
            self.is_country_included = false;
            self.numeric_field -= 1;
        }

        self
    }

    /// Exclude [`country_code`](struct.IpData.html#structfield.country_code) from request
    pub fn exclude_country_code(mut self) -> Self {
        if self.is_country_code_included {
            self.is_country_code_included = false;
            self.numeric_field -= 2;
        }

        self
    }

    /// Exclude [`region`](struct.IpData.html#structfield.region) from request
    pub fn exclude_region(mut self) -> Self {
        if self.is_region_included {
            self.is_region_included = false;
            self.numeric_field -= 4;
        }

        self
    }

    /// Exclude [`region_name`](struct.IpData.html#structfield.region_name) from request
    pub fn exclude_region_name(mut self) -> Self {
        if self.is_region_name_included {
            self.is_region_name_included = false;
            self.numeric_field -= 8;
        }

        self
    }

    /// Exclude [`city`](struct.IpData.html#structfield.city) from request
    pub fn exclude_city(mut self) -> Self {
        if self.is_city_included {
            self.is_city_included = false;
            self.numeric_field -= 16;
        }

        self
    }

    /// Exclude [`district`](struct.IpData.html#structfield.district) from request
    pub fn exclude_district(mut self) -> Self {
        if self.is_district_included {
            self.is_district_included = false;
            self.numeric_field -= 524288;
        }

        self
    }

    /// Exclude [`zip`](struct.IpData.html#structfield.zip) from request
    pub fn exclude_zip(mut self) -> Self {
        if self.is_zip_included {
            self.is_zip_included = false;
            self.numeric_field -= 32;
        }

        self
    }

    /// Exclude [`lat`](struct.IpData.html#structfield.lat) from request
    pub fn exclude_lat(mut self) -> Self {
        if self.is_lat_included {
            self.is_lat_included = false;
            self.numeric_field -= 64;
        }

        self
    }

    /// Exclude [`lon`](struct.IpData.html#structfield.lon) from request
    pub fn exclude_lon(mut self) -> Self {
        if self.is_lon_included {
            self.is_lon_included = false;
            self.numeric_field -= 128;
        }

        self
    }

    /// Exclude [`timezone`](struct.IpData.html#structfield.timezone) from request
    pub fn exclude_timezone(mut self) -> Self {
        if self.is_timezone_included {
            self.is_timezone_included = false;
            self.numeric_field -= 256;
        }

        self
    }

    /// Exclude [`offset`](struct.IpData.html#structfield.offset) from request
    pub fn exclude_offset(mut self) -> Self {
        if self.is_offset_included {
            self.is_offset_included = false;
            self.numeric_field -= 33554432;
        }

        self
    }

    /// Exclude [`currency`](struct.IpData.html#structfield.currency) from request
    pub fn exclude_currency(mut self) -> Self {
        if self.is_currency_included {
            self.is_currency_included = false;
            self.numeric_field -= 8388608;
        }

        self
    }

    /// Exclude [`isp`](struct.IpData.html#structfield.isp) from request
    pub fn exclude_isp(mut self) -> Self {
        if self.is_isp_included {
            self.is_isp_included = false;
            self.numeric_field -= 512;
        }

        self
    }

    /// Exclude [`org`](struct.IpData.html#structfield.org) from request
    pub fn exclude_org(mut self) -> Self {
        if self.is_org_included {
            self.is_org_included = false;
            self.numeric_field -= 1024;
        }

        self
    }

    /// Exclude [`as_field`](struct.IpData.html#structfield.as_field) from request
    pub fn exclude_as_field(mut self) -> Self {
        if self.is_as_field_included {
            self.is_as_field_included = false;
            self.numeric_field -= 2048;
        }

        self
    }

    /// Exclude [`asname`](struct.IpData.html#structfield.asname) from request
    pub fn exclude_asname(mut self) -> Self {
        if self.is_asname_included {
            self.is_asname_included = false;
            self.numeric_field -= 4194304;
        }

        self
    }

    /// Exclude [`reverse`](struct.IpData.html#structfield.reverse) from request
    pub fn exclude_reverse(mut self) -> Self {
        if self.is_reverse_included {
            self.is_reverse_included = false;
            self.numeric_field -= 4096;
        }

        self
    }

    /// Exclude [`mobile`](struct.IpData.html#structfield.mobile) from request
    pub fn exclude_mobile(mut self) -> Self {
        if self.is_mobile_included {
            self.is_mobile_included = false;
            self.numeric_field -= 65536;
        }

        self
    }

    /// Exclude [`proxy`](struct.IpData.html#structfield.proxy) from request
    pub fn exclude_proxy(mut self) -> Self {
        if self.is_proxy_included {
            self.is_proxy_included = false;
            self.numeric_field -= 131072;
        }

        self
    }

    /// Exclude [`hosting`](struct.IpData.html#structfield.hosting) from request
    pub fn exclude_hosting(mut self) -> Self {
        if self.is_hosting_included {
            self.is_hosting_included = false;
            self.numeric_field -= 16777216;
        }

        self
    }

    /// Exclude [`query`](struct.IpData.html#structfield.query) from request
    pub fn exclude_query(mut self) -> Self {
        if self.is_query_included {
            self.is_query_included = false;
            self.numeric_field -= 8192;
        }

        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
pub fn generate_empty_config() -> IpApiConfig {
    IpApiConfig {
        numeric_field: 32768,
        is_continent_included: false,
        is_continent_code_included: false,
        is_country_included: false,
        is_country_code_included: false,
        is_region_included: false,
        is_region_name_included: false,
        is_city_included: false,
        is_district_included: false,
        is_zip_included: false,
        is_lat_included: false,
        is_lon_included: false,
        is_timezone_included: false,
        is_offset_included: false,
        is_currency_included: false,
        is_isp_included: false,
        is_org_included: false,
        is_as_field_included: false,
        is_asname_included: false,
        is_reverse_included: false,
        is_mobile_included: false,
        is_proxy_included: false,
        is_hosting_included: false,
        is_query_included: false,
        language: IpApiLanguage::En,
    }
}

/// Generate minimum config that includes only important fields
pub fn generate_minimum_config() -> IpApiConfig {
    IpApiConfig {
        numeric_field: 41976594,
        is_continent_included: false,
        is_continent_code_included: false,
        is_country_included: false,
        is_country_code_included: true,
        is_region_included: false,
        is_region_name_included: false,
        is_city_included: true,
        is_district_included: false,
        is_zip_included: false,
        is_lat_included: false,
        is_lon_included: false,
        is_timezone_included: true,
        is_offset_included: true,
        is_currency_included: true,
        is_isp_included: true,
        is_org_included: false,
        is_as_field_included: false,
        is_asname_included: false,
        is_reverse_included: false,
        is_mobile_included: false,
        is_proxy_included: false,
        is_hosting_included: false,
        is_query_included: false,
        language: IpApiLanguage::En,
    }
}

/// Generate maximum config that includes all fields
pub fn generate_maximum_config() -> IpApiConfig {
    IpApiConfig {
        numeric_field: 66830335,
        is_continent_included: true,
        is_continent_code_included: true,
        is_country_included: true,
        is_country_code_included: true,
        is_region_included: true,
        is_region_name_included: true,
        is_city_included: true,
        is_district_included: true,
        is_zip_included: true,
        is_lat_included: true,
        is_lon_included: true,
        is_timezone_included: true,
        is_offset_included: true,
        is_currency_included: true,
        is_isp_included: true,
        is_org_included: true,
        is_as_field_included: true,
        is_asname_included: true,
        is_reverse_included: true,
        is_mobile_included: true,
        is_proxy_included: true,
        is_hosting_included: true,
        is_query_included: true,
        language: IpApiLanguage::En,
    }
}