Skip to main content

azure_core/http/options/
mod.rs

1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3
4mod instrumentation;
5mod user_agent;
6
7pub use instrumentation::*;
8use std::{borrow::Cow, collections::HashSet, sync::Arc};
9use typespec_client_core::http::{policies::Policy, DEFAULT_ALLOWED_QUERY_PARAMETERS};
10pub use typespec_client_core::http::{
11    ClientMethodOptions, ExponentialRetryOptions, FixedRetryOptions, LoggingOptions,
12    PipelineOptions, RetryOptions, Transport,
13};
14pub use user_agent::*;
15
16use crate::cloud::CloudConfiguration;
17
18/// Client options allow customization of general client policies, retry options, and more.
19#[derive(Clone, Debug, Default)]
20pub struct ClientOptions {
21    /// Policies called per call.
22    pub per_call_policies: Vec<Arc<dyn Policy>>,
23
24    /// Policies called per try.
25    pub per_try_policies: Vec<Arc<dyn Policy>>,
26
27    /// Retry options.
28    pub retry: RetryOptions,
29
30    /// Transport options.
31    pub transport: Option<Transport>,
32
33    /// User-Agent telemetry options.
34    pub user_agent: UserAgentOptions,
35
36    /// Options for request instrumentation, such as distributed tracing.
37    ///
38    /// If not specified, defaults to no instrumentation.
39    ///
40    pub instrumentation: InstrumentationOptions,
41
42    /// Logging options
43    ///
44    /// Specifies which headers and query parameters should be logged. All headers and query parameters not in the allow list will be redacted.
45    pub logging: LoggingOptions,
46
47    /// Cloud configuration for the client. If None, the client will default to Azure Public Cloud.
48    pub cloud: Option<Arc<CloudConfiguration>>,
49}
50
51pub(crate) struct CoreClientOptions {
52    pub(crate) user_agent: UserAgentOptions,
53    pub(crate) instrumentation: InstrumentationOptions,
54    pub(crate) allowed_query_params: HashSet<Cow<'static, str>>,
55}
56
57impl ClientOptions {
58    /// Efficiently deconstructs into owned [`typespec_client_core::http::ClientOptions`] as well as unwrapped or default Azure-specific options.
59    ///
60    /// If instead we implemented [`Into`], we'd have to clone Azure-specific options instead of moving memory of [`Some`] values.
61    pub(in crate::http) fn deconstruct(
62        self,
63    ) -> (CoreClientOptions, typespec_client_core::http::ClientOptions) {
64        // Merge the default allowed query parameters with any additional ones from logging options.
65        // This merged set is shared by both the logging policy and the request instrumentation policy
66        // to sanitize query parameters in logs and traced URLs.
67        let mut allowed_query_params = (*DEFAULT_ALLOWED_QUERY_PARAMETERS).clone();
68        allowed_query_params.extend(self.logging.additional_allowed_query_params.iter().cloned());
69
70        let options = typespec_client_core::http::ClientOptions {
71            per_call_policies: self.per_call_policies,
72            per_try_policies: self.per_try_policies,
73            retry: self.retry,
74            transport: self.transport,
75            logging: self.logging,
76        };
77
78        (
79            CoreClientOptions {
80                user_agent: self.user_agent,
81                instrumentation: self.instrumentation,
82                allowed_query_params,
83            },
84            options,
85        )
86    }
87}