bgmulinari / B1SLayer

A lightweight SAP Business One Service Layer client for .NET
MIT License
136 stars 47 forks source link

Batch requests and ETAG #54

Closed aldebra-admin closed 11 months ago

aldebra-admin commented 11 months ago

Hi, can ETAG for optimistic concurrency be used with SLBatchRequest? I tried with

SLBatchRequest request1 = new SLBatchRequest( HttpMethod.Patch, $"Orders({docEntry})", orderPatch1,  contentID: 1 );
SLBatchRequest request2 = new SLBatchRequest( HttpMethod.Patch, $"Orders({docEntry})", orderPatch2,  contentID: 2 );
SLBatchRequest request3 = ...;

setting

SLBatchRequest.WithHeader( "If-Match", orderEtag ); // previously read etag value

just for the 1st request or each request, but seems not working. Having not found any sample, I wonder if it is supported by Service Layers.

Thank you

bgmulinari commented 11 months ago

Hi, @aldebra-admin.

From my testing, I can confirm that Service Layer does not validate the ETag when in a batch request, like you mentioned.

I couldn't find any mention of the combined use of ETag and batch operations in the Service Layer User Manual, so I'm not sure if this is simply not supported or a bug.

My batch request with an invalid ETag for the PATCH:

POST https://192.168.18.2:50000/b1s/v1/$batch HTTP/1.1
Host: 192.168.18.2:50000
Cookie: B1SESSION=f638c792-6d0f-11ee-8000-0a0027000010; ROUTEID=.node1
Accept-Encoding: gzip, deflate
Content-Type: multipart/mixed; boundary="0d361824-c3c2-4d0b-91ca-6d6dd00f45c2"
Content-Length: 584

--0d361824-c3c2-4d0b-91ca-6d6dd00f45c2
Content-Type: multipart/mixed; boundary="changeset_64b2bd91-6da6-4de3-9bd7-3b01990ff396"

--changeset_64b2bd91-6da6-4de3-9bd7-3b01990ff396
Content-Type: application/http; msgtype=request
content-transfer-encoding: binary
Content-ID: 2

PATCH /b1s/v1/BusinessPartners('C00001') HTTP/1.1
Host: 192.168.18.2:50000
If-Match: W/"356A192B7913B04C54574D18C28D46E6395428AB"
Content-Type: application/json; charset=utf-8

{"CardName":"Test@@"}
--changeset_64b2bd91-6da6-4de3-9bd7-3b01990ff396--

--0d361824-c3c2-4d0b-91ca-6d6dd00f45c2--

The batch request response (successful):

HTTP/1.1 202 Accepted
Date: Tue, 17 Oct 2023 17:09:42 GMT
Server: Apache
DataServiceVersion: 3.0
Content-Type: multipart/mixed;boundary=batchresponse_jVO2kfgK-mLLX-O2B6-wQqu-kNas6eUt1hbJ
Vary: Accept-Encoding
Content-Length: 472

--batchresponse_jVO2kfgK-mLLX-O2B6-wQqu-kNas6eUt1hbJ
Content-Type: multipart/mixed; boundary=changesetresponse_jVO2kfgK-mLLX-O2B6-wQqu-kNas6eUt1hbJ

--changesetresponse_jVO2kfgK-mLLX-O2B6-wQqu-kNas6eUt1hbJ
Content-Type: application/http
Content-Transfer-Encoding: binary

HTTP/1.1 204 No Content
Content-ID: 2
DataServiceVersion: 3.0

--changesetresponse_jVO2kfgK-mLLX-O2B6-wQqu-kNas6eUt1hbJ--

--batchresponse_jVO2kfgK-mLLX-O2B6-wQqu-kNas6eUt1hbJ--

The response to the same PATCH request (with an invalid ETag) when not performed in a batch:

HTTP/1.1 412 Precondition Failed
Date: Tue, 17 Oct 2023 17:12:51 GMT
Server: Apache
DataServiceVersion: 3.0
Content-Type: application/json;charset=utf-8
Vary: Accept-Encoding
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Content-Length: 219

{
   "error" : {
      "code" : -2039,
      "message" : {
         "lang" : "en-us",
         "value" : "Another user or another operation modified data; to continue, open the window again (ODBC -2039)"
      }
   }
}
aldebra-admin commented 11 months ago

Thank you @bgmulinari, I had the same experience. Just to let you know, all started because seems that sometimes you have to split a single order update in more updates if you modify at the same time the order header, the adress extensions, the additional expenses and the line 'close status': so to mantain atomicity batch requests could be a good candidate, but you loose concurrency check. And I just wanted to 'save' an order 😄

On the other side, the 'replace collections' mechanism in batch requests seems to work.

bgmulinari commented 11 months ago

@aldebra-admin, as this seems to be an issue regarding the Service Layer itself and not B1SLayer, I recommend you asking a question in the official SAP Community page with the "SAP Business One Extensibility" tag. The experts there usually respond very quickly.

If you still need help regarding B1SLayer, just let me know.

aldebra-admin commented 11 months ago

@bgmulinari, thank you for the suggestion and your quick support.