Skip to content

Commit 342cc8c

Browse files
author
Liudmila Molkova
authored
Revamp RequestOptions - merge with context (Azure#44535)
1 parent b6e2504 commit 342cc8c

File tree

41 files changed

+987
-1321
lines changed

Some content is hidden

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

41 files changed

+987
-1321
lines changed

sdk/clientcore/annotation-processor-test/src/main/java/io/clientcore/annotation/processor/test/implementation/TestInterfaceClientImpl.java

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import io.clientcore.core.http.annotations.QueryParam;
1616
import io.clientcore.core.http.annotations.UnexpectedResponseExceptionDetail;
1717
import io.clientcore.core.http.models.HttpMethod;
18-
import io.clientcore.core.http.models.RequestOptions;
18+
import io.clientcore.core.http.models.RequestContext;
1919
import io.clientcore.core.http.models.Response;
2020
import io.clientcore.core.http.pipeline.HttpPipeline;
2121
import io.clientcore.core.implementation.http.ContentType;
@@ -67,41 +67,41 @@ Response<Foo> getFoo(@PathParam("key") String key, @QueryParam("label") String l
6767
@HeaderParam("Sync-Token") String syncToken);
6868

6969
@HttpRequestInformation(method = HttpMethod.GET, path = "foos", expectedStatusCodes = { 200 })
70-
Response<FooListResult> listFooListResult(@HostParam("uri") String uri, RequestOptions requestOptions);
70+
Response<FooListResult> listFooListResult(@HostParam("uri") String uri, RequestContext requestContext);
7171

7272
@HttpRequestInformation(method = HttpMethod.GET, path = "{nextLink}", expectedStatusCodes = { 200 })
7373
Response<FooListResult> listNextFooListResult(@PathParam(value = "nextLink", encoded = true) String nextLink,
74-
RequestOptions requestOptions);
74+
RequestContext requestContext);
7575

7676
@HttpRequestInformation(method = HttpMethod.GET, path = "foos", expectedStatusCodes = { 200 })
7777
Response<List<Foo>> listFoo(@HostParam("uri") String uri, @QueryParam(value = "tags", multipleQueryParams =
7878
true) List<String> tags, @QueryParam(value = "tags2", multipleQueryParams = true) List<String> tags2,
79-
RequestOptions requestOptions);
79+
RequestContext requestContext);
8080

8181
@HttpRequestInformation(method = HttpMethod.GET, path = "{nextLink}", expectedStatusCodes = { 200 })
8282
Response<List<Foo>> listNextFoo(@PathParam(value = "nextLink", encoded = true) String nextLink,
83-
RequestOptions requestOptions);
83+
RequestContext requestContext);
8484
// HttpClientTests
85-
// Need to add RequestOptions to specify ResponseBodyMode, which is otherwise provided by convenience methods
85+
// Need to add RequestContext to specify ResponseBodyMode, which is otherwise provided by convenience methods
8686
@SuppressWarnings({ "unchecked", "cast" })
8787
@HttpRequestInformation(method = HttpMethod.PUT, path = "put", expectedStatusCodes = {200})
88-
default HttpBinJSON putConvenience(String uri, int putBody, RequestOptions options) {
89-
return putResponse(uri, putBody, options).getValue();
88+
default HttpBinJSON putConvenience(String uri, int putBody, RequestContext requestContext) {
89+
return putResponse(uri, putBody, requestContext).getValue();
9090
}
9191

9292
@HttpRequestInformation(method = HttpMethod.PUT, path = "put", expectedStatusCodes = { 200 })
9393
Response<HttpBinJSON> putResponse(@HostParam("uri") String uri,
94-
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) int putBody, RequestOptions options);
94+
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) int putBody, RequestContext requestContext);
9595

9696
@HttpRequestInformation(method = HttpMethod.POST, path = "stream", expectedStatusCodes = { 200 })
9797
default HttpBinJSON postStreamConvenience(@HostParam("uri") String uri,
98-
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) int putBody, RequestOptions options) {
99-
return postStreamResponse(uri, putBody, options).getValue();
98+
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) int putBody, RequestContext requestContext) {
99+
return postStreamResponse(uri, putBody, requestContext).getValue();
100100
}
101101

