-
Notifications
You must be signed in to change notification settings - Fork 531
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
Add common project #1043
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
009a67f
feat: Add common project to handle logic common to all services but s…
lpatino10 758f3f2
feat(common): Retrieve User-Agent from core and append to current pac…
lpatino10 997f386
test(common): Add test for getDefaultHeaders()
lpatino10 25defa2
Merge branch 'v7.0.0' of https://github.com/watson-developer-cloud/ja…
lpatino10 312dd27
refactor(SdkCommon): Keep static userAgent value
lpatino10 6b0a381
feat: Add interface for SDK-specific HTTP headers
lpatino10 2443680
build: Update core dependencies
lpatino10 40a845d
test(SdkCommon): Remove unnecessary extension and change header class…
lpatino10 5802ab6
build(SdkCommon): Add checkstyle to common class (and clean up subseq…
lpatino10 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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] | ||
} |
This file contains hidden or 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,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()); | ||
return headers; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
common/src/main/java/com/ibm/watson/common/WatsonHttpHeaders.java
This file contains hidden or 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,6 @@ | ||
package com.ibm.watson.common; | ||
|
||
public interface WatsonHttpHeaders { | ||
/** Header containing analytics info. */ | ||
String X_IBMCLOUD_SDK_ANALYTICS = "X-IBMCloud-SDK-Analytics"; | ||
} |
This file contains hidden or 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 @@ | ||
[email protected]@ |
26 changes: 26 additions & 0 deletions
26
common/src/test/java/com/ibm/watson/common/SdkCommonTest.java
This file contains hidden or 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,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/")); | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -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' |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
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