// Copyright 2022 Rigetti Computing
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! This crate provides an autogenerated gRPC client for the QCS API, along with helper utilities for automatically loading
//! credentials from a user's QCS config and keeping authentication tokens refreshed.
//!
//! - [`get_channel`]: create a [`Channel`](tonic::transport::Channel) to the given gRPC endpoint with QCS authentication automatically set up.
//! - [`wrap_channel`]: wrap an existing [`Channel`](tonic::transport::Channel) with QCS authentication.
//!
//! ## Quick Start
//!
//! ```rust
//! use qcs_api_client_grpc::get_channel;
//! use qcs_api_client_grpc::services::controller::controller_client::ControllerClient;
//! use qcs_api_client_grpc::services::translation::translation_client::TranslationClient;
//!
//! async fn controller_request() {
//!     let uri = "example.per-qpu.rigetti.com:50000".parse().unwrap();
//!     let channel = get_channel(uri).unwrap();
//!     let mut client = ControllerClient::new(channel);
//!     // Use the client
//! }
//!
//! fn translation_request() {
//!     let uri = "example.translation.rigetti.com:50000".parse().unwrap();
//!     let channel = get_channel(uri).unwrap();
//!     let mut client = TranslationClient::new(channel);
//!     // Use the client
//! }
//! ```
//!
//! # Crate features
//!
//! * `server`: include the generated server code for both Controller Service
//!   and Translation Service
//! * `regen`: regenerate the protobuf code and store it in `./src/gen`
//!
//! By default, both features are disabled.

/// Utilities for creating and working with [`Channel`]s.
///
/// This module contains helper code for wrapping a [`Channel`] so that QCS credentials are
/// automatically used and refreshed as necessary.
///
/// Most users will want to use [`get_channel`], [`get_wrapped_channel`], or [`wrap_channel`].
///
/// # Generics
///
/// The functions for wrapping channels use generics `C: GrpcService<BoxBody>` to accept not only
/// bare [`Channel`]s but also channels wrapped in middleware like [`RefreshService`] or
/// [`RetryService`]
///
/// # Example
///
/// To create a channel that automatically retries on certain errors and refreshes QCS credentials
/// when authentication fails:
///
/// ```no_run
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn std::error::Error>> {
/// use qcs_api_client_grpc::tonic::parse_uri;
/// use qcs_api_client_grpc::tonic::get_channel;
/// use qcs_api_client_grpc::tonic::wrap_channel;
/// use qcs_api_client_grpc::tonic::wrap_channel_with_retry;
///
/// let uri = parse_uri("https://api.qcs.rigetti.com")?;
/// let channel = get_channel(uri)?;
/// let with_creds = wrap_channel(channel)?;
/// let with_creds_and_retry = wrap_channel_with_retry(with_creds);
/// // Use with_creds_and_retry as a gRPC client
/// # Ok(())
/// # }
/// ```
pub mod tonic;

pub use crate::tonic::{
    get_channel, get_channel_with_timeout, get_endpoint, get_endpoint_with_timeout,
    get_wrapped_channel, wrap_channel,
};
pub use qcs_api_client_common::configuration as client_configuration;

#[allow(clippy::derive_partial_eq_without_eq)]
#[allow(clippy::needless_borrow)]
pub mod models {
    pub mod controller {
        include!("gen/models.controller.rs");
        include!("gen/models.controller.serde.rs");
    }
    pub mod translation {
        include!("gen/models.translation.rs");
        include!("gen/models.translation.serde.rs");
    }
}
#[allow(clippy::derive_partial_eq_without_eq)]
pub mod services {
    pub mod controller {
        include!("gen/services.controller.rs");
        include!("gen/services.controller.serde.rs");
    }
    pub mod translation {
        include!("gen/services.translation.rs");
        include!("gen/services.translation.serde.rs");
    }
}
