Skip to content

Commit

Permalink
WS-3314: Duplicate a few more headers. Add extra properties to samples.
Browse files Browse the repository at this point in the history
  • Loading branch information
seth-mg committed Nov 1, 2024
1 parent c720871 commit f5caf78
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ Versions, of the form `x.y.z`, where `z` is greater than or equal to `100`, are
them without consultation with Babel Street.

#### Examples
View small example programs for each Rosette endpoint in the
View small example programs for each Analytics endpoint in the
[examples](examples/src/main/java/com/basistech/rosette/examples) directory.

#### Documentation & Support
Expand Down
2 changes: 1 addition & 1 deletion all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
</parent>
<artifactId>rosette-api-all</artifactId>
<name>rosette-api-all</name>
<description>Analytics API all modules combined</description>
<description>Babel Street Analytics API all modules combined</description>
<dependencies>
<dependency>
<groupId>com.basistech</groupId>
Expand Down
32 changes: 28 additions & 4 deletions api/src/main/java/com/basistech/rosette/api/HttpRosetteAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@
public class HttpRosetteAPI extends AbstractRosetteAPI {

public static final String DEFAULT_URL_BASE = "https://analytics.babelstreet.com/rest/v1";
public static final String SERVICE_NAME = "RosetteAPI";
public static final String SERVICE_NAME = "Babel-Street-Analytics-API";
public static final String BINDING_VERSION = getVersion();
public static final String USER_AGENT_STR = SERVICE_NAME + "-Java/" + BINDING_VERSION + "/"
+ System.getProperty("java.version");
private static final Logger LOG = LoggerFactory.getLogger(HttpRosetteAPI.class);
private static final String IO_EXCEPTION_MESSAGE = "IO Exception communicating with the Rosette API";
private static final String IO_EXCEPTION_MESSAGE = "IO Exception communicating with the Babel Street Analytics API";
private static final Pattern TRAILING_SLASHES = Pattern.compile("/+$");
private String urlBase = DEFAULT_URL_BASE;
private int failureRetries = 1;
Expand Down Expand Up @@ -198,6 +198,9 @@ private void initHeaders(String key, List<Header> additionalHeaders) {
this.additionalHeaders.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "gzip"));
if (key != null) {
this.additionalHeaders.add(new BasicHeader("X-BabelStreetAPI-Key", key));
this.additionalHeaders.add(new BasicHeader("X-BabelStreetAPI-Binding", "java"));
this.additionalHeaders.add(new BasicHeader("X-BabelStreetAPI-Binding-Version", BINDING_VERSION));
// TODO: Remove in a future release.
this.additionalHeaders.add(new BasicHeader("X-RosetteAPI-Binding", "java"));
this.additionalHeaders.add(new BasicHeader("X-RosetteAPI-Binding-Version", BINDING_VERSION));
}
Expand Down Expand Up @@ -401,10 +404,15 @@ private <T> T sendPostRequest(Object request, String urlStr, Class<T> clazz)
while (numRetries-- > 0) {
try (CloseableHttpResponse response = httpClient.execute(post)) {
T resp = getResponse(response, clazz);
// TODO: Remove in a future release
Header ridHeader = response.getFirstHeader("X-RosetteAPI-DocumentRequest-Id");
if (ridHeader != null && ridHeader.getValue() != null) {
LOG.debug("DocumentRequest ID {}", ridHeader.getValue());
}
Header bsidHeader = response.getFirstHeader("X-BabelStreetAPI-DocumentRequest-Id");
if (bsidHeader != null && bsidHeader.getValue() != null) {
LOG.debug("DocumentRequest ID {}", bsidHeader.getValue());
}
if (resp instanceof Response) {
responseHeadersToExtendedInformation((Response)resp, response);
}
Expand Down Expand Up @@ -556,15 +564,31 @@ private <T extends Object> T getResponse(HttpResponse httpResponse, Class<T> cla
InputStream stream = httpResponse.getEntity().getContent();
InputStream inputStream = "gzip".equalsIgnoreCase(encoding) ? new GZIPInputStream(stream) : stream) {
String ridHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-DocumentRequest-Id"));
String bsidHeader = headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-DocumentRequest-Id"));
if (HTTP_OK != status) {
String ecHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-Status-Code"));
String emHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-Status-Message"));
String ecHeader;
if (headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-Status-Code")) != null) {
ecHeader = headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-Status-Code"));
} else {
// TODO: Remove in a future release
ecHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-Status-Code"));
}
String emHeader;
if (headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-Status-Message")) != null) {
emHeader = headerValueOrNull(httpResponse.getFirstHeader("X-BabelStreetAPI-Status-Message"));
} else {
// TODO: Remove in a future release
emHeader = headerValueOrNull(httpResponse.getFirstHeader("X-RosetteAPI-Status-Message"));
}
String responseContentType = headerValueOrNull(httpResponse.getFirstHeader(HttpHeaders.CONTENT_TYPE));
if ("application/json".equals(responseContentType)) {
ErrorResponse errorResponse = mapper.readValue(inputStream, ErrorResponse.class);
if (ridHeader != null) {
LOG.debug("DocumentRequest ID {}", ridHeader);
}
if (bsidHeader != null) {
LOG.debug("DocumentRequest ID {}", bsidHeader);
}
if (ecHeader != null) {
errorResponse.setCode(ecHeader);
}
Expand Down
8 changes: 4 additions & 4 deletions api/src/test/java/com/basistech/rosette/api/BasicTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Basis Technology Corp.
* Copyright 2024 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -138,7 +138,7 @@ void testHeaders() throws Exception {
.withHeader(HttpHeaders.USER_AGENT, HttpRosetteAPI.USER_AGENT_STR))
.respond(HttpResponse.response()
.withHeader("Content-Type", "application/json")
.withHeader("X-RosetteAPI-Concurrency", "5")
.withHeader("X-BabelStreetAPI-Concurrency", "5")
.withStatusCode(200)
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}", StandardCharsets.UTF_8));

Expand All @@ -148,7 +148,7 @@ void testHeaders() throws Exception {
.additionalHeader("X-Foo", "Bar")
.build();
var resp = api.ping();
assertEquals("5", resp.getExtendedInformation().get("X-RosetteAPI-Concurrency"));
assertEquals("5", resp.getExtendedInformation().get("X-BabelStreetAPI-Concurrency"));
}

@Test
Expand Down Expand Up @@ -185,7 +185,7 @@ void testExtendedInfo() throws Exception {
.withHeader("Content-Type", "application/json")
.withHeader("X-Foo", "Bar")
.withHeader("X-FooMulti", "Bar1", "Bar2")
.withHeader("X-RosetteAPI-Concurrency", "5")
.withHeader("X-BabelStreetAPI-Concurrency", "5")
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}", StandardCharsets.UTF_8));
api = new HttpRosetteAPI.Builder()
.key("foo-key")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Basis Technology Corp.
* Copyright 2024 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -50,7 +50,7 @@ void notJsonError() throws Exception {
mockServer.when(HttpRequest.request().withPath(".*/{2,}.*"))
.respond(HttpResponse.response()
.withBody("Invalid path; '//'")
.withHeader("X-RosetteAPI-Concurrency", "5")
.withHeader("X-BabelStreetAPI-Concurrency", "5")
.withStatusCode(HTTP_NOT_FOUND));
mockServer.when(HttpRequest.request()
.withMethod("GET")
Expand All @@ -59,7 +59,7 @@ void notJsonError() throws Exception {
.respond(HttpResponse.response()
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}", StandardCharsets.UTF_8)
.withStatusCode(HTTP_OK)
.withHeader("X-RosetteAPI-Concurrency", "5"));
.withHeader("X-BabelStreetAPI-Concurrency", "5"));
String mockServiceUrl = "http://localhost:" + mockServer.getPort() + "/rest//v1";
boolean exceptional = false;
try {
Expand Down
10 changes: 5 additions & 5 deletions api/src/test/java/com/basistech/rosette/api/RosetteAPITest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2022 Basis Technology Corp.
* Copyright 2024 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -100,14 +100,14 @@ public void setUp(MockServerClient mockServer) {
.withBody("{\"message\":\"Rosette API at your service\",\"time\":1461788498633}",
StandardCharsets.UTF_8)
.withStatusCode(HTTP_OK)
.withHeader("X-RosetteAPI-Concurrency", "5"));
.withHeader("X-BabelStreetAPI-Concurrency", "5"));

this.mockServer.when(HttpRequest.request()
.withPath("/info"))
.respond(HttpResponse.response()
.withStatusCode(HTTP_OK)
.withHeader("Content-Type", "application/json")
.withHeader("X-RosetteAPI-Concurrency", "5")
.withHeader("X-BabelStreetAPI-Concurrency", "5")
.withBody(INFO_RESPONSE, StandardCharsets.UTF_8));

api = new HttpRosetteAPI.Builder()
Expand Down Expand Up @@ -145,14 +145,14 @@ private void setStatusCodeResponse(String responseStr, int statusCode) throws IO
.respond(HttpResponse.response()
.withHeader("Content-Type", "application/json")
.withHeader("Content-Encoding", "gzip")
.withHeader("X-RosetteAPI-Concurrency", "5")
.withHeader("X-BabelStreetAPI-Concurrency", "5")
.withStatusCode(statusCode).withBody(gzip(responseStr)));

} else {
mockServer.when(HttpRequest.request().withPath("^(?!/info).+"))
.respond(HttpResponse.response()
.withHeader("Content-Type", "application/json")
.withHeader("X-RosetteAPI-Concurrency", "5")
.withHeader("X-BabelStreetAPI-Concurrency", "5")
.withStatusCode(statusCode).withBody(responseStr, StandardCharsets.UTF_8));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Basis Technology Corp.
* Copyright 2024 Basis Technology Corp.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -61,7 +61,7 @@ private void setupResponse(String requestPath, String responseString, int status
this.mockServer.when(HttpRequest.request().withPath(requestPath), Times.exactly(requestTimes))
.respond(HttpResponse.response()
.withHeader("Content-Type", "application/json")
.withHeader("X-RosetteAPI-Concurrency", "5")
.withHeader("X-BabelStreetAPI-Concurrency", "5")
.withStatusCode(statusCode)
.withBody(responseString)
.withDelay(TimeUnit.MILLISECONDS, delayMillis));
Expand Down
2 changes: 1 addition & 1 deletion api/src/test/mock-data/mockgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def capture(req, endpoints, filename_prefix):

headers = {"Content-Type": "application/json"}
if args.api_key:
headers['X-RosetteAPI-Key'] = args.api_key
headers['X-BabelStreetAPI-Key'] = args.api_key

# prep & clean up
for folder in ["request", "response"]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,21 @@
*/
@SuppressWarnings("java:S106")
public abstract class ExampleBase {
private static final String KEY_PROP_NAME = "rosette.api.key";
private static final String URL_PROP_NAME = "rosette.api.altUrl";
private static final String KEY_PROP_NAME = "analytics.api.key";
private static final String URL_PROP_NAME = "analytics.api.altUrl";
private static final String LEGACY_KEY_PROP_NAME = "rosette.api.key";
private static final String LEGACY_URL_PROP_NAME = "rosette.api.altUrl";
private static final String USAGE_STR = "Usage: java -cp rosette-api-examples.jar:lib/rosette-api-manifest.jar "
+ "-D" + KEY_PROP_NAME + "=<required_api_key> " + "-D" + URL_PROP_NAME + "=<optional_alternate_url> ";

/**
* @return api key using system property {@value com.basistech.rosette.examples.ExampleBase#KEY_PROP_NAME}
*/
protected String getApiKeyFromSystemProperty() {
String apiKeyStr = System.getProperty(KEY_PROP_NAME);
if (apiKeyStr == null || apiKeyStr.trim().length() < 1) {
String apiKeyStr = System.getProperty(KEY_PROP_NAME) != null
? System.getProperty(KEY_PROP_NAME)
: System.getProperty(LEGACY_KEY_PROP_NAME);
if (apiKeyStr == null || apiKeyStr.trim().isEmpty()) {
showUsage(getClass());
System.exit(1);
}
Expand All @@ -48,8 +52,10 @@ protected String getApiKeyFromSystemProperty() {
* @return alternate url using system property {@value com.basistech.rosette.examples.ExampleBase#URL_PROP_NAME}
*/
protected String getAltUrlFromSystemProperty() {
String altUrlStr = System.getProperty(URL_PROP_NAME);
if (altUrlStr == null || altUrlStr.trim().length() < 1) {
String altUrlStr = System.getProperty(URL_PROP_NAME) != null
? System.getProperty(URL_PROP_NAME)
: System.getProperty(LEGACY_URL_PROP_NAME);
if (altUrlStr == null || altUrlStr.trim().isEmpty()) {
altUrlStr = "https://analytics.babelstreet.com/rest/v1";
}
return altUrlStr.trim();
Expand Down

0 comments on commit f5caf78

Please sign in to comment.