Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private ApiClient(Builder builder) {
bodyLogger = new BodyLogger(mapper, 1024, debugTruncateBytes);
}

private static <I> void setQuery(Request in, I entity) {
public static <I> void setQuery(Request in, I entity) {
if (entity == null) {
return;
}
Expand All @@ -161,140 +161,33 @@ private static <I> void setQuery(Request in, I entity) {
}
}

private static <I> void setHeaders(Request in, Map<String, String> headers) {
if (headers == null) {
return;
}
for (Map.Entry<String, String> e : headers.entrySet()) {
in.withHeader(e.getKey(), e.getValue());
}
}

public <I, O> Collection<O> getCollection(
String path, I in, Class<O> element, Map<String, String> headers) {
public <O> Collection<O> getCollection(Request req, Class<O> element) {
return withJavaType(
path,
in,
mapper.getTypeFactory().constructCollectionType(Collection.class, element),
headers);
req, mapper.getTypeFactory().constructCollectionType(Collection.class, element));
}

public <I> Map<String, String> getStringMap(String path, I in, Map<String, String> headers) {
public Map<String, String> getStringMap(Request req) {
return withJavaType(
path,
in,
mapper.getTypeFactory().constructMapType(Map.class, String.class, String.class),
headers);
req, mapper.getTypeFactory().constructMapType(Map.class, String.class, String.class));
}

protected <I, O> O withJavaType(
String path, I in, JavaType javaType, Map<String, String> headers) {
protected <I, O> O withJavaType(Request request, JavaType javaType) {
try {
Request request = prepareRequest("GET", path, in, headers);
Response response = getResponse(request);
return deserialize(response.getBody(), javaType);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

public <O> O HEAD(String path, Class<O> target, Map<String, String> headers) {
return HEAD(path, null, target, headers);
}

public <I, O> O HEAD(String path, I in, Class<O> target, Map<String, String> headers) {
try {
return execute(prepareRequest("HEAD", path, in, headers), target);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

public <O> O GET(String path, Class<O> target, Map<String, String> headers) {
return GET(path, null, target, headers);
}

public <I, O> O GET(String path, I in, Class<O> target, Map<String, String> headers) {
try {
return execute(prepareRequest("GET", path, in, headers), target);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

public <O> O POST(String path, Class<O> target, Map<String, String> headers) {
try {
return execute(prepareRequest("POST", path, null, headers), target);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

public <I, O> O POST(String path, I in, Class<O> target, Map<String, String> headers) {
try {
return execute(prepareRequest("POST", path, in, headers), target);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

public <I, O> O PUT(String path, I in, Class<O> target, Map<String, String> headers) {
try {
return execute(prepareRequest("PUT", path, in, headers), target);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

public <I, O> O PATCH(String path, I in, Class<O> target, Map<String, String> headers) {
try {
return execute(prepareRequest("PATCH", path, in, headers), target);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

public <I, O> O DELETE(String path, I in, Class<O> target, Map<String, String> headers) {
try {
return execute(prepareRequest("DELETE", path, in, headers), target);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

private boolean hasBody(String method) {
return !method.equals("GET") && !method.equals("DELETE") && !method.equals("HEAD");
}

private <I> Request prepareBaseRequest(String method, String path, I in)
throws JsonProcessingException {
if (in == null || !hasBody(method)) {
return new Request(method, path);
} else if (InputStream.class.isAssignableFrom(in.getClass())) {
InputStream body = (InputStream) in;
return new Request(method, path, body);
} else {
String body = (in instanceof String) ? (String) in : serialize(in);
return new Request(method, path, body);
}
}

private <I> Request prepareRequest(String method, String path, I in, Map<String, String> headers)
throws JsonProcessingException {
Request req = prepareBaseRequest(method, path, in);
setQuery(req, in);
setHeaders(req, headers);
return req;
}

/**
* Executes HTTP request with retries and converts it to proper POJO
*
* @param in Commons HTTP request
* @param target Expected pojo type
* @return POJO of requested type
*/
private <T> T execute(Request in, Class<T> target) throws IOException {
public <T> T execute(Request in, Class<T> target) throws IOException {
Response out = getResponse(in);
if (target == Void.class) {
return null;
Expand Down Expand Up @@ -533,7 +426,7 @@ public <T> void deserialize(Response response, T object) throws IOException {
}
}

private String serialize(Object body) throws JsonProcessingException {
public String serialize(Object body) throws JsonProcessingException {
if (body == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -586,10 +586,13 @@ private OpenIDConnectEndpoints fetchDefaultOidcEndpoints() throws IOException {
.withHttpClient(getHttpClient())
.withGetHostFunc(v -> getHost())
.build();
return apiClient.GET(
"/oidc/.well-known/oauth-authorization-server",
OpenIDConnectEndpoints.class,
new HashMap<>());
try {
return apiClient.execute(
new Request("GET", "/oidc/.well-known/oauth-authorization-server"),
OpenIDConnectEndpoints.class);
} catch (IOException e) {
throw new DatabricksException("IO error: " + e.getMessage(), e);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.databricks.sdk.core.DatabricksException;
import com.databricks.sdk.core.http.FormRequest;
import com.databricks.sdk.core.http.HttpClient;
import com.databricks.sdk.core.http.Request;
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Base64;
Expand Down Expand Up @@ -61,12 +62,11 @@ protected static Token retrieveToken(
break;
}
headers.put("Content-Type", "application/x-www-form-urlencoded");
Request req = new Request("POST", tokenUrl, FormRequest.wrapValuesInList(params));
req.withHeaders(headers);
try {
ApiClient apiClient = new ApiClient.Builder().withHttpClient(hc).build();

OAuthResponse resp =
apiClient.POST(
tokenUrl, FormRequest.wrapValuesInList(params), OAuthResponse.class, headers);
OAuthResponse resp = apiClient.execute(req, OAuthResponse.class);
if (resp.getErrorCode() != null) {
throw new IllegalArgumentException(resp.getErrorCode() + ": " + resp.getErrorSummary());
}
Expand Down
Loading
Loading