com.databricks.sdk.core.http.Response Maven / Gradle / Ivy
package com.databricks.sdk.core.http;
import com.databricks.sdk.core.DatabricksException;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class Response {
private Request request;
private URL url;
private int statusCode;
private String status;
private Map> headers;
private InputStream body;
private String debugBody;
public Response(
Request request, int statusCode, String status, Map> headers) {
this(
request, getURLFromRequest(request), statusCode, status, headers, (InputStream) null, null);
}
public Response(
Request request, URL url, int statusCode, String status, Map> headers) {
this(request, url, statusCode, status, headers, (InputStream) null, null);
}
private static URL getURLFromRequest(Request request) {
try {
return request.getUri().toURL();
} catch (MalformedURLException e) {
throw new DatabricksException("Failed to convert URI to URL", e);
}
}
public Response(
Request request,
int statusCode,
String status,
Map> headers,
String body) {
this(
request,
getURLFromRequest(request),
statusCode,
status,
headers,
body != null ? new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)) : null,
body);
}
public Response(
Request request,
URL url,
int statusCode,
String status,
Map> headers,
String body) {
this(
request,
url,
statusCode,
status,
headers,
body != null ? new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)) : null,
body);
}
public Response(
Request request,
URL url,
int statusCode,
String status,
Map> headers,
InputStream body) {
this(request, url, statusCode, status, headers, body, "\"\"");
}
public Response(String body, URL url) {
this(
new Request("GET", "/"),
url,
200,
"OK",
Collections.emptyMap(),
new ByteArrayInputStream(body.getBytes(StandardCharsets.UTF_8)),
body);
}
private Response(
Request request,
URL url,
int statusCode,
String status,
Map> headers,
InputStream body,
String debugBody) {
this.request = request;
this.url = url;
this.statusCode = statusCode;
this.status = status;
this.headers = headers;
this.body = body;
this.debugBody = debugBody;
}
public Request getRequest() {
return request;
}
public URL getUrl() {
return url;
}
public int getStatusCode() {
return statusCode;
}
public String getStatus() {
return status;
}
public Map> getAllHeaders() {
return headers;
}
public List getHeaders(String key) {
for (Map.Entry> entry : headers.entrySet()) {
if (entry.getKey().equalsIgnoreCase(key)) {
return entry.getValue();
}
}
return null;
}
public String getFirstHeader(String key) {
List hs = getHeaders(key);
if (hs == null || hs.isEmpty()) {
return null;
}
return hs.get(0);
}
public InputStream getBody() {
return body;
}
public String getDebugBody() {
return debugBody;
}
@Override
public String toString() {
return String.format("%d %s", statusCode, status);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy