-
Notifications
You must be signed in to change notification settings - Fork 0
/
public class CalculationService2 {.java
108 lines (91 loc) · 3.97 KB
/
public class CalculationService2 {.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
public class CalculationService2 {
public class CalculationInput {
public String reference;
public List<List<Map<String, String>>> value;
}
public class CalculationRequest {
public Map<String, Object> request;
public CalculationRequest(String workspaceId, String applicationId, List<CalculationInput> inputs, List<String> outputs) {
Map<String, Object> calculation = new Map<String, Object>();
calculation.put('inputs', inputs);
calculation.put('outputs', outputs);
Map<String, Object> input = new Map<String, Object>();
input.put('calculation', calculation);
Map<String, Object> innerRequest = new Map<String, Object>();
innerRequest.put('input', input);
Map<String, Object> outerRequest = new Map<String, Object>();
outerRequest.put('workspaceId', workspaceId);
outerRequest.put('applicationId', applicationId);
outerRequest.put('request', innerRequest);
this.request = outerRequest;
}
}
public class OutputValue {
public String type;
public String formatType;
public String format;
public String text;
public String value;
public String overwrite;
}
public class Output {
public String reference;
public List<List<OutputValue>> value;
public String overwrite;
}
public class Calculation {
public Boolean success;
public List<Output> outputs;
public List<String> validations;
public List<String> messages;
}
public class OutputResponse {
public Boolean success;
public Calculation calculation;
public String goalSeek;
public String solver;
}
public class Response {
public String applicationId;
public OutputResponse response;
public String saveResult;
public Integer usedTransactionSequenceId;
public String requestId;
public Boolean success;
public String eventCreationDate;
public String retryIndex;
public String debugRetryAllowFailureCount;
}
public class CalculationResponse {
public Response response;
public Map<String, Double> timingsSeconds;
public String performanceInformation;
public Boolean isError;
public List<String> messages;
public String rawResponse;
}
public static CalculationResponse calculate(String workspaceId, String applicationId, List<CalculationInput> inputs, List<String> outputs) {
String token = TokenService.getBearerToken();
if(token == null) {
throw new CalloutException('Token is null. Cannot proceed with the API call.');
}
Http http = new Http();
HttpRequest request = new HttpRequest();
request.setEndpoint('https://api.spreadsheetweb.com/calculations/calculatesingle');
request.setMethod('POST');
request.setHeader('Authorization', 'Bearer ' + token);
request.setHeader('Content-Type', 'application/json');
CalculationRequest calculationRequest = new CalculationRequest(workspaceId, applicationId, inputs, outputs);
String body = JSON.serialize(calculationRequest);
request.setBody(body);
HttpResponse response = http.send(request);
if (response.getStatusCode() == 200) {
String responseBody = response.getBody();
CalculationResponse calculationResponse = (CalculationResponse) JSON.deserialize(responseBody, CalculationResponse.class);
calculationResponse.rawResponse = responseBody;
return calculationResponse;
} else {
throw new CalloutException('Failed to perform calculation: ' + response.getBody());
}
}
}