google_maps/client/with_rate.rs
1use crate::request_rate::api::Api;
2use std::time::Duration;
3
4// =============================================================================
5
6impl crate::client::Client {
7 // -------------------------------------------------------------------------
8 //
9 /// Sets the rate limit for the specified API.
10 ///
11 /// ## Arguments
12 ///
13 /// * `api` ‧ Which Google Maps API are you setting the rate limit for? For
14 /// example, `Api::Directions`, `Api::DistanceMatrix`, `Api::Elevation`,
15 /// `Api::Geocoding`, `Api::TimeZone`, and so on. The `Api::All` rate
16 /// limit is applied to all Google Maps API requests _in addition_ to the
17 /// per-API rate limits.
18 ///
19 /// * `requests` ‧ The number of requests the client library is attempting
20 /// to target. For example, _2 requests_ per 1 hour.
21 ///
22 /// * `duration` ‧ The duration for the targeted request rate. For example,
23 /// 1 request _per 1 minute_. This can be defined using the
24 /// `std::time::Duration` methods.
25 ///
26 /// ## Examples:
27 ///
28 /// The following examples show how one might try to limit the request
29 /// rate to achieve maximum throughput while minimizing charges by Google.
30 ///
31 /// **The following rates are subject to change by Google. Please review
32 /// the current [Google Maps Platform billing
33 /// rates](https://developers.google.com/maps/billing/gmp-billing).**
34 ///
35 /// **This Google client library's rate limiting is not persistent. If your
36 /// program is often restarted, it is easily possible to exceed Google's
37 /// monthly free credit. These are approximations and examples.**
38 ///
39 /// **To accurately minimize billing charges by Google, please use the
40 /// [Google Cloud Platform Console](https://console.cloud.google.com/)
41 /// _IAM & admin_ to set quotas for each API on the server's side.**
42 ///
43 /// You are responsible for all charges. Use with care.
44 ///
45 /// ```rust
46 /// // Assumptions:
47 /// // - $200.00 USD monthly free credit from Google. Thanks, guys!
48 /// // - 2,629,746 seconds in a month.
49 /// const GOOGLE_CREDIT: f64 = 200.0;
50 /// const SECONDS_PER_MONTH: u64 = 2_629_746;
51 /// ```
52 ///
53 /// * [Directions](https://developers.google.com/maps/billing/gmp-billing#directions)
54 /// API. You are billed for this SKU when your request does not use live
55 /// traffic information, arrival or departure times, < 10 waypoints, and
56 /// no waypoint optimization, $0.005 USD per request.
57 /// ```rust
58 /// with_rate(Api::Directions, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
59 /// ```
60 ///
61 /// * [Directions Advanced](https://developers.google.com/maps/billing/gmp-billing#directions-advanced)
62 /// API. You are billed for this SKU when your request requires live
63 /// traffic information, > 10 waypoints, and/or waypoint optimization, $0.01
64 /// per request.
65 /// ```rust
66 /// with_rate(Api::Directions, (GOOGLE_CREDIT / 0.01) as u16, Duration::from_secs(SECONDS_PER_MONTH))
67 /// ```
68 ///
69 /// * [Distance Matrix](https://developers.google.com/maps/billing/gmp-billing#distance-matrix)
70 /// API. You are billed for this SKU when your requests _do not_ require
71 /// live traffic information, $0.005 per _element_. **The below rate
72 /// assumes an average of 10 elements per request.**
73 /// ```rust
74 /// with_rate(
75 /// Api::DistanceMatrix,
76 /// (GOOGLE_CREDIT / (0.005 * 10.0)) as u16,
77 /// Duration::from_secs(SECONDS_PER_MONTH)
78 /// )
79 /// ```
80 ///
81 /// * [Distance Matrix
82 /// Advanced](https://developers.google.com/maps/billing/gmp-billing#distance-matrix-advanced)
83 /// API. You are billed for this SKU when your requests require live
84 /// traffic information, $0.01 USD per _element_. **The below rate assumes
85 /// an average of 10 elements per request.**
86 /// ```rust
87 /// with_rate(
88 /// Api::DistanceMatrix,
89 /// (GOOGLE_CREDIT / (0.01 * 10.0)) as u16,
90 /// Duration::from_secs(SECONDS_PER_MONTH)
91 /// )
92 /// ```
93 ///
94 /// * [Elevation](https://developers.google.com/maps/billing/gmp-billing#elevation)
95 /// API. $0.005 USD per request.
96 /// ```rust
97 /// with_rate(Api::Elevation, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
98 /// ```
99 ///
100 /// * [Geocoding](https://developers.google.com/maps/billing/gmp-billing#geolocation)
101 /// API. $0.005 USD per request.
102 /// ```rust
103 /// with_rate(Api::Geocoding, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
104 /// ```
105 ///
106 /// * [Time
107 /// Zone](https://developers.google.com/maps/billing/gmp-billing#time-zone)
108 /// API. $0.005 USD per request.
109 /// ```rust
110 /// with_rate(Api::TimeZone, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
111 /// ```
112 pub fn with_rate(&mut self, api: &Api, requests: u16, per_duration: Duration) -> &mut Self {
113 self.rate_limit.with_rate(api, requests, per_duration);
114 self
115 } // fn
116} // impl