Skip to content

Add API version and custom headers #41

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jul 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion codegen/build-oas.sh
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
#!/bin/bash

version=$1

if [ -z "$version" ]; then
echo "Version is required"
exit 1
fi

pushd codegen/apis
just build
popd

outdir="openapi"

docker run --rm -v $(pwd):/workspace openapitools/openapi-generator-cli:v7.6.0 generate \
--input-spec /workspace/codegen/apis/_build/2024-07/control_2024-07.oas.yaml \
--input-spec /workspace/codegen/apis/_build/$version/control_$version.oas.yaml \
--generator-name rust \
--output /workspace/$outdir \
--additional-properties "packageVersion=0.0.1"
9 changes: 8 additions & 1 deletion codegen/build-proto.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
#!/bin/bash

version=$1

SCRIPT_DIR=$(dirname "$(realpath "$0")")
outdir="protos"

if [ -z "$version" ]; then
echo "Version is required"
exit 1
fi

OUT_DIR=$SCRIPT_DIR/../$outdir

pushd $SCRIPT_DIR/apis
just build
popd

pushd $SCRIPT_DIR/proto_build
cargo run -- $OUT_DIR
cargo run -- $OUT_DIR $version
popd
10 changes: 7 additions & 3 deletions codegen/proto_build/src/main.rs
Original file line number Diff line number Diff line change
@@ -5,14 +5,18 @@ fn main() -> Result<(), Box<dyn Error>> {
let args: Vec<String> = std::env::args().collect();

let out_dir: &str;
if args.len() > 1 {
let version: &str;
if args.len() == 3 {
out_dir = &args[1];
version = &args[2];
println!("OUT_DIR: {:?}", out_dir);
println!("version: {:?}", version);
} else {
return Err("missing out_dir argument".into());
return Err("Required 2 arguments: out_dir, version".into());
}

let proto_path: &Path = "../apis/_build/2024-07/data_2024-07.proto".as_ref();
let proto_path = format!("../apis/_build/{version}/data_{version}.proto");
let proto_path: &Path = proto_path.as_ref();

// directory the main .proto file resides in
let proto_dir = proto_path
26 changes: 21 additions & 5 deletions justfile
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
build-openapi:
./codegen/build-oas.sh
cargo build -p openapi
api_version := "2024-07"

build-proto:
./codegen/build-proto.sh
# Generate version file
generate-version:
echo "/// Pinecone API version\npub const API_VERSION: &str = \"{{api_version}}\";" > pinecone_sdk/src/version.rs

# Build the OpenAPI and Protobuf definitions in `codegen/apis`
build-apis:
cd codegen/apis && just build

# Generate the control plane OpenAPI code based on the yaml files in `codegen/apis/_build`
build-openapi: build-apis generate-version
./codegen/build-oas.sh {{api_version}}

# Generate the data plane protobuf code based on the yaml files in `codegen/apis/_build`
build-proto: build-apis generate-version
./codegen/build-proto.sh {{api_version}}

# Generate all OpenAPI and protobuf code
build-client: build-apis generate-version
./codegen/build-oas.sh {{api_version}}
./codegen/build-proto.sh {{api_version}}
29 changes: 0 additions & 29 deletions pinecone_sdk/src/config.rs

This file was deleted.

6 changes: 3 additions & 3 deletions pinecone_sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -6,11 +6,11 @@
#![warn(missing_docs)]

/// Defines configurations for the Pinecone SDK.
pub mod config;

/// Defines the main entrypoint of the Pinecone SDK.
pub mod pinecone;

/// Utility modules.
pub mod utils;

/// Version information.
pub mod version;
30 changes: 14 additions & 16 deletions pinecone_sdk/src/pinecone/control.rs
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ impl PineconeClient {
};

// make openAPI call
let res = manage_indexes_api::create_index(&self.openapi_config(), create_index_request)
let res = manage_indexes_api::create_index(&self.openapi_config, create_index_request)
.await
.map_err(|e| PineconeError::from(e))?;

@@ -191,7 +191,7 @@ impl PineconeClient {
};

// make openAPI call
let res = manage_indexes_api::create_index(&self.openapi_config(), create_index_request)
let res = manage_indexes_api::create_index(&self.openapi_config, create_index_request)
.await
.map_err(|e| PineconeError::from(e))?;

@@ -244,7 +244,7 @@ impl PineconeClient {

// Gets ready status of an index
async fn is_ready(&self, name: &str) -> bool {
let res = manage_indexes_api::describe_index(&self.openapi_config(), name).await;
let res = manage_indexes_api::describe_index(&self.openapi_config, name).await;
match res {
Ok(index) => index.status.ready,
Err(_) => false,
@@ -275,7 +275,7 @@ impl PineconeClient {
/// ```
pub async fn describe_index(&self, name: &str) -> Result<IndexModel, PineconeError> {
// make openAPI call
let res = manage_indexes_api::describe_index(&self.openapi_config(), name)
let res = manage_indexes_api::describe_index(&self.openapi_config, name)
.await
.map_err(|e| PineconeError::from(e))?;

@@ -306,7 +306,7 @@ impl PineconeClient {
/// ```
pub async fn list_indexes(&self) -> Result<IndexList, PineconeError> {
// make openAPI call
let res = manage_indexes_api::list_indexes(&self.openapi_config())
let res = manage_indexes_api::list_indexes(&self.openapi_config)
.await
.map_err(|e| PineconeError::from(e))?;

@@ -356,7 +356,7 @@ impl PineconeClient {

// make openAPI call
let res = manage_indexes_api::configure_index(
&self.openapi_config(),
&self.openapi_config,
name,
configure_index_request,
)
@@ -390,7 +390,7 @@ impl PineconeClient {
/// ```
pub async fn delete_index(&self, name: &str) -> Result<(), PineconeError> {
// make openAPI call
let res = manage_indexes_api::delete_index(&self.openapi_config(), name)
let res = manage_indexes_api::delete_index(&self.openapi_config, name)
.await
.map_err(|e| PineconeError::from(e))?;

@@ -431,12 +431,10 @@ impl PineconeClient {
};

// make openAPI call
let res = manage_indexes_api::create_collection(
&self.openapi_config(),
create_collection_request,
)
.await
.map_err(|e| PineconeError::from(e))?;
let res =
manage_indexes_api::create_collection(&self.openapi_config, create_collection_request)
.await
.map_err(|e| PineconeError::from(e))?;

Ok(res)
}
@@ -464,7 +462,7 @@ impl PineconeClient {
/// # }
/// ```
pub async fn describe_collection(&self, name: &str) -> Result<CollectionModel, PineconeError> {
let res = manage_indexes_api::describe_collection(&self.openapi_config(), name)
let res = manage_indexes_api::describe_collection(&self.openapi_config, name)
.await
.map_err(|e| PineconeError::from(e))?;

@@ -494,7 +492,7 @@ impl PineconeClient {
/// ```
pub async fn list_collections(&self) -> Result<CollectionList, PineconeError> {
// make openAPI call
let res = manage_indexes_api::list_collections(&self.openapi_config())
let res = manage_indexes_api::list_collections(&self.openapi_config)
.await
.map_err(|e| PineconeError::from(e))?;

@@ -525,7 +523,7 @@ impl PineconeClient {
/// ```
pub async fn delete_collection(&self, name: &str) -> Result<(), PineconeError> {
// make openAPI call
let res = manage_indexes_api::delete_collection(&self.openapi_config(), name)
let res = manage_indexes_api::delete_collection(&self.openapi_config, name)
.await
.map_err(|e| PineconeError::from(e))?;

280 changes: 205 additions & 75 deletions pinecone_sdk/src/pinecone/mod.rs

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions pinecone_sdk/src/utils/user_agent.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use crate::config::Config;
use regex::Regex;

/// Normalizes the source tag.
@@ -20,10 +19,13 @@ fn build_source_tag(source_tag: &String) -> String {
}

/// Gets the user-agent string.
pub fn get_user_agent(config: &Config) -> String {
pub fn get_user_agent(source_tag: Option<&str>) -> String {
let mut user_agent = format!("lang=rust; pinecone-rust-client={}", "0.1.0");
if let Some(source_tag) = &config.source_tag {
user_agent.push_str(&format!("; source_tag={}", build_source_tag(source_tag)));
if let Some(st) = source_tag {
user_agent.push_str(&format!(
"; source_tag={}",
build_source_tag(&st.to_string())
));
}
return user_agent;
}
@@ -47,18 +49,16 @@ mod tests {

#[tokio::test]
async fn test_no_source_tag() {
let config = Config::new("api".to_string(), None);
assert_eq!(
get_user_agent(&config),
get_user_agent(None),
"lang=rust; pinecone-rust-client=0.1.0"
);
}

#[tokio::test]
async fn test_with_source_tag() {
let config = Config::new("api".to_string(), Some("Tag".to_string()));
assert_eq!(
get_user_agent(&config),
get_user_agent(Some("tag")),
"lang=rust; pinecone-rust-client=0.1.0; source_tag=tag"
);
}
2 changes: 2 additions & 0 deletions pinecone_sdk/src/version.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// Pinecone API version
pub const API_VERSION: &str = "2024-07";
30 changes: 24 additions & 6 deletions pinecone_sdk/tests/integration_test.rs
Original file line number Diff line number Diff line change
@@ -5,7 +5,7 @@ use pinecone_sdk::pinecone::data::{Kind, Metadata, Namespace, SparseValues, Valu
use pinecone_sdk::pinecone::PineconeClient;
use pinecone_sdk::utils::errors::PineconeError;
use rand::Rng;
use std::collections::BTreeMap;
use std::collections::{BTreeMap, HashMap};
use std::time::Duration;
use std::vec;

@@ -435,7 +435,7 @@ async fn test_list_collections() -> Result<(), PineconeError> {
}

#[tokio::test]
async fn test_delete_collection_err() -> Result<(), PineconeError> {
async fn test_delete_collection_invalid_collection() -> Result<(), PineconeError> {
let pinecone =
PineconeClient::new(None, None, None, None).expect("Failed to create Pinecone instance");

@@ -447,6 +447,27 @@ async fn test_delete_collection_err() -> Result<(), PineconeError> {
Ok(())
}

#[tokio::test]
async fn test_list_collections_invalid_api_version() -> Result<(), PineconeError> {
let headers: HashMap<String, String> = [(
pinecone_sdk::pinecone::PINECONE_API_VERSION_KEY.to_string(),
"invalid".to_string(),
)]
.iter()
.cloned()
.collect();

let pinecone = PineconeClient::new(None, None, Some(headers), None)
.expect("Failed to create Pinecone instance");

let _ = pinecone
.list_collections()
.await
.expect_err("Expected to fail listing collections due to invalid api version");

Ok(())
}

#[tokio::test]
async fn test_index() -> Result<(), PineconeError> {
let pinecone = PineconeClient::new(None, None, None, None).unwrap();
@@ -982,10 +1003,7 @@ async fn test_fetch_vectors() -> Result<(), PineconeError> {
std::thread::sleep(std::time::Duration::from_secs(5));

let fetch_response = index
.fetch(
&["1", "2"],
namespace,
)
.fetch(&["1", "2"], namespace)
.await
.expect("Failed to fetch vectors");


Unchanged files with check annotations Beta

// This file is @generated by prost-build.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct SparseValues {

Check warning on line 4 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct

Check warning on line 4 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct

Check warning on line 4 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct
#[prost(uint32, repeated, packed = "false", tag = "1")]
pub indices: ::prost::alloc::vec::Vec<u32>,

Check warning on line 6 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field

Check warning on line 6 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field

Check warning on line 6 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field
#[prost(float, repeated, packed = "false", tag = "2")]
pub values: ::prost::alloc::vec::Vec<f32>,

Check warning on line 8 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field

Check warning on line 8 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field

Check warning on line 8 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Vector {

Check warning on line 12 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct

Check warning on line 12 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct

Check warning on line 12 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct
/// This is the vector's unique id.
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
#[prost(float, repeated, packed = "false", tag = "2")]
pub values: ::prost::alloc::vec::Vec<f32>,
#[prost(message, optional, tag = "4")]
pub sparse_values: ::core::option::Option<SparseValues>,

Check warning on line 20 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field

Check warning on line 20 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field

Check warning on line 20 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field
/// This is the metadata included in the request.
#[prost(message, optional, tag = "3")]
pub metadata: ::core::option::Option<::prost_types::Struct>,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct ScoredVector {

Check warning on line 27 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct
/// This is the vector's unique id.
#[prost(string, tag = "1")]
pub id: ::prost::alloc::string::String,
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct RequestUnion {
#[prost(oneof = "request_union::RequestUnionInner", tags = "1, 2, 3")]
pub request_union_inner: ::core::option::Option<request_union::RequestUnionInner>,

Check warning on line 50 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field
}
/// Nested message and enum types in `RequestUnion`.
pub mod request_union {
#[prost(string, repeated, tag = "1")]
pub ids: ::prost::alloc::vec::Vec<::prost::alloc::string::String>,
#[prost(string, tag = "2")]
pub namespace: ::prost::alloc::string::String,

Check warning on line 116 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field
}
/// The response for the `fetch` operation.
#[allow(clippy::derive_partial_eq_without_eq)]
#[prost(string, optional, tag = "3")]
pub pagination_token: ::core::option::Option<::prost::alloc::string::String>,
#[prost(string, tag = "4")]
pub namespace: ::prost::alloc::string::String,

Check warning on line 146 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct field
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Pagination {

Check warning on line 150 in protos/_.rs

GitHub Actions / test (ubuntu-latest, stable)

missing documentation for a struct
#[prost(string, tag = "1")]
pub next: ::prost::alloc::string::String,
}
#[derive(Debug)]
pub struct Index {
/// The name of the index.
host: String,

Check warning on line 43 in pinecone_sdk/src/pinecone/data.rs

GitHub Actions / test (ubuntu-latest, stable)

field `host` is never read

Check warning on line 43 in pinecone_sdk/src/pinecone/data.rs

GitHub Actions / test (ubuntu-latest, stable)

field `host` is never read
connection: VectorServiceClient<InterceptedService<Channel, ApiKeyInterceptor>>,
}
filter,
include_values: include_values.unwrap_or(false),
include_metadata: include_metadata.unwrap_or(false),
queries: vec![],

Check warning on line 340 in pinecone_sdk/src/pinecone/data.rs

GitHub Actions / test (ubuntu-latest, stable)

use of deprecated field `pinecone::data::pb::QueryRequest::queries`

Check warning on line 340 in pinecone_sdk/src/pinecone/data.rs

GitHub Actions / test (ubuntu-latest, stable)

use of deprecated field `pinecone::data::pb::QueryRequest::queries`
vector: vec![],
sparse_vector: None,
};
filter,
include_values: include_values.unwrap_or(false),
include_metadata: include_metadata.unwrap_or(false),
queries: vec![],

Check warning on line 399 in pinecone_sdk/src/pinecone/data.rs

GitHub Actions / test (ubuntu-latest, stable)

use of deprecated field `pinecone::data::pb::QueryRequest::queries`

Check warning on line 399 in pinecone_sdk/src/pinecone/data.rs

GitHub Actions / test (ubuntu-latest, stable)

use of deprecated field `pinecone::data::pb::QueryRequest::queries`
vector,
sparse_vector,
};