102102
@HttpRequestInformation(method = HttpMethod.POST, path = "stream", expectedStatusCodes = { 200 })
103103
Response<HttpBinJSON> postStreamResponse(@HostParam("uri") String uri,
104-
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) int putBody, RequestOptions options);
104+
@BodyParam(ContentType.APPLICATION_OCTET_STREAM) int putBody, RequestContext requestContext);
105105

106106
// Service 1
107107
@HttpRequestInformation(method = HttpMethod.GET, path = "bytes/100", expectedStatusCodes = {200})

sdk/clientcore/annotation-processor-test/src/test/java/io/clientcore/annotation/processor/test/PagingOperationTests.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
import io.clientcore.core.http.models.HttpHeaders;
1111
import io.clientcore.core.http.models.HttpMethod;
1212
import io.clientcore.core.http.models.HttpRequest;
13-
import io.clientcore.core.http.models.RequestOptions;
13+
import io.clientcore.core.http.models.RequestContext;
1414
import io.clientcore.core.http.models.Response;
1515
import io.clientcore.core.http.paging.PagedIterable;
1616
import io.clientcore.core.http.paging.PagedResponse;
@@ -57,7 +57,7 @@ public void testListFoo() {
5757
TestInterfaceClientService testInterface = TestInterfaceClientService.getNewInstance(pipeline);
5858

5959
// Retrieve initial response
60-
Response<List<Foo>> initialResponse = testInterface.listFoo(uri, null, null, RequestOptions.none());
60+
Response<List<Foo>> initialResponse = testInterface.listFoo(uri, null, null, RequestContext.none());
6161

6262
List<Foo> fooFirstPageResponse = initialResponse.getValue();
6363
assertNotNull(fooFirstPageResponse);
@@ -69,7 +69,7 @@ public void testListFoo() {
6969
PagedIterable<Foo> pagedIterable = new PagedIterable<>(
7070
pagingOptions -> firstPage, // First page
7171
(pagingOptions, nextLink) -> {
72-
Response<List<Foo>> nextResponse = testInterface.listNextFoo(nextLink, RequestOptions.none());
72+
Response<List<Foo>> nextResponse = testInterface.listNextFoo(nextLink, RequestContext.none());
7373
return toPagedResponse(nextResponse, nextLink);
7474
}
7575
);
@@ -107,8 +107,8 @@ public void testListFooListResult() {
107107

108108
// Fetch the first page
109109
PagedIterable<Foo> pagedIterable = new PagedIterable<>(
110-
pagingOptions -> toPagedResponse(testInterface.listFooListResult(uri, RequestOptions.none()), nextLinkUri),
111-
(pagingOptions, nextLink) -> toPagedResponse(testInterface.listNextFooListResult(nextLink, RequestOptions.none()), null)
110+
pagingOptions -> toPagedResponse(testInterface.listFooListResult(uri, RequestContext.none()), nextLinkUri),
111+
(pagingOptions, nextLink) -> toPagedResponse(testInterface.listNextFooListResult(nextLink, RequestContext.none()), null)
112112
);
113113

114114
assertNotNull(pagedIterable);

sdk/clientcore/annotation-processor-test/src/test/java/io/clientcore/annotation/processor/test/http/TestInterfaceGenerationTests.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,20 @@
1111
import io.clientcore.core.http.models.HttpHeaders;
1212
import io.clientcore.core.http.models.HttpMethod;
1313
import io.clientcore.core.http.models.HttpRequest;
14-
import io.clientcore.core.http.models.RequestOptions;
14+
import io.clientcore.core.http.models.RequestContext;
1515
import io.clientcore.core.http.models.Response;
1616
import io.clientcore.core.http.paging.PagedIterable;
1717
import io.clientcore.core.http.paging.PagedResponse;
1818
import io.clientcore.core.http.pipeline.HttpPipeline;
1919
import io.clientcore.core.http.pipeline.HttpPipelineBuilder;
2020
import io.clientcore.core.implementation.http.ContentType;
2121
import io.clientcore.core.models.binarydata.BinaryData;
22+
import org.junit.jupiter.api.Named;
23+
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.Arguments;
26+
import org.junit.jupiter.params.provider.MethodSource;
27+
2228
import java.io.ByteArrayInputStream;
2329
import java.io.IOException;
2430
import java.nio.ByteBuffer;
@@ -29,11 +35,6 @@
2935
import java.util.Set;
3036
import java.util.stream.Collectors;
3137
import java.util.stream.Stream;
32-
import org.junit.jupiter.api.Named;
33-
import org.junit.jupiter.api.Test;
34-
import org.junit.jupiter.params.ParameterizedTest;
35-
import org.junit.jupiter.params.provider.Arguments;
36-
import org.junit.jupiter.params.provider.MethodSource;
3738

3839
import static org.junit.jupiter.api.Assertions.assertEquals;
3940
import static org.junit.jupiter.api.Assertions.assertNotNull;
@@ -240,9 +241,9 @@ public void testListFooListResult() {
240241
// Fetch the first page
241242
PagedIterable<Foo> pagedIterable = new PagedIterable<>(
242243
pagingOptions -> toPagedResponse(
243-
testInterface.listFooListResult(uri, RequestOptions.none()), null),
244+
testInterface.listFooListResult(uri, RequestContext.none()), null),
244245
(pagingOptions, nextLink) -> toPagedResponse(
245-
testInterface.listNextFooListResult(nextLink, RequestOptions.none()), nextLink));
246+
testInterface.listNextFooListResult(nextLink, RequestContext.none()), nextLink));
246247

247248
assertNotNull(pagedIterable);
248249
Set<Foo> allItems = pagedIterable.stream().collect(Collectors.toSet());
@@ -283,7 +284,7 @@ public void testListFoo() {
283284
TestInterfaceClientImpl.TestInterfaceClientService.getNewInstance(pipeline);
284285

285286
// Retrieve initial response
286-
Response<List<Foo>> initialResponse = testInterface.listFoo(uri, null, null, RequestOptions.none());
287+
Response<List<Foo>> initialResponse = testInterface.listFoo(uri, null, null, RequestContext.none());
287288

288289
List<Foo> fooFirstPageResponse = initialResponse.getValue();
289290
assertNotNull(fooFirstPageResponse);
@@ -295,7 +296,7 @@ public void testListFoo() {
295296
PagedIterable<Foo> pagedIterable = new PagedIterable<>(pagingOptions -> firstPage, // First page
296297
(pagingOptions, nextLink) -> {
297298
Response<List<Foo>> nextResponse
298-
= testInterface.listNextFoo(nextLink, RequestOptions.none());
299+
= testInterface.listNextFoo(nextLink, RequestContext.none());
299300
return toPagedResponse(nextResponse, nextLink);
300301
});
301302

sdk/clientcore/annotation-processor/README.md

Lines changed: 50 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -73,65 +73,61 @@ The client-core annotation processor for introducing compile-time code generatio
7373
public class ExampleServiceImpl implements ExampleService {
7474
private static final ClientLogger LOGGER = new ClientLogger(TestInterfaceClientService.class);
7575

76-
private final HttpPipeline defaultPipeline;
76+
private final HttpPipeline defaultPipeline;
7777

78-
private final ObjectSerializer serializer;
78+
private final ObjectSerializer serializer;
7979

80-
public ExampleServiceImpl(HttpPipeline defaultPipeline, ObjectSerializer serializer) {
81-
this.defaultPipeline = defaultPipeline;
82-
this.serializer = serializer == null ? new JsonSerializer() : serializer;
83-
}
80+
public ExampleServiceImpl(HttpPipeline defaultPipeline, ObjectSerializer serializer) {
81+
this.defaultPipeline = defaultPipeline;
82+
this.serializer = serializer == null ? new JsonSerializer() : serializer;
83+
}
8484

85-
public static ExampleService getNewInstance(HttpPipeline pipeline, ObjectSerializer serializer) {
86-
return new ExampleServiceImpl(pipeline, serializer);
87-
}
85+
public static ExampleService getNewInstance(HttpPipeline pipeline, ObjectSerializer serializer) {
86+
return new ExampleServiceImpl(pipeline, serializer);
87+
}
8888

89-
public HttpPipeline getPipeline() {
90-
return defaultPipeline;
91-
}
89+
public HttpPipeline getPipeline() {
90+
return defaultPipeline;
91+
}
9292

93-
public Response<BinaryData> getUser(String userId, Context context) {
94-
return getUser(endpoint, apiVersion, userId, context);
95-
}
96-
97-
@Override
98-
private Response<BinaryData> getUser(String endpoint, String apiVersion, String userId, Context context) {
99-
HttpPipeline pipeline = this.getPipeline();
100-
String host = endpoint + "/example/users/" + userId + "?api-version=" + apiVersion;
101-
102-
// create the request
103-
HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, host);
104-
105-
// set the headers
106-
HttpHeaders headers = new HttpHeaders();
107-
httpRequest.setHeaders(headers);
108-
109-
// add RequestOptions to the request
110-
httpRequest.setRequestOptions(requestOptions);
111-
112-
// set the body content if present
113-
114-
// send the request through the pipeline
115-
Response<BinaryData> networkResponse = pipeline.send(httpRequest);
116-
117-
final int responseCode = networkResponse.getStatusCode();
118-
boolean expectedResponse = responseCode == 200;
119-
if (!expectedResponse) {
120-
throw new RuntimeException("Unexpected response code: " + responseCode);
121-
}
122-
ResponseBodyMode responseBodyMode = ResponseBodyMode.IGNORE;
123-
if (requestOptions != null) {
124-
responseBodyMode = requestOptions.getResponseBodyMode();
125-
}
126-
if (responseBodyMode == ResponseBodyMode.DESERIALIZE) {
127-
BinaryData responseBody = response.getBody();
128-
HttpResponseAccessHelper.setValue((HttpResponse<?>) response, responseBody);
129-
} else {
130-
BinaryData responseBody = response.getBody();
131-
HttpResponseAccessHelper.setBodyDeserializer((HttpResponse<?>) response, (body) -> responseBody);
132-
}
133-
return (Response<BinaryData>) response;
134-
}
93+
public Response<BinaryData> getUser(String userId, Context context) {
94+
return getUser(endpoint, apiVersion, userId, context);
95+
}
96+
97+
@Override
98+
private Response<BinaryData> getUser(String endpoint, String apiVersion, String userId, RequestContext requestContext) {
99+
HttpPipeline pipeline = this.getPipeline();
100+
String host = endpoint + "/example/users/" + userId + "?api-version=" + apiVersion;
101+
102+
// create the request
103+
HttpRequest httpRequest = new HttpRequest(HttpMethod.GET, host);
104+
105+
// set the headers
106+
HttpHeaders headers = new HttpHeaders();
107+
httpRequest.setHeaders(headers);
108+
109+
// add RequestContext to the request
110+
httpRequest.setContext(requestContext);
111+
112+
// set the body content if present
113+
114+
// send the request through the pipeline
115+
Response<BinaryData> networkResponse = pipeline.send(httpRequest);
116+
117+
final int responseCode = networkResponse.getStatusCode();
118+
boolean expectedResponse = responseCode == 200;
119+
if (!expectedResponse) {
120+
throw new RuntimeException("Unexpected response code: " + responseCode);
121+
}
122+
123+
try {
124+
networkResponse.close();
125+
} catch (IOException e) {
126+
throw LOGGER.logThrowableAsError(new UncheckedIOException(e));
127+
}
128+
129+
return networkResponse;
130+
}
135131
}
136132
```
137133
This implementation eliminates reflection and integrates directly with your HTTP client infrastructure.

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,8 @@ private void configureInternalMethod(MethodDeclaration internalMethod, HttpReque
253253
initializeHttpRequest(body, method);
254254
setContentType(body, method);
255255
boolean serializationFormatSet = RequestBodyHandler.configureRequestBody(body, method.getBody(), processingEnv);
256-
addRequestOptionsToRequestIfPresent(body, method);
256+
addRequestContextToRequestIfPresent(body, method);
257+
257258
finalizeHttpRequest(body, method.getMethodReturnType(), method, serializationFormatSet);
258259

259260
internalMethod.setBody(body);
@@ -287,19 +288,18 @@ private void writeFile(String packageName, String serviceInterfaceImplShortName,
287288
}
288289
}
289290

290-
private void addRequestOptionsToRequestIfPresent(BlockStmt body, HttpRequestContext method) {
291-
boolean hasRequestOptions = method.getParameters()
291+
private void addRequestContextToRequestIfPresent(BlockStmt body, HttpRequestContext method) {
292+
boolean hasRequestContext = method.getParameters()
292293
.stream()
293-
.anyMatch(parameter -> "requestOptions".equals(parameter.getName())
294-
&& "RequestOptions".equals(parameter.getShortTypeName()));
294+
.anyMatch(parameter -> "requestContext".equals(parameter.getName())
295+
&& "RequestContext".equals(parameter.getShortTypeName()));
295296

296-
if (hasRequestOptions) {
297+
if (hasRequestContext) {
297298
// Create a statement for setting request options
298-
Statement statement1 = StaticJavaParser
299-
.parseStatement("if (requestOptions != null) httpRequest.setRequestOptions(requestOptions);");
300-
statement1.setComment(new LineComment("\n Set the Request Options"));
301-
Statement statement2 = StaticJavaParser.parseStatement(
302-
"if (httpRequest.getRequestOptions() != null) httpRequest.getRequestOptions().getRequestCallback().accept(httpRequest);");
299+
Statement statement1 = StaticJavaParser.parseStatement("httpRequest.setContext(requestContext);");
300+
301+
Statement statement2
302+
= StaticJavaParser.parseStatement("httpRequest.getContext().getRequestCallback().accept(httpRequest);");
303303

304304
body.addStatement(statement1);
305305
body.addStatement(statement2);

sdk/clientcore/core/checkstyle-suppressions.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,5 @@
2424
<suppress files="io.clientcore.core.serialization.json.JsonWriter.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
2525
<suppress files="io.clientcore.core.serialization.json.implementation.StringBuilderWriter.java" checks="com.azure.tools.checkstyle.checks.ThrowFromClientLoggerCheck" />
2626
<suppress files="io.clientcore.core.serialization.json.implementation.JsonUtils.java" checks="com.azure.tools.checkstyle.checks.UseCaughtExceptionCauseCheck" />
27+
<suppress files="io.clientcore.core.http.models.RequestContext.java" checks="MethodName" />
2728
</suppressions>

sdk/clientcore/core/spotbugs-exclude.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,7 @@
241241
<Class name="io.clientcore.core.implementation.instrumentation.fallback.FallbackInstrumentationTests" />
242242
<Class name="io.clientcore.core.serialization.json.JsonWriterTests" />
243243
<Class name="io.clientcore.core.serialization.json.models.JsonPatchDocumentTests" />
244+
<Class name="io.clientcore.core.http.models.RequestContextTests" />
244245
</Or>
245246
</Match>
246247
<Match>

sdk/clientcore/core/src/main/java/io/clientcore/core/http/models/HttpRequest.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public HttpRequest setTryCount(HttpRequest httpRequest, int tryCount) {
4242
private HttpHeaders headers;
4343
private BinaryData body;
4444
private ServerSentEventListener serverSentEventListener;
45-
private RequestOptions requestOptions;
45+
private RequestContext requestContext;
4646
private int tryCount;
4747

4848
/**
4949
* Create a new {@link HttpRequest} instance.
5050
*/
5151
public HttpRequest() {
5252
this.headers = new HttpHeaders();
53-
this.requestOptions = RequestOptions.none();
53+
this.requestContext = RequestContext.none();
5454
}
5555

5656
/**
@@ -165,22 +165,22 @@ public HttpRequest setBody(BinaryData content) {
165165
}
166166

167167
/**
168-
* Get the request {@link RequestOptions options}.
168+
* Get the request context. If no context was provided, {@link RequestContext#none()} is returned.
169169
*
170-
* @return The request {@link RequestOptions options}.
170+
* @return The {@link RequestContext}.
171171
*/
172-
public RequestOptions getRequestOptions() {
173-
return requestOptions;
172+
public RequestContext getContext() {
173+
return requestContext;
174174
}
175175

176176
/**
177-
* Set the request {@link RequestOptions options}.
177+
* Set the request context.
178178
*
179-
* @param requestOptions The request {@link RequestOptions options}.
179+
* @param requestContext The {@link RequestContext}.
180180
* @return The updated {@link HttpRequest}.
181181
*/
182-
public HttpRequest setRequestOptions(RequestOptions requestOptions) {
183-
this.requestOptions = requestOptions;
182+
public HttpRequest setContext(RequestContext requestContext) {
183+
this.requestContext = requestContext == null ? RequestContext.none() : requestContext;
184184
return this;
185185
}
186186

0 commit comments

Comments
 (0)