qcs-api-client-common 0.17.4

Common code for QCS API clients
Documentation
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
#![allow(unused_qualifications)]
#![allow(non_local_definitions, reason = "necessary for pyo3::pymethods")]

use pyo3::{
    exceptions::PyValueError,
    prelude::*,
    types::{PyAnyMethods, PyString},
};
use rigetti_pyo3::{create_init_submodule, impl_repr, py_function_sync_async, sync::Awaitable};
use tokio_util::sync::CancellationToken;

#[cfg(feature = "stubs")]
use pyo3_stub_gen::derive::{gen_stub_pyfunction, gen_stub_pymethods};

use crate::configuration::{
    secrets::{DEFAULT_SECRETS_PATH, SECRETS_PATH_VAR},
    settings::{DEFAULT_SETTINGS_PATH, SETTINGS_PATH_VAR},
    ClientConfigurationBuilderError, API_URL_VAR, DEFAULT_API_URL, DEFAULT_GRPC_API_URL,
    DEFAULT_PROFILE_NAME, DEFAULT_QUILC_URL, DEFAULT_QVM_URL, GRPC_API_URL_VAR, PROFILE_NAME_VAR,
    QUILC_URL_VAR, QVM_URL_VAR,
};
use crate::errors;

use super::{
    error::TokenError,
    secrets::{SecretAccessToken, SecretRefreshToken},
    settings::AuthServer,
    tokens::{ClientCredentials, ClientSecret, ExternallyManaged, PkceFlow},
    ClientConfiguration, ClientConfigurationBuilder, LoadError, OAuthGrant, OAuthSession,
    RefreshToken, TokenDispatcher,
};

create_init_submodule! {
    classes: [
        ClientConfiguration,
        ClientConfigurationBuilder,
        AuthServer,
        OAuthSession,
        RefreshToken,
        ClientCredentials,
        ClientSecret,
        ExternallyManaged,
        PkceFlow,
        SecretAccessToken,
        SecretRefreshToken,
        TokenDispatcher
    ],

    consts: [
        API_URL_VAR,
        DEFAULT_API_URL,
        DEFAULT_GRPC_API_URL,
        DEFAULT_PROFILE_NAME,
        DEFAULT_QUILC_URL,
        DEFAULT_QVM_URL,
        DEFAULT_SECRETS_PATH,
        DEFAULT_SETTINGS_PATH,
        GRPC_API_URL_VAR,
        PROFILE_NAME_VAR,
        QUILC_URL_VAR,
        QVM_URL_VAR,
        SECRETS_PATH_VAR,
        SETTINGS_PATH_VAR
    ],

    errors: [
        errors::ClientConfigurationBuilderError,
        errors::ConfigurationError,
        errors::LoadError,
        errors::TokenError
    ],

    funcs: [
        py_get_oauth_session,
        py_get_oauth_session_async,
        py_get_bearer_access_token,
        py_get_bearer_access_token_async,
        py_request_access_token,
        py_request_access_token_async
    ],

}

#[cfg(feature = "stubs")]
#[derive(IntoPyObject)]
struct Final<T>(T);

#[cfg(feature = "stubs")]
impl<T> pyo3_stub_gen::PyStubType for Final<T> {
    fn type_output() -> pyo3_stub_gen::TypeInfo {
        pyo3_stub_gen::TypeInfo::with_module("typing.Final", "typing".into())
    }
}

/// Adds module-level `str` to the `qcs_api_client_common.configuration` stub file.
macro_rules! stub_consts {
    ( $($name:ident),* ) => {
        $(
            #[cfg(feature = "stubs")]
            ::pyo3_stub_gen::module_variable!(
                "qcs_api_client_common.configuration",
                stringify!($name),
                Final<&str>,
                Final($name)
            );
        )*
    };
}

