/
cosmos_transactional_batch_response.go
89 lines (73 loc) · 2.77 KB
/
cosmos_transactional_batch_response.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package azcosmos
import (
"encoding/json"
"net/http"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
)
// TransactionalBatchResponse contains response from a transactional batch operation.
type TransactionalBatchResponse struct {
Response
// SessionToken contains the value from the session token header to be used on session consistency.
SessionToken string
// OperationResults contains the individual batch operation results.
// The order of the results is the same as the order of the operations in the batch.
OperationResults []TransactionalBatchResult
// Success indicates if the transaction was successfully committed.
// If false, one of the operations in the batch failed.
// Inspect the OperationResults, any operation with status code http.StatusFailedDependency is a dependency failure.
// The cause of the batch failure is the first operation with status code different from http.StatusFailedDependency.
Success bool
}
func newTransactionalBatchResponse(resp *http.Response) (TransactionalBatchResponse, error) {
response := TransactionalBatchResponse{
Response: newResponse(resp),
}
response.SessionToken = resp.Header.Get(cosmosHeaderSessionToken)
response.Success = resp.StatusCode != http.StatusMultiStatus
if err := runtime.UnmarshalAsJSON(resp, &response.OperationResults); err != nil {
return TransactionalBatchResponse{}, err
}
return response, nil
}
// TransactionalBatchResult represents the result of a single operation in a batch.
type TransactionalBatchResult struct {
// StatusCode contains the status code of the operation.
StatusCode int32
// RequestCharge contains the request charge for the operation.
RequestCharge float32
// ResourceBody contains the body response of the operation.
// This property is available depending on the EnableContentResponseOnWrite option.
ResourceBody []byte
// ETag contains the ETag of the operation.
ETag azcore.ETag
}
// UnmarshalJSON implements the json.Unmarshaler interface
func (or *TransactionalBatchResult) UnmarshalJSON(b []byte) error {
var attributes map[string]json.RawMessage
err := json.Unmarshal(b, &attributes)
if err != nil {
return err
}
if statusCode, ok := attributes["statusCode"]; ok {
if err := json.Unmarshal(statusCode, &or.StatusCode); err != nil {
return err
}
}
if requestCharge, ok := attributes["requestCharge"]; ok {
if err := json.Unmarshal(requestCharge, &or.RequestCharge); err != nil {
return err
}
}
if etag, ok := attributes["eTag"]; ok {
if err := json.Unmarshal(etag, &or.ETag); err != nil {
return err
}
}
if body, ok := attributes["resourceBody"]; ok {
or.ResourceBody = body
}
return nil
}