Skip to main content

objectiveai_api/functions/
client.rs

1//! Functions client implementation.
2
3use crate::ctx;
4use std::sync::Arc;
5
6/// Client for function operations.
7pub struct Client<CTXEXT, FFN, RTRVL> {
8    /// Fetcher for Function definitions.
9    pub function_fetcher: Arc<FFN>,
10    /// Client for listing functions and getting usage statistics.
11    pub retrieval_client: Arc<RTRVL>,
12    pub _ctx_ext: std::marker::PhantomData<CTXEXT>,
13}
14
15impl<CTXEXT, FFN, RTRVL> Client<CTXEXT, FFN, RTRVL> {
16    /// Creates a new functions client.
17    pub fn new(
18        function_fetcher: Arc<FFN>,
19        retrieval_client: Arc<RTRVL>,
20    ) -> Self {
21        Self {
22            function_fetcher,
23            retrieval_client,
24            _ctx_ext: std::marker::PhantomData,
25        }
26    }
27}
28
29impl<CTXEXT, FFN, RTRVL> Client<CTXEXT, FFN, RTRVL>
30where
31    CTXEXT: Send + Sync + 'static,
32    FFN: super::function_fetcher::Fetcher<CTXEXT> + Send + Sync + 'static,
33    RTRVL: super::retrieval_client::Client<CTXEXT> + Send + Sync + 'static,
34{
35    /// Lists functions.
36    pub async fn list_functions(
37        &self,
38        ctx: ctx::Context<CTXEXT>,
39    ) -> Result<
40        objectiveai::functions::response::ListFunction,
41        objectiveai::error::ResponseError,
42    > {
43        self.retrieval_client.list_functions(ctx).await
44    }
45
46    /// Retrieves a function by owner/repository/commit.
47    pub async fn get_function(
48        &self,
49        ctx: ctx::Context<CTXEXT>,
50        owner: &str,
51        repository: &str,
52        commit: Option<&str>,
53    ) -> Result<
54        objectiveai::functions::response::GetFunction,
55        objectiveai::error::ResponseError,
56    > {
57        self.function_fetcher
58            .fetch(ctx, owner, repository, commit)
59            .await?
60            .ok_or_else(|| objectiveai::error::ResponseError {
61                code: 404,
62                message: serde_json::json!({
63                    "kind": "functions",
64                    "error": "Function not found"
65                }),
66            })
67    }
68
69    /// Retrieves usage statistics for a function.
70    pub async fn get_function_usage(
71        &self,
72        ctx: ctx::Context<CTXEXT>,
73        owner: &str,
74        repository: &str,
75        commit: Option<&str>,
76    ) -> Result<
77        objectiveai::functions::response::UsageFunction,
78        objectiveai::error::ResponseError,
79    > {
80        self.retrieval_client
81            .get_function_usage(ctx, owner, repository, commit)
82            .await
83    }
84}