-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Add convenience layer to generated code for Event Grid Track 2 #14442
Conversation
Autorest regenerated using up to date version for track 2 pom file incorporated to jacoco and dependencies for module handled issue around capitalization of etag discrepancy fixed no test coverage yet
Autorest regenerated using up to date version for track 2 pom file incorporated to jacoco and dependencies for module handled issue around capitalization of etag discrepancy fixed no test coverage yet
Autogenerate EventGrid track 2 classes using the swagger. All files except EventGridPublisherImplTests.java, pom.xml, and the swagger readme.md are autogenerated.
Convenience layer for EventGrid Track 2 on top of autogenerated code.
Add a few missing javadoc comments and remove a few comments from private methods.
test and pom edits mostly
52dcaf8
to
ecb6fe2
Compare
Drop explicit support for non-JSON data. Modify/fix tests slightly.
SharedAccessSignature -> SAS, refactor methods and classes
/azp run |
You have several pipelines (over 10) configured to build pull requests in this repository. Specify which pipelines you would like to run by using /azp run [pipelines] command. You can specify multiple pipelines using a comma separated list. |
/azp run java - eventgrid - ci, java - eventgrid - ci (Build Test Windows - Java 11) |
Azure Pipelines successfully started running 1 pipeline(s). |
conforms to live test expected custom event properties
In order to start sending events, we need an `EventGridPublisherClient`. Here is code to | ||
create the synchronous and the asynchronous versions. Note that a shared access signature can | ||
be used instead of a key in any of these samples by calling the `sharedAccessSignatureCredential` | ||
method instead of `keyCredential'. |
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.
method instead of `keyCredential'. | |
method instead of `keyCredential`. |
public <T> T getData(Class<T> clazz, JsonSerializer dataDeserializer) { | ||
if (!parsed) { | ||
// data was set instead of parsed, throw exception because we don't know how the data relates to clazz | ||
throw new IllegalStateException("This method should only be called on events created through the parse method"); | ||
} | ||
|
||
return dataDeserializer.deserialize(new ByteArrayInputStream((byte[]) this.event.getData()), | ||
TypeReference.createInstance(clazz)); | ||
} |
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.
We should consider adding async overloads for these since dataDeserializer
supports async deserialization.
public EventGridServiceVersion getServiceVersion() { | ||
return EventGridServiceVersion.V2018_01_01; | ||
} |
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.
Instead of hardcoding the version here, the builder (since the builder should have a setter for service version) should pass this to the constructor and the client should use that as an instance variable that will be returned here.
return withContext(context -> publishEvents(events, context)); | ||
} | ||
|
||
Mono<Void> publishEvents(Iterable<EventGridEvent> events, Context context) { |
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.
Why is this called publish
? It's better to use the same terminology even for non-public methods.
List<com.azure.messaging.eventgrid.implementation.models.CloudEvent> implList = new ArrayList<>(); | ||
for (CloudEvent event : events) { | ||
implList.add(event.toImpl()); | ||
} |
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.
Converting to inner types should happen lazily when the Mono
is subscribed. Do the same at other places too where the list operations are performed eagerly.
* | ||
* @return the builder itself. | ||
*/ | ||
public EventGridPublisherClientBuilder keyCredential(AzureKeyCredential credential) { |
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.
The method should be named credential
to be consistent with other SDK builders.
return String.format("%s&%s=%s", unsignedSas, signKey, encodedSignature); | ||
|
||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException e) { | ||
e.printStackTrace(); |
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.
Use logs instead of e.printStackTrace()
.
*/ | ||
public EventGridSasCredential(String sas) { | ||
if (CoreUtils.isNullOrEmpty(sas)) { | ||
throw new IllegalArgumentException("the access signature cannot be null or empty"); |
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.
Log and throw.
import java.util.Locale; | ||
import java.util.Map; | ||
|
||
public final class SystemEventMappings { |
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.
Missing javadoc.
import java.time.ZoneOffset; | ||
import java.util.Random; | ||
|
||
public class ClassTime { |
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.
Add javadocs for all samples to explain what the sample does.
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.
Overall, looks great and it's good to see all design guidelines being followed! Thanks @Soreloser2
import com.azure.core.annotation.Fluent; | ||
import com.azure.core.serializer.json.jackson.JacksonJsonSerializerBuilder; | ||
import com.azure.core.util.CoreUtils; | ||
import com.azure.core.util.serializer.*; |
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.
Generally I think we try to avoid * imports, I know I had to go dive into IntelliJ settings to make it stop doing this by default:
Settings > Editor > Code Style > Java > set both count to use import with '*' settings to 99.
public Object getData() { | ||
if (!parsed) { | ||
// data was set instead of parsed, throw error | ||
throw new IllegalStateException("This method should only be called on events created through the parse method"); |
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.
I think Srikanta already called this out, but instead of throwing directly, we should get a ClientLogger
instance and call throw logger.logExceptionAsWarning/Error(...)
instead.
@@ -0,0 +1,55 @@ | |||
package com.azure.messaging.eventgrid; |
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.
Make sure all source files have the copyright & license header.
Variety of changes, major ones include: - Async methods on generic getData - Lazy list creation for send methods - Credential methods both renamed to `credential` - Logging errors instead of throwing - Additional missing javadoc comments
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.
LGTM! Some minor cosmetic changes required before merging.
import java.io.ByteArrayOutputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.time.OffsetDateTime; | ||
import java.util.*; |
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.
Enumerate all imports instead of using *
. Wonder why this is happening on generated code though. Is this how autorest generated? If that's the case, let me know. I will take a look.
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.
I believe this has to do with my IntelliJ settings, because this is code written over the autogenerated model in implementation. Brandon mentioned this in a different file as well but my settings should be fixed now.
public <T> Mono<T> getDataAsync(Class<T> clazz, JsonSerializer dataDeserializer) { | ||
if (!parsed) { | ||
// data was set instead of parsed, throw exception because we don't know how the data relates to clazz | ||
throw logger.logExceptionAsError(new IllegalStateException( |
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.
For async methods, exceptions should be passed through the publisher. Use FluxUtil.monoError()
.
return Flux.fromIterable(events) | ||
.collectList() | ||
.flatMap(list -> this.impl.publishCustomEventEventsAsync(this.hostname, list, context)); |
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.
Unfortunately, we'll have to iterate through the collection twice - once to convert Iterable to list and then when serializing. This impacts performance. Not sure, if we can avoid this without changing the API to take a List
instead.
return String.format("%s&%s=%s", unsignedSas, signKey, encodedSignature); | ||
|
||
} catch (NoSuchAlgorithmException | UnsupportedEncodingException | InvalidKeyException e) { | ||
throw new RuntimeException(logger.logThrowableAsError(e)); |
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.
throw new RuntimeException(logger.logThrowableAsError(e)); | |
throw logger.logThrowableAsError(new RuntimeException(e)); |
if (CoreUtils.isNullOrEmpty(sas)) { | ||
throw logger.logExceptionAsError(new IllegalArgumentException("the access signature cannot be null or empty")); | ||
} |
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.
https://azure.github.io/azure-sdk/java_implementation.html#java-errors-system-errors
Split the exception into NPE and IllegalArgumentException when the input is null and empty respectively.
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. See License.txt in the project root for | ||
// license information. |
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.
License headers should be the following in all Java files:
https://docs.opensource.microsoft.com/content/releasing/copyright-headers.html#java
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
mostly minor changes, except a small license change to all source files, including generated ones.
This PR is based on #14440, and new commits start at 51b5908.