Skip to content

Add common project #1043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Mar 8, 2019
Merged
5 changes: 3 additions & 2 deletions assistant/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
22 changes: 22 additions & 0 deletions common/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import org.apache.tools.ant.filters.*

apply plugin: 'checkstyle'
apply plugin: 'java'

checkstyle {
configFile = rootProject.file('checkstyle.xml')
ignoreFailures = false
}

repositories {
mavenCentral()
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
}

processResources {
filter ReplaceTokens, tokens: ["pom.version": project.version]
}
54 changes: 54 additions & 0 deletions common/src/main/java/com/ibm/watson/common/SdkCommon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.ibm.watson.common;

import com.ibm.cloud.sdk.core.http.HttpHeaders;
import com.ibm.cloud.sdk.core.util.RequestUtils;

import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SdkCommon {
private static final Logger LOG = Logger.getLogger(SdkCommon.class.getName());
private static String userAgent;

private SdkCommon() { }

private static String loadSdkVersion() {
ClassLoader classLoader = SdkCommon.class.getClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("java-sdk-version.properties");
Properties properties = new Properties();

try {
properties.load(inputStream);
} catch (Exception e) {
LOG.log(Level.WARNING, "Could not load java-sdk-version.properties", e);
}

return properties.getProperty("version", "unknown-version");
}

private static String getUserAgent() {
if (userAgent == null) {
userAgent = "watson-apis-java-sdk/" + loadSdkVersion() + "; " + RequestUtils.getUserAgent();
}
return userAgent;
}

public static Map<String, String> getDefaultHeaders(String serviceName, String serviceVersion, String operationId) {
Map<String, String> headers = new HashMap<>();

String sdkAnalyticsHeaderValue = String.format(
"service_name=%s;service_version=%s;operation_id=%s",
serviceName,
serviceVersion,
operationId
);

headers.put(WatsonHttpHeaders.X_IBMCLOUD_SDK_ANALYTICS, sdkAnalyticsHeaderValue);
headers.put(HttpHeaders.USER_AGENT, getUserAgent());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

technically speaking... :) the user agent header value could be stored in a static field and not computed each time getDefaultHeaders() is called. I'm sure the performance difference is negligible, but I couldn't stop myself from mentioning it :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any performance improvement is a good one! Done

return headers;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.ibm.watson.common;

public interface WatsonHttpHeaders {
/** Header containing analytics info. */
String X_IBMCLOUD_SDK_ANALYTICS = "X-IBMCloud-SDK-Analytics";
}
1 change: 1 addition & 0 deletions common/src/main/resources/java-sdk-version.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[email protected]@
26 changes: 26 additions & 0 deletions common/src/test/java/com/ibm/watson/common/SdkCommonTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.ibm.watson.common;

import com.ibm.cloud.sdk.core.http.HttpHeaders;
import org.junit.Test;

import java.util.Map;

import static org.junit.Assert.assertTrue;

public class SdkCommonTest {
@Test
public void testGetDefaultHeaders() {
String serviceName = "test_name";
String serviceVersion = "v1";
String operationId = "test_method";
Map<String, String> defaultHeaders = SdkCommon.getDefaultHeaders(serviceName, serviceVersion, operationId);

assertTrue(defaultHeaders.containsKey(WatsonHttpHeaders.X_IBMCLOUD_SDK_ANALYTICS));
String analyticsHeaderValue = defaultHeaders.get(WatsonHttpHeaders.X_IBMCLOUD_SDK_ANALYTICS);
assertTrue(analyticsHeaderValue.contains(serviceName));
assertTrue(analyticsHeaderValue.contains(serviceVersion));
assertTrue(analyticsHeaderValue.contains(operationId));
assertTrue(defaultHeaders.containsKey(HttpHeaders.USER_AGENT));
assertTrue(defaultHeaders.get(HttpHeaders.USER_AGENT).startsWith("watson-apis-java-sdk/"));
}
}
5 changes: 3 additions & 2 deletions compare-comply/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
5 changes: 3 additions & 2 deletions discovery/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
5 changes: 3 additions & 2 deletions language-translator/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
5 changes: 3 additions & 2 deletions natural-language-classifier/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
5 changes: 3 additions & 2 deletions natural-language-understanding/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
5 changes: 3 additions & 2 deletions personality-insights/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
2 changes: 1 addition & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
rootProject.name = 'ibm-watson'
include ':assistant', ':compare-comply', ':discovery', ':language-translator', ':natural-language-classifier',
':natural-language-understanding', ':personality-insights', ':speech-to-text', ':text-to-speech',
':tone-analyzer', ':visual-recognition', ':ibm-watson'
':tone-analyzer', ':visual-recognition', ':ibm-watson', ':common'
5 changes: 3 additions & 2 deletions speech-to-text/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
5 changes: 3 additions & 2 deletions text-to-speech/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
5 changes: 3 additions & 2 deletions tone-analyzer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down
5 changes: 3 additions & 2 deletions visual-recognition/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,9 @@ checkstyle {
}

dependencies {
compile 'com.ibm.cloud:sdk-core:1.2.1'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.1', classifier: 'tests'
compile project(':common')
compile 'com.ibm.cloud:sdk-core:1.2.2'
testCompile group: 'com.ibm.cloud', name:'sdk-core', version: '1.2.2', classifier: 'tests'
signature 'org.codehaus.mojo.signature:java17:1.0@signature'
}

Expand Down