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

Conversation

emily-emily
Copy link
Contributor

Problem

The SDK currently does not support adding a custom header for requests, which is necessary if we want to include API versions.

Solution

An API version number is added to the justfile and propagated throughout the sdk, to code generation and to PineconeClient to be sent with requests.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update
  • Infrastructure change (CI configs, etc)
  • Non-code change (docs, etc)
  • None of the above: (explain here)

Test Plan

All existing test cases pass.

@emily-emily emily-emily self-assigned this Jul 24, 2024
@emily-emily emily-emily requested a review from ericapywang July 24, 2024 22:15
@emily-emily emily-emily mentioned this pull request Jul 25, 2024
7 tasks
let client = reqwest::Client::builder()
.default_headers(headers)
.build()
.unwrap_or(reqwest::Client::new());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we able to do this once when the client is initially created? I believe this function is called every time we make a call to an OpenAPI endpoint, so probably better to not create the additional headers map every single time if possible.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we fail to build the client with headers it should error instead of silently defaulting to basic client

@emily-emily emily-emily marked this pull request as ready for review July 25, 2024 14:41
Copy link
Contributor

@ssmith-pc ssmith-pc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a few quick comments, will review in more depth later today

Comment on lines 98 to 104
// create reqwest headers
let mut headers = reqwest::header::HeaderMap::new();
for (k, v) in additional_headers.iter() {
let key = reqwest::header::HeaderName::from_bytes(k.as_bytes()).unwrap();
let value = reqwest::header::HeaderValue::from_str(v).unwrap();
headers.insert(key, value);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit heavy. There is some chatter about supporting HashMap -> HeaderMap conversions a bit more naturally. Refs: seanmonstar/reqwest#555 and hyperium/http#326.

Can you see if you can work out a better way to do this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated it to use map rather than iterating and appending each user-defined header, would this be a better approach?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about this:

        let headers: reqwest::header::HeaderMap =
            (&additional_headers)
                .try_into()
                .map_err(|_| PineconeError::InvalidHeadersError {
                    message: "Provided headers are not valid".to_string(),
                })?;

let client = reqwest::Client::builder()
.default_headers(headers)
.build()
.unwrap_or(reqwest::Client::new());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think if we fail to build the client with headers it should error instead of silently defaulting to basic client

Comment on lines 8 to 15
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());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We might want to update to use a CLI framework, but don't need to do in this PR

Copy link
Contributor

@austin-denoble austin-denoble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks for getting the headers added in, etc.

justfile Outdated

# Generate all OpenAPI and protobuf code
build-client: build-apis build-openapi build-proto
echo "/// Pinecone API version\npub const API_VERSION: &str = \"{{api_version}}\";" > pinecone_sdk/src/version.rs
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if the command to generate the version.rs file should be it's own just command. 🤔

I'm not sure, technically we want to make sure this is populated every time so maybe moving into it's own action and calling for each build command might be good?

})?;

// add X-Pinecone-Api-Version header if not present
if !headers.contains_key(PINECONE_API_VERSION_KEY) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, I appreciate allowing overrides of this via additional_headers. 👍

Copy link
Contributor

@austin-denoble austin-denoble left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, LGTM

@emily-emily emily-emily merged commit d01d51a into main Jul 25, 2024
1 check passed
@emily-emily emily-emily deleted the emily/version-and-headers branch July 25, 2024 21:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants