Skip to main content

dataplane_sdk/
sdk.rs

1//  Copyright (c) 2026 Metaform Systems, Inc
2//
3//  This program and the accompanying materials are made available under the
4//  terms of the Apache License, Version 2.0 which is available at
5//  https://www.apache.org/licenses/LICENSE-2.0
6//
7//    SPDX-License-Identifier: Apache-2.0
8//
9//    Contributors:
10//         Metaform Systems, Inc. - initial API and implementation
11
12use std::{ops::Deref, sync::Arc};
13
14use crate::core::{
15    db::{control_plane::ControlPlaneRepo, data_flow::DataFlowRepo, tx::TransactionalContext},
16    handler::DataFlowHandler,
17};
18
19pub mod internal;
20
21pub struct DataPlaneSdk<C: TransactionalContext>(Arc<internal::DataPlaneSdkInternal<C>>);
22
23impl<C> Clone for DataPlaneSdk<C>
24where
25    C: TransactionalContext,
26{
27    fn clone(&self) -> Self {
28        Self(self.0.clone())
29    }
30}
31
32impl<C> DataPlaneSdk<C>
33where
34    C: TransactionalContext,
35{
36    pub fn builder(ctx: C) -> DataPlaneSdkBuilder<C> {
37        DataPlaneSdkBuilder::new(ctx)
38    }
39
40    pub(crate) fn new(
41        ctx: C,
42        repo: Box<dyn DataFlowRepo<Transaction = C::Transaction>>,
43        control_plane_repo: Box<dyn ControlPlaneRepo<Transaction = C::Transaction>>,
44        handler: Box<dyn DataFlowHandler<Transaction = C::Transaction>>,
45        client: reqwest::Client,
46    ) -> Self {
47        Self(Arc::new(internal::DataPlaneSdkInternal {
48            ctx,
49            repo,
50            control_plane_repo,
51            handler,
52            client,
53        }))
54    }
55}
56
57impl<C> Deref for DataPlaneSdk<C>
58where
59    C: TransactionalContext,
60{
61    type Target = internal::DataPlaneSdkInternal<C>;
62
63    fn deref(&self) -> &Self::Target {
64        &self.0
65    }
66}
67
68pub struct DataPlaneSdkBuilder<C>
69where
70    C: TransactionalContext,
71{
72    ctx: C,
73    repo: Option<Box<dyn DataFlowRepo<Transaction = C::Transaction>>>,
74    control_plane_repo: Option<Box<dyn ControlPlaneRepo<Transaction = C::Transaction>>>,
75    handler: Option<Box<dyn DataFlowHandler<Transaction = C::Transaction>>>,
76    client: Option<reqwest::Client>,
77}
78
79impl<C> DataPlaneSdkBuilder<C>
80where
81    C: TransactionalContext,
82{
83    pub(crate) fn new(ctx: C) -> Self {
84        Self {
85            ctx,
86            repo: None,
87            control_plane_repo: None,
88            handler: None,
89            client: None,
90        }
91    }
92
93    pub fn with_repo(
94        mut self,
95        repo: impl DataFlowRepo<Transaction = C::Transaction> + 'static,
96    ) -> Self {
97        self.repo = Some(Box::new(repo));
98        self
99    }
100
101    pub fn with_control_plane_repo(
102        mut self,
103        control_plane_repo: impl ControlPlaneRepo<Transaction = C::Transaction> + 'static,
104    ) -> Self {
105        self.control_plane_repo = Some(Box::new(control_plane_repo));
106        self
107    }
108
109    pub fn with_handler(
110        mut self,
111        handler: impl DataFlowHandler<Transaction = C::Transaction> + 'static,
112    ) -> Self {
113        self.handler = Some(Box::new(handler));
114        self
115    }
116
117    /// Overrides the `reqwest::Client` used to send control plane notifications.
118    /// Defaults to `reqwest::Client::new()` when not set.
119    pub fn with_client(mut self, client: reqwest::Client) -> Self {
120        self.client = Some(client);
121        self
122    }
123
124    pub fn build(self) -> Result<DataPlaneSdk<C>, String> {
125        let repo = self.repo.ok_or("DataFlowRepo is not set")?;
126
127        let control_plane_repo = self
128            .control_plane_repo
129            .ok_or("ControlPlaneRepo is not set")?;
130
131        let handler = self.handler.ok_or("DataFlowHandler is not set")?;
132
133        let client = self.client.unwrap_or_default();
134
135        Ok(DataPlaneSdk::new(
136            self.ctx,
137            repo,
138            control_plane_repo,
139            handler,
140            client,
141        ))
142    }
143}