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

Issue #3365 - adding response metadata into enhanced response objects #3413

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
6 changes: 6 additions & 0 deletions .changes/next-release/feature-AmazonDynamoDB-24a4d1f.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "feature",
"category": "Amazon DynamoDB",
"contributor": "petercipov",
"description": "Pass DynamoDbResponseMetadata via ItemEnhancedResponse objects to retrieve requestId using enhanced client"
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ public DeleteItemEnhancedResponse<T> transformResponse(DeleteItemResponse respon
.attributes(attributes)
.consumedCapacity(response.consumedCapacity())
.itemCollectionMetrics(response.itemCollectionMetrics())
.responseMetadata(response.responseMetadata())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ public PutItemEnhancedResponse<T> transformResponse(PutItemResponse response,
.attributes(attributes)
.consumedCapacity(response.consumedCapacity())
.itemCollectionMetrics(response.itemCollectionMetrics())
.responseMetadata(response.responseMetadata())
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ public UpdateItemEnhancedResponse<T> transformResponse(UpdateItemResponse respon
.attributes(attributes)
.consumedCapacity(response.consumedCapacity())
.itemCollectionMetrics(response.itemCollectionMetrics())
.responseMetadata(response.responseMetadata())
.build();
} catch (RuntimeException e) {
// With a partial update it's possible to update the record into a state that the mapper can no longer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.services.dynamodb.model.ConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.DeleteItemResponse;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbResponseMetadata;
import software.amazon.awssdk.services.dynamodb.model.ItemCollectionMetrics;

/**
Expand All @@ -38,11 +39,13 @@ public final class DeleteItemEnhancedResponse<T> {
private final T attributes;
private final ConsumedCapacity consumedCapacity;
private final ItemCollectionMetrics itemCollectionMetrics;
private final DynamoDbResponseMetadata responseMetadata;

private DeleteItemEnhancedResponse(Builder<T> builder) {
this.attributes = builder.attributes;
this.consumedCapacity = builder.consumedCapacity;
this.itemCollectionMetrics = builder.itemCollectionMetrics;
this.responseMetadata = builder.responseMetadata;
}

/**
Expand Down Expand Up @@ -70,6 +73,14 @@ public ItemCollectionMetrics itemCollectionMetrics() {
return itemCollectionMetrics;
}

/**
* The response metadata, f.e. requestId
* @see DeleteItemResponse#responseMetadata() ()
*/
public DynamoDbResponseMetadata responseMetadata() {
return responseMetadata;
}

public static <T> Builder<T> builder(Class<? extends T> clzz) {
return new Builder<>();
}
Expand All @@ -86,14 +97,16 @@ public boolean equals(Object o) {
DeleteItemEnhancedResponse<?> that = (DeleteItemEnhancedResponse<?>) o;
return Objects.equals(attributes, that.attributes)
&& Objects.equals(consumedCapacity, that.consumedCapacity)
&& Objects.equals(itemCollectionMetrics, that.itemCollectionMetrics);
&& Objects.equals(itemCollectionMetrics, that.itemCollectionMetrics)
&& Objects.equals(responseMetadata, that.responseMetadata);
}

@Override
public int hashCode() {
int result = Objects.hashCode(attributes);
result = 31 * result + Objects.hashCode(consumedCapacity);
result = 31 * result + Objects.hashCode(itemCollectionMetrics);
result = 31 * result + Objects.hashCode(responseMetadata);
return result;
}

Expand All @@ -102,6 +115,8 @@ public static final class Builder<T> {
private T attributes;
private ConsumedCapacity consumedCapacity;
private ItemCollectionMetrics itemCollectionMetrics;
private DynamoDbResponseMetadata responseMetadata;


public Builder<T> attributes(T attributes) {
this.attributes = attributes;
Expand All @@ -118,6 +133,11 @@ public Builder<T> itemCollectionMetrics(ItemCollectionMetrics itemCollectionMetr
return this;
}

public Builder<T> responseMetadata(DynamoDbResponseMetadata responseMetadata) {
this.responseMetadata = responseMetadata;
return this;
}

public DeleteItemEnhancedResponse<T> build() {
return new DeleteItemEnhancedResponse<>(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.services.dynamodb.model.ConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbResponseMetadata;
import software.amazon.awssdk.services.dynamodb.model.ItemCollectionMetrics;
import software.amazon.awssdk.services.dynamodb.model.PutItemResponse;

Expand All @@ -37,11 +38,13 @@ public final class PutItemEnhancedResponse<T> {
private final T attributes;
private final ConsumedCapacity consumedCapacity;
private final ItemCollectionMetrics itemCollectionMetrics;
private final DynamoDbResponseMetadata responseMetadata;

private PutItemEnhancedResponse(Builder<T> builder) {
this.attributes = builder.attributes;
this.consumedCapacity = builder.consumedCapacity;
this.itemCollectionMetrics = builder.itemCollectionMetrics;
this.responseMetadata = builder.responseMetadata;
}

/**
Expand Down Expand Up @@ -69,6 +72,14 @@ public ItemCollectionMetrics itemCollectionMetrics() {
return itemCollectionMetrics;
}

/**
* The response metadata, f.e. requestId
* @see PutItemResponse#responseMetadata() ()
*/
public DynamoDbResponseMetadata responseMetadata() {
return responseMetadata;
}

public static <T> Builder<T> builder(Class<? extends T> clzz) {
return new Builder<>();
}
Expand All @@ -85,14 +96,16 @@ public boolean equals(Object o) {
PutItemEnhancedResponse<?> that = (PutItemEnhancedResponse<?>) o;
return Objects.equals(attributes, that.attributes)
&& Objects.equals(consumedCapacity, that.consumedCapacity)
&& Objects.equals(itemCollectionMetrics, that.itemCollectionMetrics);
&& Objects.equals(itemCollectionMetrics, that.itemCollectionMetrics)
&& Objects.equals(responseMetadata, that.responseMetadata);
}

@Override
public int hashCode() {
int result = Objects.hashCode(attributes);
result = 31 * result + Objects.hashCode(consumedCapacity);
result = 31 * result + Objects.hashCode(itemCollectionMetrics);
result = 31 * result + Objects.hashCode(responseMetadata);
return result;
}

Expand All @@ -101,6 +114,7 @@ public static final class Builder<T> {
private T attributes;
private ConsumedCapacity consumedCapacity;
private ItemCollectionMetrics itemCollectionMetrics;
private DynamoDbResponseMetadata responseMetadata;

public Builder<T> attributes(T attributes) {
this.attributes = attributes;
Expand All @@ -117,6 +131,11 @@ public Builder<T> itemCollectionMetrics(ItemCollectionMetrics itemCollectionMetr
return this;
}

public Builder<T> responseMetadata(DynamoDbResponseMetadata responseMetadata) {
this.responseMetadata = responseMetadata;
return this;
}

public PutItemEnhancedResponse<T> build() {
return new PutItemEnhancedResponse<>(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbAsyncTable;
import software.amazon.awssdk.enhanced.dynamodb.DynamoDbTable;
import software.amazon.awssdk.services.dynamodb.model.ConsumedCapacity;
import software.amazon.awssdk.services.dynamodb.model.DynamoDbResponseMetadata;
import software.amazon.awssdk.services.dynamodb.model.ItemCollectionMetrics;
import software.amazon.awssdk.services.dynamodb.model.UpdateItemResponse;

Expand All @@ -38,11 +39,13 @@ public final class UpdateItemEnhancedResponse<T> {
private final T attributes;
private final ConsumedCapacity consumedCapacity;
private final ItemCollectionMetrics itemCollectionMetrics;
private final DynamoDbResponseMetadata responseMetadata;

private UpdateItemEnhancedResponse(Builder<T> builder) {
this.attributes = builder.attributes;
this.consumedCapacity = builder.consumedCapacity;
this.itemCollectionMetrics = builder.itemCollectionMetrics;
this.responseMetadata = builder.responseMetadata;
}

/**
Expand Down Expand Up @@ -70,6 +73,14 @@ public ItemCollectionMetrics itemCollectionMetrics() {
return itemCollectionMetrics;
}

/**
* The response metadata, f.e. requestId
* @see UpdateItemResponse#responseMetadata() ()
*/
public DynamoDbResponseMetadata responseMetadata() {
return responseMetadata;
}

public static <T> Builder<T> builder(Class<? extends T> clzz) {
return new Builder<>();
}
Expand All @@ -86,14 +97,16 @@ public boolean equals(Object o) {
UpdateItemEnhancedResponse<?> that = (UpdateItemEnhancedResponse<?>) o;
return Objects.equals(attributes, that.attributes)
&& Objects.equals(consumedCapacity, that.consumedCapacity)
&& Objects.equals(itemCollectionMetrics, that.itemCollectionMetrics);
&& Objects.equals(itemCollectionMetrics, that.itemCollectionMetrics)
&& Objects.equals(responseMetadata, that.responseMetadata);
}

@Override
public int hashCode() {
int result = Objects.hashCode(attributes);
result = 31 * result + Objects.hashCode(consumedCapacity);
result = 31 * result + Objects.hashCode(itemCollectionMetrics);
result = 31 * result + Objects.hashCode(responseMetadata);
return result;
}

Expand All @@ -102,6 +115,7 @@ public static final class Builder<T> {
private T attributes;
private ConsumedCapacity consumedCapacity;
private ItemCollectionMetrics itemCollectionMetrics;
private DynamoDbResponseMetadata responseMetadata;

public Builder<T> attributes(T attributes) {
this.attributes = attributes;
Expand All @@ -118,6 +132,11 @@ public Builder<T> itemCollectionMetrics(ItemCollectionMetrics itemCollectionMetr
return this;
}

public Builder<T> responseMetadata(DynamoDbResponseMetadata responseMetadata) {
this.responseMetadata = responseMetadata;
return this;
}

public UpdateItemEnhancedResponse<T> build() {
return new UpdateItemEnhancedResponse<>(this);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static software.amazon.awssdk.enhanced.dynamodb.internal.AttributeValues.stringValue;
import static software.amazon.awssdk.enhanced.dynamodb.mapper.StaticAttributeTags.primaryPartitionKey;
Expand All @@ -40,9 +41,12 @@
import software.amazon.awssdk.enhanced.dynamodb.internal.client.DefaultDynamoDbEnhancedAsyncClient;
import software.amazon.awssdk.enhanced.dynamodb.mapper.StaticTableSchema;
import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.DeleteItemEnhancedResponse;
import software.amazon.awssdk.enhanced.dynamodb.model.EnhancedGlobalSecondaryIndex;
import software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.PutItemEnhancedResponse;
import software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedRequest;
import software.amazon.awssdk.enhanced.dynamodb.model.UpdateItemEnhancedResponse;
import software.amazon.awssdk.services.dynamodb.model.ConditionalCheckFailedException;
import software.amazon.awssdk.services.dynamodb.model.DeleteTableRequest;
import software.amazon.awssdk.services.dynamodb.model.ProjectionType;
Expand Down Expand Up @@ -256,6 +260,19 @@ public void putThenGetItemUsingKey() {
assertThat(result, is(record));
}

@Test
public void putReturnsRequestId() {
Record record = new Record()
.setId("id-value")
.setSort("sort-value")
.setAttribute("one")
.setAttribute2("two")
.setAttribute3("three");

PutItemEnhancedResponse<Record> putResponse = mappedTable.putItemWithResponse(r -> r.item(record)).join();
assertThat(putResponse.responseMetadata().requestId(), notNullValue());
}

@Test
public void putThenGetItemUsingKeyItem() {
Record record = new Record()
Expand Down Expand Up @@ -401,6 +418,13 @@ public void putWithConditionThatFails() {
.join();
}

@Test
public void deleteReturnsRequestId() {
DeleteItemEnhancedResponse<Record> deleteResponse =
mappedTable.deleteItemWithResponse(r -> r.key(k -> k.partitionValue("id-value").sortValue("sort-value"))).join();
assertThat(deleteResponse.responseMetadata().requestId(), notNullValue());
}

@Test
public void deleteNonExistentItem() {
Record result = mappedTable.deleteItem(r -> r.key(k -> k.partitionValue("id-value").sortValue("sort-value"))).join();
Expand Down Expand Up @@ -463,6 +487,20 @@ public void deleteWithConditionThatFails() {
.build()).join();
}

@Test
public void updateReturnsRequestId() {
Record record = new Record()
.setId("id-value")
.setSort("sort-value")
.setAttribute("one")
.setAttribute2("two")
.setAttribute3("three");

UpdateItemEnhancedResponse<Record> updateResponse =
mappedTable.updateItemWithResponse(r -> r.item(record)).join();
assertThat(updateResponse.responseMetadata().requestId(), notNullValue());
}

@Test
public void updateOverwriteCompleteRecord_usingShortcutForm() {
Record record = new Record()
Expand Down
Loading