Skip to content

Commit 233e531

Browse files
authored
feat(auth-registry): add get_auth_registry() for Python (#1032)
1 parent afe61af commit 233e531

File tree

3 files changed

+25
-11
lines changed

3 files changed

+25
-11
lines changed

python/cocoindex/auth_registry.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44

55
from dataclasses import dataclass
66
from typing import Generic, TypeVar
7-
import threading
87

98
from . import _engine # type: ignore
10-
from .convert import dump_engine_object
9+
from .convert import dump_engine_object, load_engine_object
1110

1211
T = TypeVar("T")
1312

@@ -38,3 +37,8 @@ def add_auth_entry(key: str, value: T) -> AuthEntryReference[T]:
3837
def ref_auth_entry(key: str) -> AuthEntryReference[T]:
3938
"""Reference an auth entry by its key."""
4039
return AuthEntryReference(key)
40+
41+
42+
def get_auth_entry(cls: type[T], ref: TransientAuthEntryReference[T]) -> T:
43+
"""Get an auth entry by its key."""
44+
return load_engine_object(cls, _engine.get_auth_entry(ref.key))

src/base/spec.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,14 @@ pub struct TransientFlowSpec {
516516
pub output_value: ValueMapping,
517517
}
518518

519+
impl<T> AuthEntryReference<T> {
520+
pub fn new(key: String) -> Self {
521+
Self {
522+
key,
523+
_phantom: std::marker::PhantomData,
524+
}
525+
}
526+
}
519527
pub struct AuthEntryReference<T> {
520528
pub key: String,
521529
_phantom: std::marker::PhantomData<T>,
@@ -535,10 +543,7 @@ impl<T> fmt::Display for AuthEntryReference<T> {
535543

536544
impl<T> Clone for AuthEntryReference<T> {
537545
fn clone(&self) -> Self {
538-
Self {
539-
key: self.key.clone(),
540-
_phantom: std::marker::PhantomData,
541-
}
546+
Self::new(self.key.clone())
542547
}
543548
}
544549

@@ -562,10 +567,7 @@ impl<'de, T> Deserialize<'de> for AuthEntryReference<T> {
562567
D: serde::Deserializer<'de>,
563568
{
564569
let untyped_ref = UntypedAuthEntryReference::<String>::deserialize(deserializer)?;
565-
Ok(AuthEntryReference {
566-
key: untyped_ref.key,
567-
_phantom: std::marker::PhantomData,
568-
})
570+
Ok(AuthEntryReference::new(untyped_ref.key))
569571
}
570572
}
571573

src/py/mod.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::execution::evaluator::evaluate_transient_flow;
22
use crate::prelude::*;
33

44
use crate::base::schema::{FieldSchema, ValueType};
5-
use crate::base::spec::{NamedSpec, OutputMode, ReactiveOpSpec, SpecFormatter};
5+
use crate::base::spec::{AuthEntryReference, NamedSpec, OutputMode, ReactiveOpSpec, SpecFormatter};
66
use crate::lib_context::{
77
QueryHandlerContext, clear_lib_context, get_auth_registry, init_lib_context,
88
};
@@ -630,6 +630,13 @@ fn add_transient_auth_entry(value: Pythonized<serde_json::Value>) -> PyResult<St
630630
.into_py_result()
631631
}
632632

633+
#[pyfunction]
634+
fn get_auth_entry(key: String) -> PyResult<Pythonized<serde_json::Value>> {
635+
let auth_ref = AuthEntryReference::new(key);
636+
let json_value: serde_json::Value = get_auth_registry().get(&auth_ref).into_py_result()?;
637+
Ok(Pythonized(json_value))
638+
}
639+
633640
#[pyfunction]
634641
fn get_app_namespace(py: Python<'_>) -> PyResult<String> {
635642
let app_namespace = py
@@ -671,6 +678,7 @@ fn cocoindex_engine(m: &Bound<'_, PyModule>) -> PyResult<()> {
671678
m.add_function(wrap_pyfunction!(remove_flow_context, m)?)?;
672679
m.add_function(wrap_pyfunction!(add_auth_entry, m)?)?;
673680
m.add_function(wrap_pyfunction!(add_transient_auth_entry, m)?)?;
681+
m.add_function(wrap_pyfunction!(get_auth_entry, m)?)?;
674682
m.add_function(wrap_pyfunction!(get_app_namespace, m)?)?;
675683

676684
m.add_class::<builder::flow_builder::FlowBuilder>()?;

0 commit comments

Comments
 (0)