Skip to content
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

Fix(pagination): Fix paging APIs - Regenerate from swagger. #15860

Merged
merged 3 commits into from
Oct 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sdk/digitaltwins/azure-digitaltwins-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## 1.0.0-beta.3 (Unreleased)

- Fixed issue with pagination APIs that support max-item-count where the item count was not respected from the second page forward.

## 1.0.0-beta.2 (2020-09-24)

Expand Down
2 changes: 1 addition & 1 deletion sdk/digitaltwins/azure-digitaltwins-core/autorest.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ input-file: https://raw.githubusercontent.com/Azure/azure-rest-api-specs/master/

output-folder: "./"
license-header: MICROSOFT_MIT_SMALL
use: '@autorest/java@4.0.2'
use: '@autorest/java@4.0.3'
java:
add-context-parameter: true
namespace: com.azure.digitaltwins.core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ public <T> PagedFlux<T> listRelationships(String digitalTwinId, Class<T> clazz)
public <T> PagedFlux<T> listRelationships(String digitalTwinId, String relationshipName, Class<T> clazz) {
return new PagedFlux<>(
() -> withContext(context -> listRelationshipsFirstPage(digitalTwinId, relationshipName, clazz, context)),
nextLink -> withContext(context -> listRelationshipsNextPage(this.protocolLayer.getHost() + nextLink, clazz, context)));
nextLink -> withContext(context -> listRelationshipsNextPage(nextLink, clazz, context)));
}

