Skip to content

Commit 79b4ce5

Browse files
authored
API updates for ClientCore based on review part 1 (Azure#44188)
API updates for ClientCore based on review part 1
1 parent c95e739 commit 79b4ce5

File tree

151 files changed

+1999
-3258
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+1999
-3258
lines changed

sdk/clientcore/annotation-processor/src/main/java/io/clientcore/annotation/processor/templating/JavaParserTemplateProcessor.java

Lines changed: 35 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@
3636
import javax.annotation.processing.ProcessingEnvironment;
3737
import java.io.IOException;
3838
import java.io.Writer;
39+
import java.lang.reflect.Field;
3940
import java.nio.ByteBuffer;
41+
import java.util.HashMap;
4042
import java.util.Map;
4143
import java.util.stream.Collectors;
4244

@@ -46,6 +48,29 @@
4648
* This class generates the implementation of the service interface.
4749
*/
4850
public class JavaParserTemplateProcessor implements TemplateProcessor {
51+
private static final Map<String, String> LOWERCASE_HEADER_TO_HTTPHEADENAME_CONSTANT;
52+
53+
static {
54+
LOWERCASE_HEADER_TO_HTTPHEADENAME_CONSTANT = new HashMap<>();
55+
for (Field field : HttpHeaderName.class.getDeclaredFields()) {
56+
// Only inspect public static final fields (aka, constants)
57+
if (!java.lang.reflect.Modifier.isPublic(field.getModifiers())
58+
|| !java.lang.reflect.Modifier.isStatic(field.getModifiers())
59+
|| !java.lang.reflect.Modifier.isFinal(field.getModifiers())) {
60+
continue;
61+
}
62+
63+
String constantName = field.getName();
64+
HttpHeaderName httpHeaderName = null;
65+
try {
66+
httpHeaderName = (HttpHeaderName) field.get(null);
67+
} catch (IllegalAccessException e) {
68+
throw new RuntimeException(e);
69+
}
70+
71+
LOWERCASE_HEADER_TO_HTTPHEADENAME_CONSTANT.put(httpHeaderName.getCaseInsensitiveName(), constantName);
72+
}
73+
}
4974

5075
/**
5176
* Initializes a new instance of the {@link JavaParserTemplateProcessor} class.
@@ -268,8 +293,9 @@ private void initializeHttpRequest(BlockStmt body, HttpRequestContext method) {
268293
body.tryAddImportToParentCompilationUnit(HttpMethod.class);
269294

270295
body.addStatement(StaticJavaParser.parseStatement("String host = " + method.getHost() + ";"));
271-
Statement statement = StaticJavaParser.parseStatement(
272-
"HttpRequest httpRequest = new HttpRequest(HttpMethod." + method.getHttpMethod() + ", host);");
296+
Statement statement
297+
= StaticJavaParser.parseStatement("HttpRequest httpRequest = new HttpRequest().setMethod(HttpMethod."
298+
+ method.getHttpMethod() + ").setUri(host);");
273299
statement.setLineComment("Create the HTTP request");
274300
body.addStatement(statement);
275301
}
@@ -282,35 +308,23 @@ private void addHeadersToRequest(BlockStmt body, HttpRequestContext method) {
282308
body.tryAddImportToParentCompilationUnit(HttpHeaders.class);
283309
body.tryAddImportToParentCompilationUnit(HttpHeaderName.class);
284310

285-
Statement statement = StaticJavaParser.parseStatement("HttpHeaders headers = new HttpHeaders();");
286-
statement.setLineComment("Set the headers");
287-
body.addStatement(statement);
288311
for (Map.Entry<String, String> header : method.getHeaders().entrySet()) {
289-
String enumHeaderKey = header.getKey().toUpperCase().replace("-", "_");
290-
boolean isEnumExists = false;
291-
for (HttpHeaderName httpHeaderName : HttpHeaderName.values()) {
292-
if (httpHeaderName.getCaseInsensitiveName().equals(header.getKey().toLowerCase())) {
293-
isEnumExists = true;
294-
break;
295-
}
296-
}
297-
298312
boolean isStringType = method.getParameters()
299313
.stream()
300314
.anyMatch(parameter -> parameter.getName().equals(header.getValue())
301315
&& "String".equals(parameter.getShortTypeName()));
302316
String value = isStringType ? header.getValue() : "String.valueOf(" + header.getValue() + ")";
303317

304-
if (isEnumExists) {
305-
body.addStatement(StaticJavaParser
306-
.parseStatement("headers.add(HttpHeaderName." + enumHeaderKey + ", " + value + ");"));
307-
} else {
318+
String constantName = LOWERCASE_HEADER_TO_HTTPHEADENAME_CONSTANT.get(header.getKey().toLowerCase());
319+
if (constantName != null) {
308320
body.addStatement(StaticJavaParser.parseStatement(
309-
"headers.add(HttpHeaderName.fromString(\"" + header.getKey() + "\"), " + value + ");"));
321+
"httpRequest.getHeaders().add(HttpHeaderName." + constantName + ", " + value + ");"));
322+
} else {
323+
body.addStatement(
324+
StaticJavaParser.parseStatement("httpRequest.getHeaders().add(HttpHeaderName.fromString(\""
325+
+ header.getKey() + "\"), " + value + ");"));
310326
}
311327
}
312-
313-
body.addStatement(StaticJavaParser.parseStatement("httpRequest.setHeaders(headers);"));
314328
}
315329

316330
private void addRequestBody(BlockStmt body, HttpRequestContext method) {

sdk/clientcore/annotation-processor/src/test/java/io/clientcore/annotation/processor/models/TemplateInputTest.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ void setAndGetUnexpectedResponseExceptionDetails() {
9797
}
9898

9999
private static final class MockUnexpectedResponseExceptionDetail implements UnexpectedResponseExceptionDetail {
100-
@Override
101-
public String exceptionTypeName() {
102-
return "MockException";
103-
}
104-
105100
@Override
106101
public int[] statusCode() {
107102
return new int[0];

sdk/clientcore/core/checkstyle-suppressions.xml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,16 @@
1919
<suppress files="io.clientcore.core.implementation.utils.ImplUtils.java" checks="MissingJavadocTypeCheck" />
2020
<suppress files="io.clientcore.core.implementation.instrumentation.Slf4jLoggerShim.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
2121
<suppress files="io.clientcore.core.implementation.utils.EnvironmentConfiguration.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
22-
<suppress files="io.clientcore.core.utils.Union.java" checks="com.azure.tools.checkstyle.checks.EnforceFinalFieldsCheck" />
2322
<suppress files="io.clientcore.core.implementation.ReflectionSerializable.java" checks="com.azure.tools.checkstyle.checks.JavadocThrowsChecks" />
2423
<suppress files="io.clientcore.core.implementation.http.serializer.HttpResponseBodyDecoder.java" checks="com.azure.tools.checkstyle.checks.JavadocThrowsChecks" />
25-
<suppress files="io.clientcore.core.http.client.DefaultHttpClientBuilder.java" checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" />
24+
<suppress files="io.clientcore.core.http.client.JdkHttpClientBuilder.java" checks="com.azure.tools.checkstyle.checks.ServiceClientBuilderCheck" />
2625
<suppress files="io.clientcore.core.http.pipeline.HttpInstrumentationPolicy.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
2726
<suppress files="io.clientcore.core.implementation.MethodHandleReflectiveInvoker.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
2827
<suppress files="io.clientcore.core.implementation.http.rest.LengthValidatingInputStream.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
28+
<suppress files="io.clientcore.core.instrumentation.logging.LoggingEvent.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
2929
<suppress files="io.clientcore.core.serialization.json.JsonReader.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
3030
<suppress files="io.clientcore.core.serialization.json.JsonWriteContext.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
31-
<suppress files="io.clientcore.core.serialization.json.implementation.DefaultJsonReader.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
32-
<suppress files="io.clientcore.core.serialization.json.implementation.DefaultJsonWriter.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
31+
<suppress files="io.clientcore.core.serialization.json.JsonWriter.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
3332
<suppress files="io.clientcore.core.serialization.json.implementation.StringBuilderWriter.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
3433
<suppress files="io.clientcore.core.serialization.json.models.JsonNumber.java" checks="com.azure.tools.checkstyle.checks.UseCaughtExceptionCauseCheck" />
3534
</suppressions>

sdk/clientcore/core/spotbugs-exclude.xml

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
<Class name="io.clientcore.core.http.pipeline.HttpPipelineBuilder" />
1919
<Class name="io.clientcore.core.http.pipeline.HttpPipelineNextPolicy" />
2020
<Class name="io.clientcore.core.http.pipeline.HttpRetryPolicy" />
21+
<Class name="io.clientcore.core.implementation.GenericParameterizedType" />
2122
<Class name="io.clientcore.core.implementation.ReflectionUtilsMethodHandle" />
2223
<Class name="io.clientcore.core.implementation.http.HttpPipelineCallState" />
2324
<Class name="io.clientcore.core.implementation.http.rest.PercentEscaper" />
@@ -27,8 +28,6 @@
2728
<Class name="io.clientcore.core.implementation.http.rest.ResponseConstructorsNoCacheReflection" />
2829
<Class name="io.clientcore.core.implementation.http.rest.ResponseExceptionConstructorCache" />
2930
<Class name="io.clientcore.core.implementation.http.rest.RestProxyImpl" />
30-
<Class name="io.clientcore.core.implementation.http.serializer.CompositeSerializer" />
31-
<Class name="io.clientcore.core.utils.DateTimeRfc1123" />
3231
<Class name="io.clientcore.core.implementation.utils.InternalContext" />
3332
<Class name="io.clientcore.core.implementation.utils.JsonSerializer" />
3433
<Class name="io.clientcore.core.implementation.utils.Providers" />
@@ -37,7 +36,7 @@
3736
<Class name="io.clientcore.core.implementation.utils.XmlSerializer" />
3837
<Class name="io.clientcore.core.serialization.xml.XmlReader" />
3938
<Class name="io.clientcore.core.shared.HttpClientTests" />
40-
<Class name="io.clientcore.core.implementation.GenericParameterizedType" />
39+
<Class name="io.clientcore.core.utils.DateTimeRfc1123" />
4140
<Class name="io.clientcore.core.utils.SharedExecutorService" />
4241
<Class name="io.clientcore.core.utils.Union" />
4342
<Class name="io.clientcore.core.utils.binarydata.FileBinaryData" />
@@ -46,15 +45,11 @@
4645
<Class name="io.clientcore.core.utils.configuration.Configuration" />
4746
</Or>
4847
</Match>
49-
<Match>
50-
<Bug pattern="BC_VACUOUS_INSTANCEOF" />
51-
<Class name="io.clientcore.core.http.pipeline.HttpRetryPolicy" />
52-
</Match>
5348
<Match>
5449
<Bug pattern="CNT_ROUGH_CONSTANT_VALUE" />
5550
<Or>
5651
<Class name="io.clientcore.core.implementation.serializer.BinaryDataSerializationTests" />
57-
<Class name="io.clientcore.core.serialization.json.contract.JsonReaderContractTests" />
52+
<Class name="io.clientcore.core.serialization.json.JsonReaderTests" />
5853
</Or>
5954
</Match>
6055
<Match>
@@ -69,9 +64,7 @@
6964
<Bug pattern="CT_CONSTRUCTOR_THROW" />
7065
<Or>
7166
<Class name="io.clientcore.core.credentials.KeyCredential" />
72-
<Class name="io.clientcore.core.http.client.DefaultHttpClient" />
73-
<Class name="io.clientcore.core.http.client.DefaultHttpClientBuilder" />
74-
<Class name="io.clientcore.core.http.models.HttpRequest" />
67+
<Class name="io.clientcore.core.http.client.JdkHttpClientBuilder" />
7568
<Class name="io.clientcore.core.http.pipeline.KeyCredentialPolicy" />
7669
<Class name="io.clientcore.core.implementation.http.rest.SwaggerMethodParser" />
7770
<Class name="io.clientcore.core.implementation.instrumentation.otel.OTelInstrumentation" />
@@ -93,17 +86,12 @@
9386
</Match>
9487
<Match>
9588
<Bug pattern="DMI_RANDOM_USED_ONLY_ONCE" />
96-
<Or>
97-
<Class name="io.clientcore.core.http.pipeline.HttpPipelinePolicyTests$SyncPolicy" />
98-
<Class name="io.clientcore.core.utils.TestUtils" />
99-
</Or>
89+
<Class name="io.clientcore.core.utils.TestUtils" />
10090
</Match>
10191
<Match>
10292
<Bug pattern="DM_CONVERT_CASE" />
10393
<Or>
104-
<Class name="io.clientcore.core.http.exceptions.HttpExceptionType" />
10594
<Class name="io.clientcore.core.http.models.HttpHeaderName" />
106-
<Class name="io.clientcore.core.http.pipeline.HttpPipelineBuilder" />
10795
<Class name="io.clientcore.core.utils.ServerSentEventUtils" />
10896
<Class name="io.clientcore.core.utils.auth.AuthUtils" />
10997
</Or>
@@ -112,9 +100,8 @@
112100
<Bug pattern="DM_DEFAULT_ENCODING" />
113101
<Or>
114102
<Class name="io.clientcore.core.http.RestProxyTests" />
115-
<Class name="io.clientcore.core.http.client.DefaultHttpClientIT" />
116103
<Class name="io.clientcore.core.http.client.SimpleBasicAuthHttpProxyServer" />
117-
<Class name="io.clientcore.core.http.pipeline.HttpPipelinePolicyTests$SyncPolicy" />
104+
<Class name="io.clientcore.core.implementation.http.client.DefaultHttpClientIT" />
118105
<Class name="io.clientcore.core.implementation.http.rest.RestProxyImplTests" />
119106
<Class name="io.clientcore.core.implementation.serializer.AdditionalPropertiesSerializerTests" />
120107
<Class name="io.clientcore.core.implementation.serializer.BinaryDataSerializationTests" />
@@ -124,8 +111,8 @@
124111
<Class name="io.clientcore.core.implementation.serializer.JsonSerializableEndToEndTests" />
125112
<Class name="io.clientcore.core.instrumentation.logging.ClientLoggerTests" />
126113
<Class name="io.clientcore.core.instrumentation.logging.InstrumentationTestUtils" />
127-
<Class name="io.clientcore.core.serialization.json.implementation.DefaultJsonReader" />
128-
<Class name="io.clientcore.core.serialization.json.implementation.DefaultJsonWriter" />
114+
<Class name="io.clientcore.core.serialization.json.JsonReader" />
115+
<Class name="io.clientcore.core.serialization.json.JsonWriter" />
129116
<Class name="io.clientcore.core.serialization.xml.XmlReader" />
130117
<Class name="io.clientcore.core.shared.HttpClientTests" />
131118
<Class name="io.clientcore.core.shared.HttpClientTestsServer" />
@@ -154,7 +141,7 @@
154141
</Match>
155142
<Match>
156143
<Bug pattern="ES_COMPARING_STRINGS_WITH_EQ" />
157-
<Class name="io.clientcore.core.serialization.json.contract.models.JsonStringContractTests" />
144+
<Class name="io.clientcore.core.serialization.json.models.JsonStringTests" />
158145
</Match>
159146
<Match>
160147
<Bug pattern="HARD_CODE_PASSWORD" />
@@ -236,10 +223,9 @@
236223
<Match>
237224
<Bug pattern="NP_NULL_PARAM_DEREF_ALL_TARGETS_DANGEROUS" />
238225
<Or>
239-
<Class name="io.clientcore.core.serialization.json.contract.JsonWriterContractTests" />
240226
<Class name="io.clientcore.core.implementation.instrumentation.fallback.FallbackInstrumentationTests" />
227+
<Class name="io.clientcore.core.serialization.json.JsonWriterTests" />
241228
</Or>
242-
243229
</Match>
244230
<Match>
245231
<Bug pattern="NP_NULL_PARAM_DEREF_NONVIRTUAL" />
@@ -273,10 +259,10 @@
273259
<Match>
274260
<Bug pattern="PZLA_PREFER_ZERO_LENGTH_ARRAYS" />
275261
<Or>
276-
<Class name="io.clientcore.core.utils.Base64Uri" />
277262
<Class name="io.clientcore.core.implementation.utils.JsonSerializer" />
278263
<Class name="io.clientcore.core.implementation.utils.XmlSerializer" />
279-
<Class name="io.clientcore.core.serialization.json.implementation.DefaultJsonReader" />
264+
<Class name="io.clientcore.core.serialization.json.JsonReader" />
265+
<Class name="io.clientcore.core.utils.Base64Uri" />
280266
<Class name="io.clientcore.core.utils.TestUtils" />
281267
<Class name="io.clientcore.core.utils.serializers.MockSerializer" />
282268
</Or>
@@ -291,11 +277,7 @@
291277
</Match>
292278
<Match>
293279
<Bug pattern="RV_RETURN_VALUE_IGNORED_BAD_PRACTICE" />
294-
<Class name="io.clientcore.core.http.client.implementation.InputStreamTimeoutResponseSubscriber" />
295-
</Match>
296-
<Match>
297-
<Bug pattern="RV_RETURN_VALUE_IGNORED_INFERRED" />
298-
<Class name="io.clientcore.core.utils.UnionTests" />
280+
<Class name="io.clientcore.core.implementation.http.client.InputStreamTimeoutResponseSubscriber" />
299281
</Match>
300282
<Match>
301283
<Bug pattern="RV_RETURN_VALUE_IGNORED_NO_SIDE_EFFECT" />
@@ -311,10 +293,7 @@
311293
</Match>
312294
<Match>
313295
<Bug pattern="SE_BAD_FIELD" />
314-
<Or>
315-
<Class name="io.clientcore.core.http.exceptions.HttpResponseException" />
316-
<Class name="io.clientcore.core.serialization.json.implementation.jackson.core.util.RequestPayload" />
317-
</Or>
296+
<Class name="io.clientcore.core.serialization.json.implementation.jackson.core.util.RequestPayload" />
318297
</Match>
319298
<Match>
320299
<Bug pattern="SE_NO_SERIALVERSIONID" />
@@ -347,7 +326,6 @@
347326
<Match>
348327
<Bug pattern="SIC_INNER_SHOULD_BE_STATIC_ANON" />
349328
<Or>
350-
<Class name="io.clientcore.core.http.pipeline.HttpPipelinePolicyTests" />
351329
<Class name="io.clientcore.core.http.pipeline.HttpPipelineTests" />
352330
<Class name="io.clientcore.core.http.pipeline.RedirectPolicyTest" />
353331
<Class name="io.clientcore.core.http.pipeline.RetryPolicyTests" />
@@ -370,9 +348,8 @@
370348
<Match>
371349
<Bug pattern="UWF_FIELD_NOT_INITIALIZED_IN_CONSTRUCTOR" />
372350
<Or>
373-
<Class name="io.clientcore.core.http.client.implementation.InputStreamTimeoutResponseSubscriber" />
351+
<Class name="io.clientcore.core.implementation.http.client.InputStreamTimeoutResponseSubscriber" />
374352
<Class name="io.clientcore.core.implementation.http.rest.ResponseConstructorsCacheBenchMark" />
375-
<Class name="io.clientcore.core.serialization.json.implementation.DefaultJsonWriterContractTests" />
376353
<Class name="io.clientcore.core.serialization.xml.DefaultXmlWriterContractTests" />
377354
<Class name="io.clientcore.core.serialization.xml.implementation.aalto.in.ReaderConfig$EncodingContext" />
378355
<Class name="io.clientcore.core.utils.binarydata.BinaryDataTest$BinaryDataAsProperty" />
@@ -407,7 +384,7 @@
407384
</Match>
408385
<Match>
409386
<Bug pattern="WMI_WRONG_MAP_ITERATOR" />
410-
<Class name="io.clientcore.core.serialization.json.contract.JsonReaderContractTests" />
387+
<Class name="io.clientcore.core.serialization.json.JsonReaderTests" />
411388
</Match>
412389
<Match>
413390
<Bug pattern="XXE_XMLSTREAMREADER" />

sdk/clientcore/core/src/main/java/io/clientcore/core/http/annotations/UnexpectedResponseExceptionDetail.java

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
package io.clientcore.core.http.annotations;
55

6-
import io.clientcore.core.http.exceptions.HttpExceptionType;
76
import io.clientcore.core.http.exceptions.HttpResponseException;
87

98
import java.lang.annotation.Repeatable;
@@ -14,9 +13,8 @@
1413
import static java.lang.annotation.RetentionPolicy.RUNTIME;
1514

1615
/**
17-
* The {@link HttpExceptionType} that is thrown or returned when one of the status codes is returned from a REST API. Multiple
18-
* annotations can be used. When no codes are listed that exception is always thrown or returned if it is reached
19-
* during evaluation, this should be treated as a default case. If no default case is annotated the fall through
16+
* Multiple annotations can be used. When no codes are listed that exception is always thrown or returned if it is
17+
* reached during evaluation, this should be treated as a default case. If no default case is annotated the fall through
2018
* exception is {@link HttpResponseException}.
2119
*
2220
* <p><strong>Example:</strong></p>
@@ -56,14 +54,6 @@
5654
@Target(METHOD)
5755
@Repeatable(UnexpectedResponseExceptionDetails.class)
5856
public @interface UnexpectedResponseExceptionDetail {
59-
/**
60-
* The name of the {@link HttpExceptionType} of an {@link HttpResponseException} that should be thrown/returned when
61-
* the API returns an unrecognized status code.
62-
*
63-
* @return The {@link HttpExceptionType} that should be thrown/returned, represented as a {@link String}.
64-
*/
65-
String exceptionTypeName() default "";
66-
6757
/**
6858
* HTTP status codes which trigger the {@link HttpResponseException} to be thrown or returned. If no status codes
6959
* are listed the exception is always thrown or returned.

0 commit comments

Comments
 (0)