-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feeat(openapi): add batch endpoint to v2 using requestbody (#10100)
- Loading branch information
1 parent
a6b1701
commit d552106
Showing
12 changed files
with
211 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
plugins { | ||
id 'java' | ||
} | ||
|
||
dependencies { | ||
implementation project(':entity-registry') | ||
implementation project(':metadata-operation-context') | ||
implementation project(':metadata-auth:auth-api') | ||
|
||
implementation externalDependency.jacksonDataBind | ||
implementation externalDependency.httpClient | ||
|
||
compileOnly externalDependency.lombok | ||
|
||
annotationProcessor externalDependency.lombok | ||
} |
88 changes: 88 additions & 0 deletions
88
.../openapi-servlet/models/src/main/java/io/datahubproject/openapi/client/OpenApiClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package io.datahubproject.openapi.client; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import io.datahubproject.metadata.context.OperationContext; | ||
import io.datahubproject.openapi.v2.models.BatchGetUrnRequest; | ||
import io.datahubproject.openapi.v2.models.BatchGetUrnResponse; | ||
import java.io.ByteArrayOutputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import lombok.Getter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.apache.hc.client5.http.classic.methods.HttpPost; | ||
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient; | ||
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder; | ||
import org.apache.hc.core5.http.ClassicHttpResponse; | ||
import org.apache.hc.core5.http.ContentType; | ||
import org.apache.hc.core5.http.HttpHeaders; | ||
import org.apache.hc.core5.http.io.entity.StringEntity; | ||
|
||
/** TODO: This should be autogenerated from our own OpenAPI */ | ||
@Slf4j | ||
public class OpenApiClient { | ||
|
||
private final CloseableHttpClient httpClient; | ||
private final String gmsHost; | ||
private final int gmsPort; | ||
private final boolean useSsl; | ||
@Getter private final OperationContext systemOperationContext; | ||
|
||
private static final String OPENAPI_PATH = "/openapi/v2/entity/batch/"; | ||
private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); | ||
|
||
public OpenApiClient( | ||
String gmsHost, int gmsPort, boolean useSsl, OperationContext systemOperationContext) { | ||
this.gmsHost = gmsHost; | ||
this.gmsPort = gmsPort; | ||
this.useSsl = useSsl; | ||
httpClient = HttpClientBuilder.create().build(); | ||
this.systemOperationContext = systemOperationContext; | ||
} | ||
|
||
public BatchGetUrnResponse getBatchUrnsSystemAuth(String entityName, BatchGetUrnRequest request) { | ||
return getBatchUrns( | ||
entityName, | ||
request, | ||
systemOperationContext.getSystemAuthentication().get().getCredentials()); | ||
} | ||
|
||
public BatchGetUrnResponse getBatchUrns( | ||
String entityName, BatchGetUrnRequest request, String authCredentials) { | ||
String url = | ||
(useSsl ? "https://" : "http://") + gmsHost + ":" + gmsPort + OPENAPI_PATH + entityName; | ||
HttpPost httpPost = new HttpPost(url); | ||
httpPost.setHeader(HttpHeaders.AUTHORIZATION, authCredentials); | ||
try { | ||
httpPost.setEntity( | ||
new StringEntity( | ||
OBJECT_MAPPER.writeValueAsString(request), ContentType.APPLICATION_JSON)); | ||
httpPost.setHeader("Content-type", "application/json"); | ||
return httpClient.execute(httpPost, OpenApiClient::mapResponse); | ||
} catch (IOException e) { | ||
log.error("Unable to execute Batch Get request for urn: " + request.getUrns(), e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static BatchGetUrnResponse mapResponse(ClassicHttpResponse response) { | ||
BatchGetUrnResponse serializedResponse; | ||
try { | ||
ByteArrayOutputStream result = new ByteArrayOutputStream(); | ||
InputStream contentStream = response.getEntity().getContent(); | ||
byte[] buffer = new byte[1024]; | ||
int length = contentStream.read(buffer); | ||
while (length > 0) { | ||
result.write(buffer, 0, length); | ||
length = contentStream.read(buffer); | ||
} | ||
serializedResponse = | ||
OBJECT_MAPPER.readValue( | ||
result.toString(StandardCharsets.UTF_8), BatchGetUrnResponse.class); | ||
} catch (IOException e) { | ||
log.error("Wasn't able to convert response into expected type.", e); | ||
throw new RuntimeException(e); | ||
} | ||
return serializedResponse; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
...-servlet/models/src/main/java/io/datahubproject/openapi/v2/models/BatchGetUrnRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package io.datahubproject.openapi.v2.models; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.io.Serializable; | ||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.EqualsAndHashCode; | ||
import lombok.Value; | ||
|
||
@Value | ||
@EqualsAndHashCode | ||
@Builder | ||
@JsonDeserialize(builder = BatchGetUrnRequest.BatchGetUrnRequestBuilder.class) | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
public class BatchGetUrnRequest implements Serializable { | ||
@JsonProperty("urns") | ||
@Schema(required = true, description = "The list of urns to get.") | ||
List<String> urns; | ||
|
||
@JsonProperty("aspectNames") | ||
@Schema(required = true, description = "The list of aspect names to get") | ||
List<String> aspectNames; | ||
|
||
@JsonProperty("withSystemMetadata") | ||
@Schema(required = true, description = "Whether or not to retrieve system metadata") | ||
boolean withSystemMetadata; | ||
} |
20 changes: 20 additions & 0 deletions
20
...servlet/models/src/main/java/io/datahubproject/openapi/v2/models/BatchGetUrnResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.datahubproject.openapi.v2.models; | ||
|
||
import com.fasterxml.jackson.annotation.JsonInclude; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import io.swagger.v3.oas.annotations.media.Schema; | ||
import java.io.Serializable; | ||
import java.util.List; | ||
import lombok.Builder; | ||
import lombok.Value; | ||
|
||
@Value | ||
@Builder | ||
@JsonInclude(JsonInclude.Include.NON_NULL) | ||
@JsonDeserialize(builder = BatchGetUrnResponse.BatchGetUrnResponseBuilder.class) | ||
public class BatchGetUrnResponse implements Serializable { | ||
@JsonProperty("entities") | ||
@Schema(description = "List of entity responses") | ||
List<GenericEntity> entities; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters