Skip to content

Commit

Permalink
Fix issues discovered in e2e tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rylev committed Jul 30, 2021
1 parent fd6e71a commit d8ce878
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
23 changes: 16 additions & 7 deletions sdk/cosmos/src/clients/collection_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use crate::clients::*;
use crate::operations::*;
use crate::requests;
use crate::resources::ResourceType;
use crate::CosmosEntity;
use crate::ReadonlyString;
use azure_core::PipelineContext;
use azure_core::{pipeline::Pipeline, Context, HttpClient};
use azure_core::{pipeline::Pipeline, Context, HttpClient, Request};
use serde::Serialize;

/// A client for Cosmos collection resources.
Expand Down Expand Up @@ -63,16 +64,14 @@ impl CollectionClient {
}

/// create a document in a collection
pub async fn create_document<D: Serialize>(
pub async fn create_document<'a, D: Serialize + CosmosEntity<'a>>(
&self,
ctx: Context,
document: &D,
document: &'a D,
options: CreateDocumentOptions<'_>,
) -> Result<CreateDocumentResponse, crate::Error> {
let mut request = self
.cosmos_client()
.prepare_request_pipeline("collection", http::Method::POST);
let mut pipeline_context = PipelineContext::new(ctx, ResourceType::Collections.into());
let mut request = self.prepare_request_pipeline(http::Method::POST);
let mut pipeline_context = PipelineContext::new(ctx, ResourceType::Documents.into());

options.decorate_request(&mut request, document)?;
let response = self
Expand Down Expand Up @@ -162,4 +161,14 @@ impl CollectionClient {
pub(crate) fn pipeline(&self) -> &Pipeline<CosmosContext> {
self.cosmos_client().pipeline()
}

fn prepare_request_pipeline(&self, http_method: http::Method) -> Request {
let path = &format!(
"dbs/{}/colls/{}",
self.database_client().database_name(),
self.collection_name()
);
self.cosmos_client()
.prepare_request_pipeline(&path, http_method)
}
}
4 changes: 2 additions & 2 deletions sdk/cosmos/src/operations/create_document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ impl<'a> CreateDocumentOptions<'a> {
document: &'b DOC,
) -> Result<(), crate::Error>
where
DOC: Serialize,
DOC: Serialize + CosmosEntity<'b>,
{
let serialized = serde_json::to_string(document)?;
let partition_key = self
.partition_key
.clone()
.unwrap_or_else(|| serialize_partition_key(document).unwrap());
.unwrap_or_else(|| serialize_partition_key(&document.partition_key()).unwrap());

add_as_partition_key_header_serialized2(&partition_key, req);
azure_core::headers::add_optional_header2(&self.if_match_condition, req)?;
Expand Down

0 comments on commit d8ce878

Please sign in to comment.