stub_consts!(
    API_URL_VAR,
    DEFAULT_API_URL,
    DEFAULT_GRPC_API_URL,
    DEFAULT_PROFILE_NAME,
    DEFAULT_QUILC_URL,
    DEFAULT_QVM_URL,
    DEFAULT_SECRETS_PATH,
    DEFAULT_SETTINGS_PATH,
    GRPC_API_URL_VAR,
    PROFILE_NAME_VAR,
    QUILC_URL_VAR,
    QVM_URL_VAR,
    SECRETS_PATH_VAR,
    SETTINGS_PATH_VAR
);

/// Manual implementation to extract tokens from Python objects.
///
/// For Python functions that require a `SecretRefreshToken`,
/// users can provide a Python `str`, a `RefreshToken`, or a `SecretRefreshToken`.
impl FromPyObject<'_, '_> for SecretRefreshToken {
    type Error = PyErr;

    fn extract(obj: Borrowed<'_, '_, PyAny>) -> Result<Self, Self::Error> {
        if let Ok(token) = obj.cast::<PyString>() {
            Ok(Self::__new__(token.extract()?))
        } else if let Ok(token) = obj.cast::<RefreshToken>() {
            Ok(token.borrow().refresh_token.clone())
        } else if let Ok(token) = obj.cast::<Self>() {
            Ok(token.borrow().clone())
        } else {
            Err(PyValueError::new_err(
                "expected str | SecretRefreshToken | RefreshToken",
            ))
        }
    }
}

impl FromPyObject<'_, '_> for SecretAccessToken {
    type Error = PyErr;

    fn extract(obj: Borrowed<'_, '_, PyAny>) -> Result<Self, Self::Error> {
        if let Ok(token) = obj.cast::<PyString>() {
            Ok(Self::__new__(token.extract()?))
        } else if let Ok(token) = obj.cast::<Self>() {
            Ok(token.borrow().clone())
        } else {
            Err(PyValueError::new_err("expected str | SecretAccessToken"))
        }
    }
}

impl FromPyObject<'_, '_> for ClientSecret {
    type Error = PyErr;

    fn extract(obj: Borrowed<'_, '_, PyAny>) -> Result<Self, Self::Error> {
        if let Ok(token) = obj.cast::<PyString>() {
            Ok(Self::__new__(token.extract()?))
        } else if let Ok(token) = obj.cast::<Self>() {
            Ok(token.borrow().clone())
        } else {
            Err(PyValueError::new_err("expected str | ClientSecret"))
        }
    }
}

impl_repr!(RefreshToken);

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl RefreshToken {
    #[new]
    const fn __new__(refresh_token: SecretRefreshToken) -> Self {
        Self::new(refresh_token)
    }
}

impl_repr!(ClientCredentials);

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl ClientCredentials {
    #[new]
    fn __new__(client_id: String, client_secret: String) -> Self {
        Self::new(client_id, ClientSecret::from(client_secret))
    }
}

impl_repr!(ExternallyManaged);

#[cfg_attr(not(feature = "stubs"), optipy::strip_pyo3(only_stubs))]
#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl ExternallyManaged {
    #[new]
    fn __new__(
        #[gen_stub(
            override_type(
                type_repr="collections.abc.Callable[[AuthServer], str]",
                imports=("collections.abc")
            )
        )]
        refresh_function: Bound<'_, PyAny>,
    ) -> PyResult<Self> {
        if !refresh_function.is_callable() {
            return Err(pyo3::exceptions::PyTypeError::new_err(
                "refresh_function must be callable",
            ));
        }

        let refresh_function = refresh_function.unbind();

        #[allow(trivial_casts)] // Compilation fails without the cast.
        // The provided refresh function will panic if there is an issue with the refresh function.
        // This raises a `PanicException` within Python.
        let refresh_closure = move |auth_server: AuthServer| {
            let refresh_function = Python::attach(|py| refresh_function.clone_ref(py));
            Box::pin(async move {
                Python::attach(|py| {
                    let result = refresh_function.call1(py, (auth_server,));
                    match result {
                        Ok(value) => value
                            .extract::<String>(py)
                            .map_or_else(|_| panic!("ExternallyManaged refresh function returned an unexpected type. Expected a string, got {value:?}"), Ok),
                        Err(err) => Err(Box::<dyn std::error::Error + Send + Sync>::from(err))
                    }
                })
            }) as super::tokens::RefreshResult
        };

        Ok(Self::new(refresh_closure))
    }
}

