-
Notifications
You must be signed in to change notification settings - Fork 1
/
CreateBusinessPartnerCommand.java
72 lines (57 loc) · 3.07 KB
/
CreateBusinessPartnerCommand.java
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
package com.sap.cloud.sdk.tutorial.utils;
import java.util.List;
import com.google.gson.Gson;
import java.time.Duration;
import java.util.Collections;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.sap.cloud.sdk.cloudplatform.resilience.ResilienceConfiguration;
import com.sap.cloud.sdk.cloudplatform.resilience.ResilienceDecorator;
import com.sap.cloud.sdk.cloudplatform.resilience.ResilienceIsolationMode;
import com.sap.cloud.sdk.cloudplatform.resilience.ResilienceRuntimeException;
import com.sap.cloud.sdk.datamodel.odata.client.exception.ODataException;
import com.sap.cloud.sdk.cloudplatform.connectivity.HttpDestination;
import com.sap.cloud.sdk.datamodel.odata.helper.ModificationResponse;
import com.sap.cloud.sdk.s4hana.datamodel.odata.namespaces.businesspartner.BusinessPartner;
import com.sap.cloud.sdk.s4hana.datamodel.odata.services.BusinessPartnerService;
import com.sap.cloud.sdk.s4hana.datamodel.odata.services.DefaultBusinessPartnerService;
public class CreateBusinessPartnerCommand {
private static final long serialVersionUID = 1L;
private static final Logger logger = LoggerFactory.getLogger(CreateBusinessPartnerCommand.class);
private final HttpDestination destination;
private final BusinessPartner businessPartner;
private static final String APIKEY_HEADER = "apikey";
private static final String SANDBOX_APIKEY = "<YOUR APIKEY GOES HERE>";
private final BusinessPartnerService businessPartnerService;
private final ResilienceConfiguration myResilienceConfig;
public CreateBusinessPartnerCommand(HttpDestination destination, BusinessPartner businessPartner) {
this(destination, businessPartner,
new DefaultBusinessPartnerService());
}
public CreateBusinessPartnerCommand(HttpDestination destination, BusinessPartner businessPartner, BusinessPartnerService service) {
this.destination = destination;
this.businessPartner = businessPartner;
businessPartnerService = service;
myResilienceConfig = ResilienceConfiguration.of(BusinessPartnerService.class)
.isolationMode(ResilienceIsolationMode.TENANT_AND_USER_OPTIONAL)
.timeLimiterConfiguration(
ResilienceConfiguration.TimeLimiterConfiguration.of().timeoutDuration(Duration.ofMillis(10000)))
.bulkheadConfiguration(ResilienceConfiguration.BulkheadConfiguration.of().maxConcurrentCalls(20));
}
public ModificationResponse<BusinessPartner> execute() {
return ResilienceDecorator.executeSupplier(this::run, myResilienceConfig, e -> {
logger.warn("Fallback called because of exception.", e);
return null;
});
}
private ModificationResponse<BusinessPartner> run() {
try {
return businessPartnerService.createBusinessPartner(businessPartner)
.withHeader(APIKEY_HEADER, SANDBOX_APIKEY)
.executeRequest(destination);
} catch (ODataException e) {
throw new ResilienceRuntimeException(e);
}
}
}