-
Notifications
You must be signed in to change notification settings - Fork 931
Add useragent for service and version #6212
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
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5075de7
Add changelog
RanVaknin bcbe304
Refactor test, edit changelog
RanVaknin 2218184
Add user agent for service version
RanVaknin 91cfbf6
Add changelog
RanVaknin 9f574ee
Remove extra changelog
RanVaknin 921711c
Fix unit test
RanVaknin d217303
Fix fixture files
RanVaknin 1e12de3
Fix more fixture files
RanVaknin bf40579
Move useragent info to ServiceVersionInfo
RanVaknin 3803f67
Building useragent in client class instead of a constant
RanVaknin 5af32f0
Refactor, add test
RanVaknin d5db4dc
Removing empty string default
RanVaknin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"type": "feature", | ||
"category": "AWS SDK for Java v2", | ||
"contributor": "", | ||
"description": "Adding the version of the used service to the useragent" | ||
} |
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
74 changes: 74 additions & 0 deletions
74
...in/java/software/amazon/awssdk/codegen/poet/client/specs/ServiceVersionUserAgentSpec.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,74 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.poet.client.specs; | ||
|
||
import static software.amazon.awssdk.core.util.VersionInfo.SDK_VERSION; | ||
|
||
import com.squareup.javapoet.ClassName; | ||
import com.squareup.javapoet.FieldSpec; | ||
import com.squareup.javapoet.MethodSpec; | ||
import com.squareup.javapoet.TypeSpec; | ||
import javax.lang.model.element.Modifier; | ||
import software.amazon.awssdk.codegen.model.intermediate.IntermediateModel; | ||
import software.amazon.awssdk.codegen.poet.ClassSpec; | ||
import software.amazon.awssdk.codegen.poet.PoetExtension; | ||
import software.amazon.awssdk.codegen.poet.PoetUtils; | ||
|
||
|
||
public class ServiceVersionUserAgentSpec implements ClassSpec { | ||
private final PoetExtension poetExtension; | ||
private final IntermediateModel model; | ||
|
||
public ServiceVersionUserAgentSpec(IntermediateModel model) { | ||
this.poetExtension = new PoetExtension(model); | ||
this.model = model; | ||
} | ||
|
||
@Override | ||
public TypeSpec poetSpec() { | ||
TypeSpec.Builder builder = TypeSpec.classBuilder("ServiceVersionUserAgent") | ||
RanVaknin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.addModifiers(Modifier.PUBLIC, Modifier.FINAL) | ||
.addAnnotation(PoetUtils.generatedAnnotation()) | ||
alextwoods marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.addField(userAgentField()) | ||
.addMethod(privateConstructor()); | ||
return builder.build(); | ||
} | ||
|
||
private String transformServiceId(String serviceId) { | ||
// According to User Agent 2.0 spec, replace spaces with underscores | ||
return serviceId.replace(" ", "_"); | ||
} | ||
|
||
private FieldSpec userAgentField() { | ||
return FieldSpec.builder(String.class, "USER_AGENT", Modifier.PUBLIC, Modifier.STATIC, Modifier.FINAL) | ||
.initializer("$S", transformServiceId(model.getMetadata().getServiceId()) + "#" + SDK_VERSION) | ||
.addJavadoc("Returns a user agent containing the service and " | ||
+ "version info") | ||
.build(); | ||
|
||
} | ||
|
||
protected MethodSpec privateConstructor() { | ||
return MethodSpec.constructorBuilder() | ||
.addModifiers(Modifier.PRIVATE) | ||
.build(); | ||
} | ||
|
||
@Override | ||
public ClassName className() { | ||
return poetExtension.getServiceVersionUserAgentClass(); | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...test/java/software/amazon/awssdk/codegen/poet/client/ServiceVersionUserAgentSpecTest.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,58 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"). | ||
* You may not use this file except in compliance with the License. | ||
* A copy of the License is located at | ||
* | ||
* http://aws.amazon.com/apache2.0 | ||
* | ||
* or in the "license" file accompanying this file. This file is distributed | ||
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either | ||
* express or implied. See the License for the specific language governing | ||
* permissions and limitations under the License. | ||
*/ | ||
|
||
package software.amazon.awssdk.codegen.poet.client; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import com.squareup.javapoet.JavaFile; | ||
import com.squareup.javapoet.TypeSpec; | ||
import java.io.InputStream; | ||
import java.util.Scanner; | ||
import org.junit.jupiter.api.Test; | ||
import software.amazon.awssdk.codegen.poet.ClassSpec; | ||
import software.amazon.awssdk.codegen.poet.ClientTestModels; | ||
import software.amazon.awssdk.codegen.poet.client.specs.ServiceVersionUserAgentSpec; | ||
import software.amazon.awssdk.core.util.VersionInfo; | ||
|
||
public class ServiceVersionUserAgentSpecTest { | ||
|
||
// Fixture test that compares generated ServiceVersionUserAgent class against expected output. | ||
// The fixture file uses {{VERSION}} as a placeholder which gets replaced with the current | ||
// SDK version at test time, since the generated code injects the actual version at build time. | ||
@Test | ||
void testServiceVersionUserAgentClass() { | ||
String currVersion = VersionInfo.SDK_VERSION; | ||
ClassSpec serviceVersionUserAgentSpec = new ServiceVersionUserAgentSpec(ClientTestModels.restJsonServiceModels()); | ||
|
||
String expectedContent = loadFixtureFile("test-service-version-user-agent-class.java"); | ||
expectedContent = expectedContent.replace("{{VERSION}}", currVersion); | ||
|
||
String actualContent = generateContent(serviceVersionUserAgentSpec); | ||
|
||
assertThat(actualContent).isEqualToIgnoringWhitespace(expectedContent); | ||
} | ||
|
||
private String loadFixtureFile(String filename) { | ||
InputStream is = getClass().getResourceAsStream("specs/" + filename); | ||
return new Scanner(is).useDelimiter("\\A").next(); | ||
} | ||
|
||
private String generateContent(ClassSpec spec) { | ||
TypeSpec typeSpec = spec.poetSpec(); | ||
JavaFile javaFile = JavaFile.builder(spec.className().packageName(), typeSpec).build(); | ||
return javaFile.toString(); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
...ftware/amazon/awssdk/codegen/poet/client/specs/test-service-version-user-agent-class.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,15 @@ | ||
package software.amazon.awssdk.services.json.internal; | ||
|
||
import java.lang.String; | ||
import software.amazon.awssdk.annotations.Generated; | ||
|
||
@Generated("software.amazon.awssdk:codegen") | ||
public final class ServiceVersionUserAgent { | ||
zoewangg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/** | ||
* Returns a user agent containing the service and version info | ||
*/ | ||
public static final String USER_AGENT = "Json_Service#{{VERSION}}"; | ||
RanVaknin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private ServiceVersionUserAgent() { | ||
} | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.