impl_repr!(PkceFlow);

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl PkceFlow {
    #[new]
    fn __new__(py: Python<'_>, auth_server: AuthServer) -> PyResult<Self> {
        pyo3_async_runtimes::tokio::run(py, async move {
            let cancel_token = cancel_token_with_ctrl_c();
            Self::new_login_flow(cancel_token, &auth_server)
                .await
                .map_err(|err| LoadError::from(err).into())
        })
    }
}

#[cfg(feature = "stubs")]
pyo3_stub_gen::impl_stub_type!(
    OAuthGrant = RefreshToken | ClientConfiguration | ExternallyManaged | PkceFlow
);

impl_repr!(OAuthSession);

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl OAuthSession {
    #[new]
    #[pyo3(signature = (payload, auth_server, access_token = None))]
    const fn __new__(
        payload: OAuthGrant,
        auth_server: AuthServer,
        access_token: Option<SecretAccessToken>,
    ) -> Self {
        Self::new(payload, auth_server, access_token)
    }

    #[pyo3(name = "validate")]
    fn py_validate(&self) -> Result<SecretAccessToken, TokenError> {
        self.validate()
    }

    #[pyo3(name = "request_access_token")]
    fn py_request_access_token(&self, py: Python<'_>) -> PyResult<SecretAccessToken> {
        py_request_access_token(py, self.clone())
    }

    #[pyo3(name = "request_access_token_async")]
    fn py_request_access_token_async<'py>(
        &self,
        py: Python<'py>,
    ) -> PyResult<Awaitable<'py, SecretAccessToken>> {
        py_request_access_token_async(py, self.clone())
    }
}

py_function_sync_async! {
    #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_api_client_common.configuration"))]
    #[pyfunction]
    async fn get_oauth_session(tokens: Option<TokenDispatcher>) -> PyResult<OAuthSession> {
        Ok(tokens.ok_or(TokenError::NoRefreshToken)?.tokens().await)
    }
}

py_function_sync_async! {
    /// Gets the `Bearer` access token, refreshing it if it is expired.
    ///
    /// # Errors
    ///
    /// Raises a `TokenError` if there's a problem providing the token.
    #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_api_client_common.configuration"))]
    #[pyfunction]
    async fn get_bearer_access_token(configuration: ClientConfiguration) -> PyResult<SecretAccessToken> {
        configuration.get_bearer_access_token().await.map_err(PyErr::from)
    }
}

py_function_sync_async! {
    /// Request and return an updated access token using these credentials.
    ///
    /// # Errors
    ///
    /// Raises a `TokenError` if there's a problem providing the token.
    #[cfg_attr(feature = "stubs", gen_stub_pyfunction(module = "qcs_api_client_common.configuration"))]
    #[pyfunction]
    async fn request_access_token(session: OAuthSession) -> PyResult<SecretAccessToken> {
        session.clone().request_access_token().await.cloned().map_err(PyErr::from)
    }
}

