-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathapi.java.tmpl
More file actions
219 lines (199 loc) · 8.87 KB
/
Copy pathapi.java.tmpl
File metadata and controls
219 lines (199 loc) · 8.87 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
// Code generated from OpenAPI specs by Databricks SDK Generator. DO NOT EDIT.
package com.databricks.sdk.service.{{.Package.Name}};
import java.io.IOException;
import java.io.InputStream;
import java.util.Collection;
import java.util.Map;
import java.time.Duration;
import java.util.Arrays;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import java.util.function.Function;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.databricks.sdk.core.ApiClient;
import com.databricks.sdk.core.DatabricksException;
import com.databricks.sdk.support.Generated;
import com.databricks.sdk.support.Paginator;
import com.databricks.sdk.support.Wait;
{{range .Package.ImportedEntities}}
import com.databricks.sdk.service.{{.Package.Name}}.{{.PascalName}};{{end}}
{{if .Description}}/**
{{.Comment " * " 80}}
*/{{end}}
@Generated
public class {{.PascalName}}API {
private static final Logger LOG = LoggerFactory.getLogger({{.PascalName}}API.class);
private final {{.PascalName}}Service impl;
{{range .Subservices}}
private {{.PascalName}}API {{.CamelName}}API;
{{end}}
/** Regular-use constructor */
public {{.PascalName}}API(ApiClient apiClient) {
impl = new {{.PascalName}}Impl(apiClient);
{{range .Subservices}}
{{.CamelName}}API = new {{.PascalName}}API(apiClient);
{{end}}
}
/** Constructor for mocks */
public {{.PascalName}}API({{.PascalName}}Service mock) {
impl = mock;
}
{{range .Waits}}
public {{.Poll.Response.PascalName}} {{.CamelName}}({{range $i, $x := .Binding}}{{if $i}}, {{end}}{{template "type" .PollField.Entity}} {{.PollField.CamelName}}{{end}})
throws TimeoutException {
return {{.CamelName}}({{range .Binding}}{{.PollField.CamelName}}, {{end}}Duration.ofMinutes({{.Timeout}}), null);
}
public {{.Poll.Response.PascalName}} {{.CamelName}}({{range .Binding}}{{template "type" .PollField.Entity}} {{.PollField.CamelName}}, {{end}}
Duration timeout, Consumer<{{.Poll.Response.PascalName}}> callback) throws TimeoutException {
long deadline = System.currentTimeMillis() + timeout.toMillis();
java.util.List<{{.Status.Entity.PascalName}}> targetStates = Arrays.asList({{range $i,$x := .Success}}{{if $i}}, {{end}}{{.Entity.PascalName}}.{{.ConstantName}}{{end}});{{if .Failure}}
java.util.List<{{.Status.Entity.PascalName}}> failureStates = Arrays.asList({{range $i,$x := .Failure}}{{if $i}}, {{end}}{{.Entity.PascalName}}.{{.ConstantName}}{{end}});{{end}}
String statusMessage = "polling...";
int attempt = 1;
while (System.currentTimeMillis() < deadline) {
{{.Poll.Response.PascalName}} poll = {{template "java-name" .Poll}}(new {{.Poll.Request.PascalName}}(){{range .Binding}}.set{{.PollField.PascalName}}({{.PollField.CamelName}}){{- end}});
{{.Status.Entity.PascalName}} status = poll{{range .StatusPath}}.get{{.PascalName}}(){{end}};
{{if .ComplexMessagePath -}}
statusMessage = String.format("current status: %s", status);
if (poll.get{{.MessagePathHead.PascalName}}() != null) {
statusMessage = poll{{range .MessagePath}}.get{{.PascalName}}(){{end}};
}
{{- else if .MessagePath -}}
statusMessage = poll{{range .MessagePath}}.get{{.PascalName}}(){{end}};
{{- else -}}
statusMessage = String.format("current status: %s", status);
{{- end}}
if (targetStates.contains(status)) {
return poll;
}
if (callback != null) {
callback.accept(poll);
}{{if .Failure -}}
if (failureStates.contains(status)) {
String msg = String.format("failed to reach {{range $i, $e := .Success}}{{if $i}} or {{end}}{{$e.ConstantName}}{{end}}, got %s: %s", status, statusMessage);
throw new IllegalStateException(msg);
}
{{end}}
String prefix = String.format("{{range $i, $b := .Binding}}{{if $i}}, {{end -}}{{.PollField.CamelName}}=%s{{- end}}"{{range .Binding}}, {{.PollField.CamelName}}{{- end}});
int sleep = attempt;
if (sleep > 10) {
// sleep 10s max per attempt
sleep = 10;
}
LOG.info("{}: ({}) {} (sleeping ~{}s)", prefix, status, statusMessage, sleep);
try {
Thread.sleep((long) (sleep * 1000L + Math.random() * 1000));
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new DatabricksException("Current thread was interrupted", e);
}
attempt++;
}
throw new TimeoutException(String.format("timed out after %s: %s", timeout, statusMessage));
}
{{end}}
{{range .Methods}}
{{if and .Request .Request.RequiredFields}}
public {{template "method-return-type" .}} {{template "java-name" .}}({{range $i, $p := .Request.RequiredFields -}}
{{if $i}}, {{end}}{{template "type-unboxed" .Entity }} {{.CamelName}}{{if .IsNameReserved}}Value{{end}}
{{- end}}) {
{{if or (not .Response.IsEmpty) .Wait -}}return {{end}}{{template "java-name" .}}(new {{.Request.PascalName}}(){{range .Request.RequiredFields}}
.set{{.PascalName}}({{.CamelName}}{{if .IsNameReserved}}Value{{end}}){{end}});
}
{{end}}
{{if .Description}}/**
{{.Comment " * " 80}}
*/{{end}}
public {{template "method-return-type" .}} {{template "java-name" .}}({{if .Request}}{{template "type" .Request}} request{{end}}) {
{{template "method-call" .}}
}
{{end}}
{{range .Subservices}}
/**
* {{.Summary}}
*/
public {{.PascalName}}API {{.PascalName}}() {
return {{.CamelName}}API;
}
{{end}}
public {{.PascalName}}Service impl() {
return impl;
}
}
{{define "java-name" -}}
{{.CamelName}}{{if .IsNameReserved}}Content{{end}}
{{- end}}
{{define "method-call" -}}
{{if and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) -}}{{template "method-call-retried" .}}
{{- else if .Pagination -}}{{template "method-call-paginated" .}}
{{- else}}{{template "method-call-default" .}}{{end}}
{{- end}}
{{define "method-call-paginated" -}}
{{- if and .Pagination.Offset (not (eq .Path "/api/2.0/clusters/events")) -}}
request.set{{.Pagination.Offset.PascalName}}(
{{- if eq .Pagination.Increment 1 -}}
1
{{- else if contains .Path "/scim/v2/" -}}
1
{{- else -}}
0
{{- end}}L);{{end -}}
{{if and .Pagination.Limit (contains .Path "/scim/v2/")}}
if (request.get{{.Pagination.Limit.PascalName}}() == null) {
request.set{{.Pagination.Limit.PascalName}}(100L);
}{{end -}}
return new Paginator<>(
{{ if .Request }}request{{ else }}null{{ end }},
{{ if .Request }}impl::{{template "java-name" .}}{{ else }}(Void v) -> impl.{{template "java-name" .}}(){{ end }},
{{template "type" .Response}}::get{{.Pagination.Results.PascalName}},
response ->
{{ if not .Pagination.MultiRequest }}
null
{{- else if eq .Path "/api/2.0/clusters/events" -}}
response.getNextPage()
{{- else if .Pagination.Token -}}
{
String token = response.get{{.Pagination.Token.Bind.PascalName}}();
if (token == null) {
return null;
}
return request.set{{.Pagination.Token.PollField.PascalName}}(token);
}
{{- else if eq .Pagination.Increment 1 -}}
{
Long page = request.get{{.Pagination.Offset.PascalName}}();
if (page == null) {
page = 1L; // redash uses 1-based pagination
}
return request.set{{.Pagination.Offset.PascalName}}(page+1L);
}
{{- else -}}
{
Long offset = request.get{{.Pagination.Offset.PascalName}}();
if (offset == null) {
offset = 0L;
}
offset += response.get{{.Pagination.Results.PascalName}}().size();
return request.set{{.Pagination.Offset.PascalName}}(offset);
}
{{- end}}
){{if .NeedsOffsetDedupe -}}.withDedupe({{.Pagination.Entity.PascalName}}::get{{.IdentifierField.PascalName}}){{end}};
{{- end}}
{{define "method-call-retried" -}}
{{if not .Response.IsEmpty}}{{.Response.PascalName}} response = {{end}}impl.{{template "java-name" .}}(request);
return new Wait<>((timeout, callback) -> {{.Wait.CamelName}}({{range .Wait.Binding}}{{if .IsResponseBind}}response{{else}}request{{end}}.get{{.Bind.PascalName}}(), {{end}}timeout, callback){{if not .Response.IsEmpty}}, response{{end}});
{{- end}}
{{define "method-call-default" -}}
{{if not .Response.IsEmpty -}}return {{end}}impl.{{template "java-name" .}}({{if .Request}}request{{end}});
{{- end}}
{{define "method-return-type" -}}
{{if and .Wait (and (not .IsCrudRead) (not (eq .SnakeName "get_run"))) }}Wait<{{.Wait.Poll.Response.PascalName}},{{if not .Response.IsEmpty}}{{.Response.PascalName}}{{else}}Void{{end}}>
{{- else if not .Response.IsEmpty }}{{if .Response.ArrayValue -}}
Iterable<{{ template "type" .Response.ArrayValue }}>
{{- else if .Pagination -}}
Iterable<{{ template "type" .Pagination.Entity }}>
{{- else -}}
{{template "type" .Response}}
{{- end}}{{else}}void{{end}}
{{- end}}