<T> Mono<PagedResponse<T>> listRelationshipsFirstPage(String digitalTwinId, String relationshipName, Class<T> clazz, Context context) {
Expand Down Expand Up @@ -520,7 +520,7 @@ <T> Mono<PagedResponse<T>> listRelationshipsNextPage(String nextLink, Class<T> c
<T> PagedFlux<T> listRelationships(String digitalTwinId, String relationshipName, Class<T> clazz, Context context) {
return new PagedFlux<>(
() -> listRelationshipsFirstPage(digitalTwinId, relationshipName, clazz, context),
nextLink -> listRelationshipsNextPage(this.protocolLayer.getHost() + nextLink, clazz, context));
nextLink -> listRelationshipsNextPage(nextLink, clazz, context));
}

/**
Expand All @@ -533,13 +533,13 @@ <T> PagedFlux<T> listRelationships(String digitalTwinId, String relationshipName
public PagedFlux<IncomingRelationship> listIncomingRelationships(String digitalTwinId) {
return new PagedFlux<>(
() -> withContext(context -> listIncomingRelationshipsFirstPageAsync(digitalTwinId, context)),
nextLink -> withContext(context -> listIncomingRelationshipsNextSinglePageAsync(this.protocolLayer.getHost() + nextLink, context)));
nextLink -> withContext(context -> listIncomingRelationshipsNextSinglePageAsync(nextLink, context)));
}

PagedFlux<IncomingRelationship> listIncomingRelationships(String digitalTwinId, Context context) {
return new PagedFlux<>(
() -> listIncomingRelationshipsFirstPageAsync(digitalTwinId, context),
nextLink -> listIncomingRelationshipsNextSinglePageAsync(this.protocolLayer.getHost() + nextLink, context));
nextLink -> listIncomingRelationshipsNextSinglePageAsync(nextLink, context));
}

Mono<PagedResponse<IncomingRelationship>> listIncomingRelationshipsFirstPageAsync(String digitalTwinId, Context context){
Expand Down Expand Up @@ -668,13 +668,13 @@ public PagedFlux<DigitalTwinsModelData> listModels() {
public PagedFlux<DigitalTwinsModelData> listModels(ModelsListOptions modelsListOptions) {
return new PagedFlux<>(
() -> withContext(context -> listModelsSinglePageAsync(modelsListOptions, context)),
nextLink -> withContext(context -> listModelsNextSinglePageAsync(this.protocolLayer.getHost() + nextLink, context)));
nextLink -> withContext(context -> listModelsNextSinglePageAsync(nextLink, modelsListOptions, context)));
}

PagedFlux<DigitalTwinsModelData> listModels(ModelsListOptions modelsListOptions, Context context){
return new PagedFlux<>(
() -> listModelsSinglePageAsync(modelsListOptions, context),
nextLink -> listModelsNextSinglePageAsync(this.protocolLayer.getHost() + nextLink, context));
nextLink -> listModelsNextSinglePageAsync(nextLink, modelsListOptions, context));
}

Mono<PagedResponse<DigitalTwinsModelData>> listModelsSinglePageAsync(ModelsListOptions modelsListOptions, Context context){
Expand All @@ -700,21 +700,24 @@ Mono<PagedResponse<DigitalTwinsModelData>> listModelsSinglePageAsync(ModelsListO
);
}

Mono<PagedResponse<DigitalTwinsModelData>> listModelsNextSinglePageAsync(String nextLink, Context context){
return protocolLayer.getDigitalTwinModels().listNextSinglePageAsync(nextLink, context)
Mono<PagedResponse<DigitalTwinsModelData>> listModelsNextSinglePageAsync(String nextLink, ModelsListOptions modelsListOptions, Context context){
return protocolLayer.getDigitalTwinModels().listNextSinglePageAsync(
nextLink,
new DigitalTwinModelsListOptions().setMaxItemCount(modelsListOptions.getMaxItemCount()),
context)
.map(objectPagedResponse -> {
List<DigitalTwinsModelData> convertedList = objectPagedResponse.getValue().stream()
.map(ModelDataConverter::map)
.filter(Objects::nonNull)
.collect(Collectors.toList());
return new PagedResponseBase<>(
objectPagedResponse.getRequest(),
objectPagedResponse.getStatusCode(),
objectPagedResponse.getHeaders(),
convertedList,
objectPagedResponse.getContinuationToken(),
((PagedResponseBase)objectPagedResponse).getDeserializedHeaders());
});
List<DigitalTwinsModelData> convertedList = objectPagedResponse.getValue().stream()
.map(ModelDataConverter::map)
.filter(Objects::nonNull)
.collect(Collectors.toList());
return new PagedResponseBase<>(
objectPagedResponse.getRequest(),
objectPagedResponse.getStatusCode(),
objectPagedResponse.getHeaders(),
convertedList,
objectPagedResponse.getContinuationToken(),
((PagedResponseBase)objectPagedResponse).getDeserializedHeaders());
});
}

/**
Expand Down Expand Up @@ -1048,14 +1051,14 @@ public PagedFlux<EventRoute> listEventRoutes(EventRoutesListOptions options)
{
return new PagedFlux<>(
() -> withContext(context -> listEventRoutesFirstPage(options, context)),
nextLink -> withContext(context -> listEventRoutesNextPage(this.protocolLayer.getHost() + nextLink, context)));
nextLink -> withContext(context -> listEventRoutesNextPage(nextLink, options, context)));
}

PagedFlux<EventRoute> listEventRoutes(EventRoutesListOptions options, Context context)
{
return new PagedFlux<>(
() -> listEventRoutesFirstPage(options, context),
nextLink -> listEventRoutesNextPage(this.protocolLayer.getHost() + nextLink, context));
nextLink -> listEventRoutesNextPage(nextLink, options, context));
}

Mono<PagedResponse<EventRoute>> listEventRoutesFirstPage(EventRoutesListOptions options, Context context) {
Expand All @@ -1065,10 +1068,10 @@ Mono<PagedResponse<EventRoute>> listEventRoutesFirstPage(EventRoutesListOptions
.map(pagedEventRouteMappingFunction);
}

Mono<PagedResponse<EventRoute>> listEventRoutesNextPage(String nextLink, Context context) {
Mono<PagedResponse<EventRoute>> listEventRoutesNextPage(String nextLink, EventRoutesListOptions options, Context context) {
return protocolLayer
.getEventRoutes()
.listNextSinglePageAsync(nextLink, context)
.listNextSinglePageAsync(nextLink, EventRouteListOptionsConverter.map(options), context)
.map(pagedEventRouteMappingFunction);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,11 @@ public EventRoutesImpl getEventRoutes() {
return this.eventRoutes;
}

/** Initializes an instance of AzureDigitalTwinsAPI client. */
/**
* Initializes an instance of AzureDigitalTwinsAPI client.
*
* @param host server parameter.
*/
AzureDigitalTwinsAPIImpl(String host) {
this(
new HttpPipelineBuilder()
Expand All @@ -124,6 +128,7 @@ public EventRoutesImpl getEventRoutes() {
* Initializes an instance of AzureDigitalTwinsAPI client.
*
* @param httpPipeline The HTTP pipeline to send requests through.
* @param host server parameter.
*/
AzureDigitalTwinsAPIImpl(HttpPipeline httpPipeline, String host) {
this(httpPipeline, JacksonAdapter.createDefaultSerializerAdapter(), host);
Expand All @@ -134,6 +139,7 @@ public EventRoutesImpl getEventRoutes() {
*
* @param httpPipeline The HTTP pipeline to send requests through.
* @param serializerAdapter The serializer to serialize an object into a string.
* @param host server parameter.
*/
AzureDigitalTwinsAPIImpl(HttpPipeline httpPipeline, SerializerAdapter serializerAdapter, String host) {
this.httpPipeline = httpPipeline;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,37 @@
package com.azure.digitaltwins.core.implementation;

import com.azure.core.annotation.ServiceClientBuilder;
import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.policy.CookiePolicy;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpLoggingPolicy;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.http.policy.HttpPolicyProviders;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.http.policy.UserAgentPolicy;
import com.azure.core.util.Configuration;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** A builder for creating a new instance of the AzureDigitalTwinsAPI type. */
@ServiceClientBuilder(serviceClients = {AzureDigitalTwinsAPIImpl.class})
public final class AzureDigitalTwinsAPIImplBuilder {
private static final String SDK_NAME = "name";

private static final String SDK_VERSION = "version";

private final Map<String, String> properties = new HashMap<>();

public AzureDigitalTwinsAPIImplBuilder() {
this.pipelinePolicies = new ArrayList<>();
}

/*
* server parameter
*/
Expand Down Expand Up @@ -64,6 +84,88 @@ public AzureDigitalTwinsAPIImplBuilder serializerAdapter(SerializerAdapter seria
return this;
}

/*
* The HTTP client used to send the request.
*/
private HttpClient httpClient;

/**
* Sets The HTTP client used to send the request.
*
* @param httpClient the httpClient value.
* @return the AzureDigitalTwinsAPIImplBuilder.
*/
public AzureDigitalTwinsAPIImplBuilder httpClient(HttpClient httpClient) {
this.httpClient = httpClient;
return this;
}

/*
* The configuration store that is used during construction of the service
* client.
*/
private Configuration configuration;

/**
* Sets The configuration store that is used during construction of the service client.
*
* @param configuration the configuration value.
* @return the AzureDigitalTwinsAPIImplBuilder.
*/
public AzureDigitalTwinsAPIImplBuilder configuration(Configuration configuration) {
this.configuration = configuration;
return this;
}

/*
* The logging configuration for HTTP requests and responses.
*/
private HttpLogOptions httpLogOptions;

/**
* Sets The logging configuration for HTTP requests and responses.
*
* @param httpLogOptions the httpLogOptions value.
* @return the AzureDigitalTwinsAPIImplBuilder.
*/
public AzureDigitalTwinsAPIImplBuilder httpLogOptions(HttpLogOptions httpLogOptions) {
this.httpLogOptions = httpLogOptions;
return this;
}

/*
* The retry policy that will attempt to retry failed requests, if
* applicable.
*/
private RetryPolicy retryPolicy;

/**
* Sets The retry policy that will attempt to retry failed requests, if applicable.
*
* @param retryPolicy the retryPolicy value.
* @return the AzureDigitalTwinsAPIImplBuilder.
*/
public AzureDigitalTwinsAPIImplBuilder retryPolicy(RetryPolicy retryPolicy) {
this.retryPolicy = retryPolicy;
return this;
}

/*
* The list of Http pipeline policies to add.
*/
private List<HttpPipelinePolicy> pipelinePolicies;

/**
* Adds a custom Http pipeline policy.
*
* @param customPolicy The custom Http pipeline policy to add.
* @return the AzureDigitalTwinsAPIImplBuilder.
*/
public AzureDigitalTwinsAPIImplBuilder addPolicy(HttpPipelinePolicy customPolicy) {
pipelinePolicies.add(customPolicy);
return this;
}

/**
* Builds an instance of AzureDigitalTwinsAPIImpl with the provided parameters.
*
Expand All @@ -74,15 +176,37 @@ public AzureDigitalTwinsAPIImpl buildClient() {
this.host = "https://digitaltwins-name.digitaltwins.azure.net";
}
if (pipeline == null) {
this.pipeline =
new HttpPipelineBuilder()
.policies(new UserAgentPolicy(), new RetryPolicy(), new CookiePolicy())
.build();
this.pipeline = createHttpPipeline();
}
if (serializerAdapter == null) {
this.serializerAdapter = JacksonAdapter.createDefaultSerializerAdapter();
}
AzureDigitalTwinsAPIImpl client = new AzureDigitalTwinsAPIImpl(pipeline, serializerAdapter, host);
return client;
}

private HttpPipeline createHttpPipeline() {
Configuration buildConfiguration =
(configuration == null) ? Configuration.getGlobalConfiguration() : configuration;
if (httpLogOptions == null) {
httpLogOptions = new HttpLogOptions();
}
List<HttpPipelinePolicy> policies = new ArrayList<>();
String clientName = properties.getOrDefault(SDK_NAME, "UnknownName");
String clientVersion = properties.getOrDefault(SDK_VERSION, "UnknownVersion");
policies.add(
new UserAgentPolicy(httpLogOptions.getApplicationId(), clientName, clientVersion, buildConfiguration));
HttpPolicyProviders.addBeforeRetryPolicies(policies);
policies.add(retryPolicy == null ? new RetryPolicy() : retryPolicy);
policies.add(new CookiePolicy());
policies.addAll(this.pipelinePolicies);
HttpPolicyProviders.addAfterRetryPolicies(policies);
policies.add(new HttpLoggingPolicy(httpLogOptions));
HttpPipeline httpPipeline =
new HttpPipelineBuilder()
.policies(policies.toArray(new HttpPipelinePolicy[0]))
.httpClient(httpClient)
.build();
return httpPipeline;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,10 @@ Mono<Response<Void>> delete(
@ExpectedResponses({200})
@UnexpectedResponseExceptionType(ErrorResponseException.class)
Mono<Response<PagedModelDataCollection>> listNext(
@PathParam(value = "nextLink", encoded = true) String nextLink, Context context);
@PathParam(value = "nextLink", encoded = true) String nextLink,
@HostParam("$host") String host,
@HeaderParam("x-ms-max-item-count") Integer maxItemCount,
Context context);
}

/**
Expand Down Expand Up @@ -267,18 +270,32 @@ public Mono<Response<Void>> deleteWithResponseAsync(String id, Context context)
* Get the next page of items.
*
* @param nextLink The nextLink parameter.
* @param digitalTwinModelsListOptions Parameter group.
* @param context The context to associate with this operation.
* @throws IllegalArgumentException thrown if parameters fail the validation.
* @throws ErrorResponseException thrown if the request is rejected by server.
* @throws RuntimeException all other wrapped checked exceptions if the request fails to be sent.
* @return a collection of ModelData objects.
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<PagedResponse<ModelData>> listNextSinglePageAsync(String nextLink, Context context) {
public Mono<PagedResponse<ModelData>> listNextSinglePageAsync(
String nextLink, DigitalTwinModelsListOptions digitalTwinModelsListOptions, Context context) {
if (nextLink == null) {
return Mono.error(new IllegalArgumentException("Parameter nextLink is required and cannot be null."));
}
return service.listNext(nextLink, context)
if (this.client.getHost() == null) {
return Mono.error(
new IllegalArgumentException("Parameter this.client.getHost() is required and cannot be null."));
}
if (digitalTwinModelsListOptions != null) {
digitalTwinModelsListOptions.validate();
}
Integer maxItemCountInternal = null;
if (digitalTwinModelsListOptions != null) {
maxItemCountInternal = digitalTwinModelsListOptions.getMaxItemCount();
}
Integer maxItemCount = maxItemCountInternal;
return service.listNext(nextLink, this.client.getHost(), maxItemCount, context)
.map(
res ->
new PagedResponseBase<>(
Expand Down
Loading