impl_repr!(ClientConfiguration);

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl ClientConfiguration {
    #[new]
    #[pyo3(signature = (
            api_url = None, grpc_api_url = None, quilc_url = None, qvm_url = None,
            oauth_session = None,
            ))]
    fn __new__(
        api_url: Option<String>,
        grpc_api_url: Option<String>,
        quilc_url: Option<String>,
        qvm_url: Option<String>,
        oauth_session: Option<OAuthSession>,
    ) -> Self {
        let mut builder = ClientConfigurationBuilder::default();

        if let Some(api_url) = api_url {
            builder.api_url(api_url);
        }

        if let Some(grpc_api_url) = grpc_api_url {
            builder.grpc_api_url(grpc_api_url);
        }

        if let Some(quilc_url) = quilc_url {
            builder.quilc_url(quilc_url);
        }

        if let Some(qvm_url) = qvm_url {
            builder.qvm_url(qvm_url);
        }

        builder.oauth_session(oauth_session);

        builder
            .build()
            .expect("our builder is valid regardless of which URLs are set")
    }

    #[staticmethod]
    #[pyo3(name = "load_default")]
    fn py_load_default(_py: Python<'_>) -> Result<Self, LoadError> {
        Self::load_default()
    }

    #[staticmethod]
    #[pyo3(name = "load_default_with_login")]
    fn py_load_default_with_login(py: Python<'_>) -> PyResult<Self> {
        pyo3_async_runtimes::tokio::run(py, async move {
            let cancel_token = cancel_token_with_ctrl_c();
            Self::load_with_login(cancel_token, None)
                .await
                .map_err(Into::into)
        })
    }

    #[staticmethod]
    #[pyo3(name = "builder")]
    fn py_builder() -> ClientConfigurationBuilder {
        ClientConfigurationBuilder::default()
    }

    #[staticmethod]
    #[pyo3(name = "load_profile")]
    fn py_load_profile(_py: Python<'_>, profile_name: String) -> Result<Self, LoadError> {
        Self::load_profile(profile_name)
    }

    /// Gets the `Bearer` access token, refreshing it if it is expired.
    ///
    /// # Errors
    ///
    /// Raises a `TokenError` if there's a problem providing the token.
    #[pyo3(name = "get_bearer_access_token")]
    fn py_get_bearer_access_token(&self, py: Python<'_>) -> PyResult<SecretAccessToken> {
        py_get_bearer_access_token(py, self.clone())
    }

    #[pyo3(name = "get_bearer_access_token_async")]
    fn py_get_bearer_access_token_async<'py>(
        &self,
        py: Python<'py>,
    ) -> PyResult<Awaitable<'py, SecretAccessToken>> {
        py_get_bearer_access_token_async(py, self.clone())
    }

    /// Get the configured [`OAuthSession`].
    ///
    /// # Errors
    ///
    /// Raises a `TokenError` if there is a problem fetching the tokens.
    pub fn get_oauth_session(&self, py: Python<'_>) -> PyResult<OAuthSession> {
        py_get_oauth_session(py, self.oauth_session.clone())
    }

    fn get_oauth_session_async<'py>(
        &self,
        py: Python<'py>,
    ) -> PyResult<Awaitable<'py, OAuthSession>> {
        py_get_oauth_session_async(py, self.oauth_session.clone())
    }
}

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl ClientConfigurationBuilder {
    #[new]
    fn __new__() -> Self {
        Self::default()
    }

    /// The [`OAuthSession`] to use to authenticate with the QCS API.
    ///
    /// When set to [`None`], the configuration will not manage an OAuth Session, and access to the
    /// QCS API will be limited to unauthenticated routes.
    #[setter]
    fn set_oauth_session(&mut self, oauth_session: Option<OAuthSession>) {
        self.oauth_session = Some(oauth_session.map(Into::into));
    }

    #[pyo3(name = "build")]
    fn py_build(&self) -> Result<ClientConfiguration, ClientConfigurationBuilderError> {
        self.build()
    }
}

impl_repr!(AuthServer);

#[cfg_attr(feature = "stubs", gen_stub_pymethods)]
#[pymethods]
impl AuthServer {
    #[new]
    #[pyo3(signature = (client_id, issuer, scopes = None))]
    const fn __new__(client_id: String, issuer: String, scopes: Option<Vec<String>>) -> Self {
        Self::new(client_id, issuer, scopes)
    }

    #[staticmethod]
    #[pyo3(name = "default")]
    fn py_default() -> Self {
        Self::default()
    }
}

fn cancel_token_with_ctrl_c() -> CancellationToken {
    let cancel_token = CancellationToken::new();
    let cancel_token_ctrl_c = cancel_token.clone();
    tokio::spawn(cancel_token.clone().run_until_cancelled_owned(async move {
        match tokio::signal::ctrl_c().await {
            Ok(()) => cancel_token_ctrl_c.cancel(),
            Err(error) => eprintln!("Failed to register signal handler: {error}"),
        }
    }));
    cancel_token
}