Skip to content

Commit 4081435

Browse files
committed
feat: multi-node client
1 parent 1f68cce commit 4081435

File tree

158 files changed

+14609
-5582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

158 files changed

+14609
-5582
lines changed

typesense-go-unwrapped-api-spec.yaml renamed to openapi.yml

Lines changed: 3529 additions & 3462 deletions
Large diffs are not rendered by default.

preprocessed_openapi.yml

Lines changed: 4481 additions & 0 deletions
Large diffs are not rendered by default.

typesense/Cargo.toml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,15 @@ sha2 = "0.10"
2626
typesense_derive = { version = "0.1.0", path = "../typesense_derive", optional = true }
2727
typesense_codegen = { version = "0.25.0", path = "../typesense_codegen" }
2828
reqwest-retry = "0.7.0"
29-
reqwest = { version = "0.11", features = ["json"] }
30-
reqwest-middleware = { version = "0.3", features = ["json"] }
29+
reqwest = { version = "0.12", features = ["json"] }
30+
reqwest-middleware = { version = "0.4.2", features = ["json"] }
31+
thiserror = "1.0"
3132

3233
[dev-dependencies]
3334
dotenvy = "0.15"
3435
trybuild = "1.0.42"
36+
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
37+
wiremock = "0.5"
3538

3639
[target.'cfg(not(target_arch = "wasm32"))'.dev-dependencies]
3740
tokio = { version = "1.5", features = ["macros", "rt", "rt-multi-thread"] }
@@ -50,3 +53,6 @@ required-features = ["derive"]
5053
name = "api_tests"
5154
path = "tests/api/lib.rs"
5255

56+
[[test]]
57+
name = "client"
58+
path = "tests/client/mod.rs"
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//! Provides access to the API endpoint for posting analytics events.
2+
//!
3+
//! An `Events` instance is created via the `Client::analytics().events()` method.
4+
5+
use super::{Client, Error};
6+
use std::sync::Arc;
7+
use typesense_codegen::{
8+
apis::{analytics_api, configuration},
9+
models,
10+
};
11+
12+
/// Provides methods for interacting with analytics events.
13+
///
14+
/// This struct is created by calling `client.analytics().events()`.
15+
pub struct Events<'a> {
16+
pub(super) client: &'a Client,
17+
}
18+
19+
impl<'a> Events<'a> {
20+
/// Creates a new `Events` instance.
21+
pub(super) fn new(client: &'a Client) -> Self {
22+
Self { client }
23+
}
24+
25+
/// Posts an analytics event for tracking user behavior.
26+
///
27+
/// This is useful for features like "search result ranking based on popularity."
28+
///
29+
/// # Arguments
30+
/// * `schema` - An `AnalyticsEventCreateSchema` object representing the event.
31+
pub async fn create(
32+
&self,
33+
schema: models::AnalyticsEventCreateSchema,
34+
) -> Result<models::AnalyticsEventCreateResponse, Error<analytics_api::CreateAnalyticsEventError>>
35+
{
36+
let params = analytics_api::CreateAnalyticsEventParams {
37+
analytics_event_create_schema: schema,
38+
};
39+
self.client
40+
.execute(|config: Arc<configuration::Configuration>| {
41+
let params_for_move = params.clone();
42+
async move { analytics_api::create_analytics_event(&config, params_for_move).await }
43+
})
44+
.await
45+
}
46+
}

