Skip to content
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

How to add headers to ContainerClient requests? #2254

Open
kate-goldenring opened this issue Feb 28, 2025 · 3 comments
Open

How to add headers to ContainerClient requests? #2254

kate-goldenring opened this issue Feb 28, 2025 · 3 comments
Assignees
Labels
Cosmos The azure_cosmos crate customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close.

Comments

@kate-goldenring
Copy link

I am looking to implement compare-and-swap functionality with the new v0.22.0 CosmosDB SDK. With the previous v0.21.0 SDK (example), i was able to use the if_match_condition(azure_core::request_options::IfMatchCondition::Match(etag)) method on the DocumentClient. That client has been replaced by the ContainerClient in the v0.22.0 SDK, which is missing many methods for adding headers to requests. What is the best way to add a header (such as the "if-match" one) to an individual request issued by the ContainerClient? I created a policy for the cases I wanted to add headers for all requests; however, I am not sure how to add headers on individual requests like i used to be able to with the predefined methods.

@github-project-automation github-project-automation bot moved this to Untriaged in Azure SDK Rust Feb 28, 2025
@github-actions github-actions bot added customer-reported Issues that are reported by GitHub users external to the Azure organization. needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. question The issue doesn't require a change to the product in order to be resolved. Most issues start as that labels Feb 28, 2025
@RickWinter RickWinter added the Cosmos The azure_cosmos crate label Mar 4, 2025
@github-actions github-actions bot removed the needs-triage Workflow: This is a new issue that needs to be triaged to the appropriate team. label Mar 4, 2025
@analogrelay
Copy link
Member

A number of things from the previous unofficial versions of the client haven't quite made it over yet, and this is one of them. We'll be adding specific options as we continue, but for now, using a Policy is your best bet. As you noticed, the policy runs on every request, but you can "configure" it using the Context object.

Every method takes an "Options" value as it's final parameter, and every ...Options type has a method_options of type ClientMethodOptions. That type allows you to specify a Context value, which will flow down through all the policies. The Context value you provide in the options for the individual method is the one that ends up being passed to your Policy's send method. The Context is a generic key-value store that is keyed off of the type of the value you pass in, so you could do something like this:

pub struct IfMatch(String);

let context = Context::new().with_value(IfMatch(etag));
container_client.upsert_item(..., ItemOptions {
    method_options: ClientMethodOptions {
        context,
        ..Default::default()
    },
    ..Default::default()
}).await;

And then in your policy:

if let Some(IfMatch(if_match)) = ctx.value::<IfMatch>() {
    // Add your header here
}

This is definitely a bit clunky and not the long-term plan, but if you'd like to move to 0.22.0 and were using this feature from the previous versions, it's a way to get unblocked!

@kate-goldenring
Copy link
Author

Thank you @analogrelay! This unblocks me. I appreciate the support.

@analogrelay analogrelay added the issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close. label Mar 6, 2025
Copy link

github-actions bot commented Mar 6, 2025

Hi @kate-goldenring. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

@RickWinter RickWinter added feature-request This issue requires a new behavior in the product in order be resolved. and removed question The issue doesn't require a change to the product in order to be resolved. Most issues start as that feature-request This issue requires a new behavior in the product in order be resolved. labels Mar 6, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Cosmos The azure_cosmos crate customer-reported Issues that are reported by GitHub users external to the Azure organization. issue-addressed Workflow: The Azure SDK team believes it to be addressed and ready to close.
Projects
Status: Untriaged
Development

No branches or pull requests

3 participants