cleancoder
cleancoder

Reputation: 172

How to access HTTP Status Code for individual update request contained in a batch request using OutboundDeliveryV2ServiceBatch?

We have a requirement where we have to update multiple OutbDeliverItems. For efficiency reasons, we opted for a OData batch request.

Initially we were using BatchRequestBuilder to build the batch request and upon execution of BatchRequest.execute(), we were able to get a BatchResult object. Then we were able to call BatchResult.get() to get a List<BatchResultPart>. Then we were able to iterate over that list and were able to call BatchResultPart.getHttpStatusCode() to get the HTTP Status code for individual update requests. Below is the code snippet for it (omitting exception handling and keeping it simple):

BatchRequest batchRequest = // Generate batch request;

BatchResult batchResult = batchRequest.execute(// Some destination);

for (BatchResultPart resultForLineItem : batchResult.get()) {
    if (resultForLineItem.getHttpStatusCode() != Response.Status.ACCEPTED.getStatusCode()) {
        // Do something
    }
}

The requirement is such that we have to check the HTTP Status code for each item and the do some business logic.

But now we have decided to use OutboundDeliveryV2ServiceBatch for updating the items. The new code snippet looks like this (omitting exception handling and keeping it simple):

OutboundDeliveryV2ServiceBatchChangeSet changeSet = outboundDeliveryService.beginChangeSet();

items.forEach(changeSet::updateOutbDeliveryItem); // items is a List<OutbDeliveryItem>

BatchResponse batchResponse = changeSet.endChangeSet().execute(destination);

The problem is that batchResponse above provides just single method get(index) which returns a io.vavr.control.Try<BatchResponseChangeSet>. On this object we can call isSuccess(), a/t this blog post. But this is for the whole change set and not for individual updates within the change set. Further there are no methods on the BatchResponseChangeSet which can help us in accessing the response of the individual updates. There is a method BatchResponseChangeSet.getCreatedEntities(), which unfortunately is of no use to us, since it does not provide any info about the status of the individual update for that item.

Please point us to the right direction on how the functionality that we were able to achieve by using BatchRequestBuilder can be achieved by using OutboundDeliveryV2ServiceBatch.

Thank you.

Upvotes: 1

Views: 366

Answers (1)

Emdee
Emdee

Reputation: 1701

Quoting the official OData V2 spec on batch processing:

All operations in a ChangeSet represent a single change unit so the server must successfully process and apply all the requests in the ChangeSet or else apply none of the requests in the ChangeSet.

According to that, it appears to be against the spec that individual operations within one change set have different "results" (in the sense of successful or not). That is why the API of the SAP Cloud SDK allows you to check for the result of the whole change set.

Either all individual operations in the same change set are successful or none, but nothing else.

Following that conclusion, concerning getCreatedEntities we can derive that it allows you to access all created/updated entities within the same change set.

Addition:

Consider putting each update operation in its own, individual change set, if that suits your business requirements. Then, you would be able to figure out the result of each change set (and thereby of each corresponding update operation). Note that using this approach you will not leverage the concept of a change set in that sense that it ensures you that either all or none update operations are successful.

Upvotes: 2

Related Questions