typesense/src/client/analytics/mod.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//! Provides access to the analytics API endpoints for managing rules and posting events.
2+
//!
3+
//! An `Analytics` instance is created via the main `Client::analytics()` method.
4+
pub mod events;
5+
pub mod rule;
6+
pub mod rules;
7+
use super::{Client, Error};
8+
pub use events::Events;
9+
pub use rule::Rule;
10+
pub use rules::Rules;
11+
12+
/// Provides methods for interacting with Typesense analytics rules and events.
13+
///
14+
/// This struct is created by calling `client.analytics()`.
15+
pub struct Analytics<'a> {
16+
pub(super) client: &'a Client,
17+
}
18+
19+
impl<'a> Analytics<'a> {
20+
/// Creates a new `Analytics` instance.
21+
pub(super) fn new(client: &'a Client) -> Self {
22+
Self { client }
23+
}
24+
25+
/// Provides access to endpoints for managing a collection of analytics rules.
26+
pub fn rules(&self) -> Rules<'a> {
27+
Rules::new(self.client)
28+
}
29+
30+
/// Provides access to endpoints for managing a single analytics rule.
31+
///
32+
/// # Arguments
33+
/// * `rule_name` - The name of the analytics rule to manage.
34+
pub fn rule(&self, rule_name: &'a str) -> Rule<'a> {
35+
Rule::new(self.client, rule_name)
36+
}
37+
38+
/// Provides access to the endpoint for creating analytics events.
39+
///
40+
/// Example: `client.analytics().events().create(...).await`
41+
pub fn events(&self) -> Events<'a> {
42+
Events::new(self.client)
43+
}
44+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Provides access to the API endpoints for managing a single analytics rule.
2+
//!
3+
//! An `Rule` instance is created via the `Client::analytics().rule("rule_name")` method.
4+
5+
use super::{Client, Error};
6+
use std::sync::Arc;
7+
use typesense_codegen::{
8+
apis::{analytics_api, configuration},
9+
models,
10+
};
11+
12+
/// Provides methods for interacting with a specific analytics rule.
13+
///
14+
/// This struct is created by calling `analytics.rule("rule_name")`.
15+
pub struct Rule<'a> {
16+
pub(super) client: &'a Client,
17+
pub(super) rule_name: &'a str,
18+
}
19+
20+
impl<'a> Rule<'a> {
21+
/// Creates a new `Rule` instance for a specific rule name.
22+
pub(super) fn new(client: &'a Client, rule_name: &'a str) -> Self {
23+
Self { client, rule_name }
24+
}
25+
26+
/// Retrieves the details of this specific analytics rule.
27+
pub async fn retrieve(
28+
&self,
29+
) -> Result<models::AnalyticsRuleSchema, Error<analytics_api::RetrieveAnalyticsRuleError>> {
30+
let params = analytics_api::RetrieveAnalyticsRuleParams {
31+
rule_name: self.rule_name.to_string(),
32+
};
33+
self.client
34+
.execute(|config: Arc<configuration::Configuration>| {
35+
let params_for_move = params.clone();
36+
async move { analytics_api::retrieve_analytics_rule(&config, params_for_move).await }
37+
})
38+
.await
39+
}
40+
41+
/// Permanently deletes this specific analytics rule.
42+
pub async fn delete(
43+
&self,
44+
) -> Result<models::AnalyticsRuleDeleteResponse, Error<analytics_api::DeleteAnalyticsRuleError>>
45+
{
46+
let params = analytics_api::DeleteAnalyticsRuleParams {
47+
rule_name: self.rule_name.to_string(),
48+
};
49+
self.client
50+
.execute(|config: Arc<configuration::Configuration>| {
51+
let params_for_move = params.clone();
52+
async move { analytics_api::delete_analytics_rule(&config, params_for_move).await }
53+
})
54+
.await
55+
}
56+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//! Provides access to the API endpoints for managing analytics rules.
2+
//!
3+
//! An `Rules` instance is created via the `Client::analytics().rules()` method.
4+
5+
use super::{Client, Error};
6+
use std::sync::Arc;
7+
use typesense_codegen::{
8+
apis::{analytics_api, configuration},
9+
models,
10+
};
11+
12+
/// Provides methods for interacting with a collection of analytics rules.
13+
///
14+
/// This struct is created by calling `client.analytics().rules()`.
15+
pub struct Rules<'a> {
16+
pub(super) client: &'a Client,
17+
}
18+
19+
impl<'a> Rules<'a> {
20+
/// Creates a new `Rules` instance.
21+
pub(super) fn new(client: &'a Client) -> Self {
22+
Self { client }
23+
}
24+
25+
/// Creates a new analytics rule.
26+
///
27+
/// # Arguments
28+
/// * `schema` - An `AnalyticsRuleSchema` object describing the rule to be created.
29+
pub async fn create(
30+
&self,
31+
schema: models::AnalyticsRuleSchema,
32+
) -> Result<models::AnalyticsRuleSchema, Error<analytics_api::CreateAnalyticsRuleError>> {
33+
let params = analytics_api::CreateAnalyticsRuleParams {
34+
analytics_rule_schema: schema,
35+
};
36+
self.client
37+
.execute(|config: Arc<configuration::Configuration>| {
38+
let params_for_move = params.clone();
39+
async move { analytics_api::create_analytics_rule(&config, params_for_move).await }
40+
})
41+
.await
42+
}
43+
44+
/// Creates or updates an analytics rule with the given name.
45+
///
46+
/// # Arguments
47+
/// * `rule_name` - The name of the analytics rule to create or update.
48+
/// * `schema` - An `AnalyticsRuleUpsertSchema` object with the rule's parameters.
49+
pub async fn upsert(
50+
&self,
51+
rule_name: &str,
52+
schema: models::AnalyticsRuleUpsertSchema,
53+
) -> Result<models::AnalyticsRuleSchema, Error<analytics_api::UpsertAnalyticsRuleError>> {
54+
let params = analytics_api::UpsertAnalyticsRuleParams {
55+
rule_name: rule_name.to_string(),
56+
analytics_rule_upsert_schema: schema,
57+
};
58+
self.client
59+
.execute(|config: Arc<configuration::Configuration>| {
60+
let params_for_move = params.clone();
61+
async move { analytics_api::upsert_analytics_rule(&config, params_for_move).await }
62+
})
63+
.await
64+
}
65+
66+
/// Retrieves the details of all analytics rules.
67+
pub async fn retrieve(
68+
&self,
69+
) -> Result<
70+
models::AnalyticsRulesRetrieveSchema,
71+
Error<analytics_api::RetrieveAnalyticsRulesError>,
72+
> {
73+
self.client
74+
.execute(|config: Arc<configuration::Configuration>| async move {
75+
analytics_api::retrieve_analytics_rules(&config).await
76+
})
77+
.await
78+
}
79+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
//! Provides access to API endpoints for a single document within a Typesense collection.
2+
//!
3+
//! An instance of `Document` is scoped to a specific document and is created
4+
//! via a parent `Collection` struct, for example:
5+
//! `client.collection("collection_name").document("document_id")`
6+
7+
use super::{Client, Error};
8+
use std::sync::Arc;
9+
use typesense_codegen::apis::{configuration, documents_api};
10+
11+
/// Provides methods for interacting with a single document within a specific Typesense collection.
12+
///
13+
/// This struct is created by calling a method like `collection.document("document_id")`.
14+
pub struct Document<'a> {
15+
pub(super) client: &'a Client,
16+
pub(super) collection_name: &'a str,
17+
pub(super) document_id: &'a str,
18+
}
19+
20+
impl<'a> Document<'a> {
21+
/// Creates a new `Document` instance for a specific document ID.
22+
/// This is intended for internal use by the parent `Documents` struct.
23+
pub(super) fn new(client: &'a Client, collection_name: &'a str, document_id: &'a str) -> Self {
24+
Self {
25+
client,
26+
collection_name,
27+
document_id,
28+
}
29+
}
30+
31+
/// Fetches this individual document from the collection.
32+
pub async fn get(&self) -> Result<serde_json::Value, Error<documents_api::GetDocumentError>> {
33+
let params = documents_api::GetDocumentParams {
34+
collection_name: self.collection_name.to_string(),
35+
document_id: self.document_id.to_string(),
36+
};
37+
38+
self.client
39+
.execute(|config: Arc<configuration::Configuration>| {
40+
let params_for_move = params.clone();
41+
async move { documents_api::get_document(&config, params_for_move).await }
42+
})
43+
.await
44+
}
45+
46+
/// Updates this individual document. The update can be partial.
47+
///
48+
/// # Arguments
49+
/// * `document` - A `serde_json::Value` containing the fields to update.
50+
pub async fn update(
51+
&self,
52+
document: serde_json::Value,
53+
) -> Result<serde_json::Value, Error<documents_api::UpdateDocumentError>> {
54+
let params = documents_api::UpdateDocumentParams {
55+
collection_name: self.collection_name.to_string(),
56+
document_id: self.document_id.to_string(),
57+
body: document,
58+
dirty_values: None,
59+
};
60+
self.client
61+
.execute(|config: Arc<configuration::Configuration>| {
62+
let params_for_move = params.clone();
63+
async move { documents_api::update_document(&config, params_for_move).await }
64+
})
65+
.await
66+
}
67+
68+
/// Deletes this individual document from the collection.
69+
pub async fn delete(
70+
&self,
71+
) -> Result<serde_json::Value, Error<documents_api::DeleteDocumentError>> {
72+
let params = documents_api::DeleteDocumentParams {
73+
collection_name: self.collection_name.to_string(),
74+
document_id: self.document_id.to_string(),
75+
};
76+
self.client
77+
.execute(|config: Arc<configuration::Configuration>| {
78+
let params_for_move = params.clone();
79+
async move { documents_api::delete_document(&config, params_for_move).await }
80+
})
81+
.await
82+
}
83+
}

0 commit comments

Comments
 (0)