Skip to content

Commit

Permalink
Multiple version support with validation succeed at live tests
Browse files Browse the repository at this point in the history
  • Loading branch information
wangrui-msft committed Apr 26, 2022
1 parent 3ee898e commit 34c2be3
Show file tree
Hide file tree
Showing 19 changed files with 600 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public final class RoomsClientBuilder {
private final List<HttpPipelinePolicy> customPolicies = new ArrayList<HttpPipelinePolicy>();
private ClientOptions clientOptions;
private RetryPolicy retryPolicy;
private RoomsServiceVersion serviceVersion;

/**
* Set endpoint of the service
Expand Down Expand Up @@ -163,6 +164,7 @@ public RoomsClientBuilder httpLogOptions(HttpLogOptions logOptions) {
* @return the updated RoomsClientBuilder object
*/
public RoomsClientBuilder serviceVersion(RoomsServiceVersion version) {
this.serviceVersion = version;
return this;
}

Expand Down Expand Up @@ -221,8 +223,11 @@ private AzureCommunicationRoomServiceImpl createServiceImpl() {
builderPipeline = createHttpPipeline(httpClient);
}

RoomsServiceVersion apiVersion = serviceVersion != null ? serviceVersion : RoomsServiceVersion.getLatest();

AzureCommunicationRoomServiceImplBuilder clientBuilder = new AzureCommunicationRoomServiceImplBuilder();
clientBuilder.endpoint(endpoint)
.apiVersion(apiVersion.getVersion())
.pipeline(builderPipeline);

return clientBuilder.buildClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
* The versions of Communication Rooms Service supported by this client library.
*/
public enum RoomsServiceVersion implements ServiceVersion {
/**
* Service version {@code 2021-04-07}
*/
V2021_04_07_Preview("2021-04-07"),

/**
* Service version {@code 2022-02-01}
*/
V2022_02_01_Preview("2022-02-01");

private final String version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import com.azure.core.http.policy.CookiePolicy;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.http.policy.UserAgentPolicy;
import com.azure.core.util.serializer.JacksonAdapter;
import com.azure.core.util.serializer.SerializerAdapter;

/** Initializes a new instance of the AzureCommunicationRoomService type. */
public final class AzureCommunicationRoomServiceImpl {
Expand Down Expand Up @@ -48,6 +50,18 @@ public HttpPipeline getHttpPipeline() {
return this.httpPipeline;
}

/** The serializer to serialize an object into a string. */
private final SerializerAdapter serializerAdapter;

/**
* Gets The serializer to serialize an object into a string.
*
* @return the serializerAdapter value.
*/
public SerializerAdapter getSerializerAdapter() {
return this.serializerAdapter;
}

/** The RoomsImpl object to access its operations. */
private final RoomsImpl rooms;

Expand All @@ -60,24 +74,47 @@ public RoomsImpl getRooms() {
return this.rooms;
}

/** Initializes an instance of AzureCommunicationRoomService client. */
AzureCommunicationRoomServiceImpl(String endpoint) {
/**
* Initializes an instance of AzureCommunicationRoomService client.
*
* @param endpoint The endpoint of the Azure Communication resource.
* @param apiVersion Api Version.
*/
AzureCommunicationRoomServiceImpl(String endpoint, String apiVersion) {
this(
new HttpPipelineBuilder()
.policies(new UserAgentPolicy(), new RetryPolicy(), new CookiePolicy())
.build(),
endpoint);
JacksonAdapter.createDefaultSerializerAdapter(),
endpoint,
apiVersion);
}

/**
* Initializes an instance of AzureCommunicationRoomService client.
*
* @param httpPipeline The HTTP pipeline to send requests through.
* @param endpoint The endpoint of the Azure Communication resource.
* @param apiVersion Api Version.
*/
AzureCommunicationRoomServiceImpl(HttpPipeline httpPipeline, String endpoint, String apiVersion) {
this(httpPipeline, JacksonAdapter.createDefaultSerializerAdapter(), endpoint, apiVersion);
}

/**
* Initializes an instance of AzureCommunicationRoomService client.
*
* @param httpPipeline The HTTP pipeline to send requests through.
* @param serializerAdapter The serializer to serialize an object into a string.
* @param endpoint The endpoint of the Azure Communication resource.
* @param apiVersion Api Version.
*/
AzureCommunicationRoomServiceImpl(HttpPipeline httpPipeline, String endpoint) {
AzureCommunicationRoomServiceImpl(
HttpPipeline httpPipeline, SerializerAdapter serializerAdapter, String endpoint, String apiVersion) {
this.httpPipeline = httpPipeline;
this.serializerAdapter = serializerAdapter;
this.endpoint = endpoint;
this.apiVersion = "2022-02-01";
this.apiVersion = apiVersion;
this.rooms = new RoomsImpl(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,38 @@
package com.azure.communication.rooms.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 AzureCommunicationRoomService type. */
@ServiceClientBuilder(serviceClients = {AzureCommunicationRoomServiceImpl.class})
public final class AzureCommunicationRoomServiceImplBuilder {
private static final String SDK_NAME = "name";

private static final String SDK_VERSION = "version";

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

/** Create an instance of the AzureCommunicationRoomServiceImplBuilder. */
public AzureCommunicationRoomServiceImplBuilder() {
this.pipelinePolicies = new ArrayList<>();
}

/*
* The endpoint of the Azure Communication resource.
*/
Expand All @@ -30,6 +53,22 @@ public AzureCommunicationRoomServiceImplBuilder endpoint(String endpoint) {
return this;
}

/*
* Api Version
*/
private String apiVersion;

/**
* Sets Api Version.
*
* @param apiVersion the apiVersion value.
* @return the AzureCommunicationRoomServiceImplBuilder.
*/
public AzureCommunicationRoomServiceImplBuilder apiVersion(String apiVersion) {
this.apiVersion = apiVersion;
return this;
}

/*
* The HTTP pipeline to send requests through
*/
Expand All @@ -46,19 +85,146 @@ public AzureCommunicationRoomServiceImplBuilder pipeline(HttpPipeline pipeline)
return this;
}

/*
* The serializer to serialize an object into a string
*/
private SerializerAdapter serializerAdapter;

/**
* Sets The serializer to serialize an object into a string.
*
* @param serializerAdapter the serializerAdapter value.
* @return the AzureCommunicationRoomServiceImplBuilder.
*/
public AzureCommunicationRoomServiceImplBuilder serializerAdapter(SerializerAdapter serializerAdapter) {
this.serializerAdapter = serializerAdapter;
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 AzureCommunicationRoomServiceImplBuilder.
*/
public AzureCommunicationRoomServiceImplBuilder 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 AzureCommunicationRoomServiceImplBuilder.
*/
public AzureCommunicationRoomServiceImplBuilder 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 AzureCommunicationRoomServiceImplBuilder.
*/
public AzureCommunicationRoomServiceImplBuilder 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 AzureCommunicationRoomServiceImplBuilder.
*/
public AzureCommunicationRoomServiceImplBuilder retryPolicy(RetryPolicy retryPolicy) {
this.retryPolicy = retryPolicy;
return this;
}

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

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

/**
* Builds an instance of AzureCommunicationRoomServiceImpl with the provided parameters.
*
* @return an instance of AzureCommunicationRoomServiceImpl.
*/
public AzureCommunicationRoomServiceImpl buildClient() {
if (apiVersion == null) {
this.apiVersion = "2022-02-01";
}
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();
}
AzureCommunicationRoomServiceImpl client = new AzureCommunicationRoomServiceImpl(pipeline, endpoint);
AzureCommunicationRoomServiceImpl client =
new AzureCommunicationRoomServiceImpl(pipeline, serializerAdapter, endpoint, apiVersion);
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;
}
}
Loading

0 comments on commit 34c2be3

Please sign in to comment.