Struct google_maps::ClientSettings[][src]

pub struct ClientSettings {
    pub key: String,
    pub rate_limit: RequestRate,
    pub reqwest_client: Option<Client>,
}
Expand description

Use the ClientSettings struct’s implemented methods to set your Google API key and other settings such as: rate limiting, maxium retries, & retry delay times for your requests.

This structure contains your API key - the preferred way for authenticating with the Google Maps Platform APIs, your request rate limit settings, and your automatic retry settings.

How to use this structure’s methods in a builder pattern:

let mut my_settings = ClientSettings::new(YOUR_GOOGLE_API_KEY_HERE)
    .with_max_delay(std::time::Duration::from_secs(32))
    .with_max_retries(10)
    .with_rate(Api::All, 1, std::time::Duration::from_secs(2))
    .finalize();

Fields

key: String

Your application’s API key. This key identifies your application for purposes of quota management. Learn how to get a key. Contains the application’s API key and other settings.

rate_limit: RequestRate

Rate limits for each of the Google Cloud Maps Platform APIs.

reqwest_client: Option<Client>

Allows you to optionally provide your own pre-configured reqwest client that will be used by the Google Maps client.

Implementations

Completes the builder pattern into a final structure.

Arguments:

This method accepts no arguments.

Initialize the settings needed for a Google Cloud Maps API transaction.

The Directions API is a service that calculates directions between locations. You can search for directions for several modes of transportation, including transit, driving, walking, or cycling.

use google_maps::prelude::*;
directions(
    // Origin: Canadian Museum of Nature
    Location::Address(String::from("240 McLeod St, Ottawa, ON K2P 2R1")),
    // Destination: Canada Science and Technology Museum
    Location::LatLng(LatLng::try_from(dec!(45.403_509), dec!(-75.618_904))?),
)

The Distance Matrix API is a service that provides travel distance and time for a matrix of origins and destinations, based on the recommended route between start and end points.

use google_maps::prelude::*;
distance_matrix(
    // Origins
    vec![
        // Microsoft
        Waypoint::Address(String::from("One Microsoft Way, Redmond, WA 98052, United States")),
        // Cloudflare
        Waypoint::Address(String::from("101 Townsend St, San Francisco, CA 94107, United States")),
    ],
    // Destinations
    vec![
        // Google
        Waypoint::PlaceId(String::from("ChIJj61dQgK6j4AR4GeTYWZsKWw")),
        // Mozilla
        Waypoint::LatLng(LatLng::try_from(dec!(37.387_316), dec!(-122.060_008))?),
    ],
)

The Elevation API provides elevation data for all locations on the surface of the earth, including depth locations on the ocean floor (which return negative values).

The Geocoding API is a service that provides geocoding and reverse geocoding of addresses. Geocoding is the process of converting addresses (like a street address) into geographic coordinates (like latitude and longitude), which you can use to place markers on a map, or position the map.

The Geocoding API is a service that provides geocoding and reverse geocoding of addresses. Reverse geocoding is the process of converting geographic coordinates into a human-readable address.

use google_maps::LatLng;
reverse_geocoding(
    // 10 Downing St, Westminster, London
    LatLng::try_from(dec!(51.503_364), dec!(-0.127_625))?,
)

The Time Zone API provides time offset data for locations on the surface of the earth. You request the time zone information for a specific latitude/longitude pair and date. The API returns the name of that time zone, the time offset from UTC, and the daylight savings offset.

use google_maps::{LatLng, NaiveDate};
time_zone(
     // St. Vitus Cathedral in Prague, Czechia
     LatLng::try_from(dec!(50.090_903), dec!(14.400_512))?,
     // Tuesday February 23, 2020 @ 6:00:00 pm
     NaiveDate::from_ymd(2020, 2, 23).and_hms(18, 00, 0)
)

Sets the rate limit for the specified API.

Arguments

  • api ‧ Which Google Maps API are you setting the rate limit for? For example, Api::Directions, Api::DistanceMatrix, Api::Elevation, Api::Geocoding, Api::TimeZone, and so on. The Api::All rate limit is applied to all Google Maps API requests in addition to the per-API rate limits.

  • requests ‧ The number of requests the client library is attempting to target. For example, 2 requests per 1 hour.

  • duration ‧ The duration for the targeted request rate. For example, 1 request per 1 minute. This can be defined using the std::time::Duration methods.

Examples:

The following examples show how one might try to limit the request rate to achieve maximum throughput while minimizing charges by Google.

The following rates are subject to change by Google. Please review the current Google Maps Platform billing rates.

This Google client library’s rate limiting is not persistent. If your program is often restarted, it is easily possible to exceed Google’s monthly free credit. These are approximations and examples.

To accurately minimize billing charges by Google, please use the Google Cloud Platform Console IAM & admin to set quotas for each API on the server’s side.

You are responsible for all charges. Use with care.

// Assumptions:
// - $200.00 USD monthly free credit from Google. Thanks, guys!
// - 2,629,746 seconds in a month.
const GOOGLE_CREDIT: f64 = 200.0;
const SECONDS_PER_MONTH: u64 = 2_629_746;
  • Directions API. You are billed for this SKU when your request does not use live traffic information, arrival or departure times, < 10 waypoints, and no waypoint optimization, $0.005 USD per request.
with_rate(Api::Directions, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
  • Directions Advanced API. You are billed for this SKU when your request requires live traffic information, > 10 waypoints, and/or waypoint optimization, $0.01 per request.
with_rate(Api::Directions, (GOOGLE_CREDIT / 0.01) as u16, Duration::from_secs(SECONDS_PER_MONTH))
  • Distance Matrix API. You are billed for this SKU when your requests do not require live traffic information, $0.005 per element. The below rate assumes an average of 10 elements per request.
with_rate(
    Api::DistanceMatrix,
    (GOOGLE_CREDIT / (0.005 * 10.0)) as u16,
    Duration::from_secs(SECONDS_PER_MONTH)
)
  • Distance Matrix Advanced API. You are billed for this SKU when your requests require live traffic information, $0.01 USD per element. The below rate assumes an average of 10 elements per request.
with_rate(
    Api::DistanceMatrix,
    (GOOGLE_CREDIT / (0.01 * 10.0)) as u16,
    Duration::from_secs(SECONDS_PER_MONTH)
)
with_rate(Api::Elevation, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
with_rate(Api::Geocoding, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))
with_rate(Api::TimeZone, (GOOGLE_CREDIT / 0.005) as u16, Duration::from_secs(SECONDS_PER_MONTH))

Passes a user configured reqwest client for the Google Maps client to use. This allows the you to have more control over the how the Google Maps client connects to the Google Maps server.

Mause mentioned that this feature could be useful for writing tests. Thanks for the suggestion!

Arguments

  • reqwest_client ‧ A reqwest client built using the reqwest::Client::builder() function.

Examples:

let reqwest_client = reqwest::Client::builder()
    .user_agent("My Cool App v1.0")
    .build()?;

let mut google_maps_client = ClientSettings::new("YOUR_API_KEY_HERE")
    .with_reqwest_client(reqwest_client)
    .finalize();

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more