|
| 1 | +package documentation; |
| 2 | + |
| 3 | +import documentation.implementation.DocumentationClientImpl; |
| 4 | +import io.clientcore.core.annotations.Metadata; |
| 5 | +import io.clientcore.core.annotations.MetadataProperties; |
| 6 | +import io.clientcore.core.annotations.ServiceClientBuilder; |
| 7 | +import io.clientcore.core.http.client.HttpClient; |
| 8 | +import io.clientcore.core.http.models.ProxyOptions; |
| 9 | +import io.clientcore.core.http.pipeline.HttpInstrumentationOptions; |
| 10 | +import io.clientcore.core.http.pipeline.HttpInstrumentationPolicy; |
| 11 | +import io.clientcore.core.http.pipeline.HttpPipeline; |
| 12 | +import io.clientcore.core.http.pipeline.HttpPipelineBuilder; |
| 13 | +import io.clientcore.core.http.pipeline.HttpPipelinePolicy; |
| 14 | +import io.clientcore.core.http.pipeline.HttpRedirectOptions; |
| 15 | +import io.clientcore.core.http.pipeline.HttpRedirectPolicy; |
| 16 | +import io.clientcore.core.http.pipeline.HttpRetryOptions; |
| 17 | +import io.clientcore.core.http.pipeline.HttpRetryPolicy; |
| 18 | +import io.clientcore.core.instrumentation.Instrumentation; |
| 19 | +import io.clientcore.core.instrumentation.SdkInstrumentationOptions; |
| 20 | +import io.clientcore.core.traits.ConfigurationTrait; |
| 21 | +import io.clientcore.core.traits.EndpointTrait; |
| 22 | +import io.clientcore.core.traits.HttpTrait; |
| 23 | +import io.clientcore.core.traits.ProxyTrait; |
| 24 | +import io.clientcore.core.utils.CoreUtils; |
| 25 | +import io.clientcore.core.utils.configuration.Configuration; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.Objects; |
| 30 | + |
| 31 | +/** |
| 32 | + * A builder for creating a new instance of the DocumentationClient type. |
| 33 | + */ |
| 34 | +@ServiceClientBuilder(serviceClients = { ListsClient.class, TextFormattingClient.class }) |
| 35 | +public final class DocumentationClientBuilder |
| 36 | + implements HttpTrait<DocumentationClientBuilder>, ProxyTrait<DocumentationClientBuilder>, |
| 37 | + ConfigurationTrait<DocumentationClientBuilder>, EndpointTrait<DocumentationClientBuilder> { |
| 38 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 39 | + private static final String SDK_NAME = "name"; |
| 40 | + |
| 41 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 42 | + private static final String SDK_VERSION = "version"; |
| 43 | + |
| 44 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 45 | + private static final Map<String, String> PROPERTIES = CoreUtils.getProperties("documentation.properties"); |
| 46 | + |
| 47 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 48 | + private final List<HttpPipelinePolicy> pipelinePolicies; |
| 49 | + |
| 50 | + /** |
| 51 | + * Create an instance of the DocumentationClientBuilder. |
| 52 | + */ |
| 53 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 54 | + public DocumentationClientBuilder() { |
| 55 | + this.pipelinePolicies = new ArrayList<>(); |
| 56 | + } |
| 57 | + |
| 58 | + /* |
| 59 | + * The HTTP client used to send the request. |
| 60 | + */ |
| 61 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 62 | + private HttpClient httpClient; |
| 63 | + |
| 64 | + /** |
| 65 | + * {@inheritDoc}. |
| 66 | + */ |
| 67 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 68 | + @Override |
| 69 | + public DocumentationClientBuilder httpClient(HttpClient httpClient) { |
| 70 | + this.httpClient = httpClient; |
| 71 | + return this; |
| 72 | + } |
| 73 | + |
| 74 | + /* |
| 75 | + * The retry options to configure retry policy for failed requests. |
| 76 | + */ |
| 77 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 78 | + private HttpRetryOptions retryOptions; |
| 79 | + |
| 80 | + /** |
| 81 | + * {@inheritDoc}. |
| 82 | + */ |
| 83 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 84 | + @Override |
| 85 | + public DocumentationClientBuilder httpRetryOptions(HttpRetryOptions retryOptions) { |
| 86 | + this.retryOptions = retryOptions; |
| 87 | + return this; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * {@inheritDoc}. |
| 92 | + */ |
| 93 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 94 | + @Override |
| 95 | + public DocumentationClientBuilder addHttpPipelinePolicy(HttpPipelinePolicy customPolicy) { |
| 96 | + Objects.requireNonNull(customPolicy, "'customPolicy' cannot be null."); |
| 97 | + pipelinePolicies.add(customPolicy); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + /* |
| 102 | + * The redirect options to configure redirect policy |
| 103 | + */ |
| 104 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 105 | + private HttpRedirectOptions redirectOptions; |
| 106 | + |
| 107 | + /** |
| 108 | + * {@inheritDoc}. |
| 109 | + */ |
| 110 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 111 | + @Override |
| 112 | + public DocumentationClientBuilder httpRedirectOptions(HttpRedirectOptions redirectOptions) { |
| 113 | + this.redirectOptions = redirectOptions; |
| 114 | + return this; |
| 115 | + } |
| 116 | + |
| 117 | + /* |
| 118 | + * The instrumentation configuration for HTTP requests and responses. |
| 119 | + */ |
| 120 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 121 | + private HttpInstrumentationOptions httpInstrumentationOptions; |
| 122 | + |
| 123 | + /** |
| 124 | + * {@inheritDoc}. |
| 125 | + */ |
| 126 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 127 | + @Override |
| 128 | + public DocumentationClientBuilder |
| 129 | + httpInstrumentationOptions(HttpInstrumentationOptions httpInstrumentationOptions) { |
| 130 | + this.httpInstrumentationOptions = httpInstrumentationOptions; |
| 131 | + return this; |
| 132 | + } |
| 133 | + |
| 134 | + /* |
| 135 | + * The proxy options used during construction of the service client. |
| 136 | + */ |
| 137 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 138 | + private ProxyOptions proxyOptions; |
| 139 | + |
| 140 | + /** |
| 141 | + * {@inheritDoc}. |
| 142 | + */ |
| 143 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 144 | + @Override |
| 145 | + public DocumentationClientBuilder proxyOptions(ProxyOptions proxyOptions) { |
| 146 | + this.proxyOptions = proxyOptions; |
| 147 | + return this; |
| 148 | + } |
| 149 | + |
| 150 | + /* |
| 151 | + * The configuration store that is used during construction of the service client. |
| 152 | + */ |
| 153 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 154 | + private Configuration configuration; |
| 155 | + |
| 156 | + /** |
| 157 | + * {@inheritDoc}. |
| 158 | + */ |
| 159 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 160 | + @Override |
| 161 | + public DocumentationClientBuilder configuration(Configuration configuration) { |
| 162 | + this.configuration = configuration; |
| 163 | + return this; |
| 164 | + } |
| 165 | + |
| 166 | + /* |
| 167 | + * The service endpoint |
| 168 | + */ |
| 169 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 170 | + private String endpoint; |
| 171 | + |
| 172 | + /** |
| 173 | + * {@inheritDoc}. |
| 174 | + */ |
| 175 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 176 | + @Override |
| 177 | + public DocumentationClientBuilder endpoint(String endpoint) { |
| 178 | + this.endpoint = endpoint; |
| 179 | + return this; |
| 180 | + } |
| 181 | + |
| 182 | + /** |
| 183 | + * Builds an instance of DocumentationClientImpl with the provided parameters. |
| 184 | + * |
| 185 | + * @return an instance of DocumentationClientImpl. |
| 186 | + */ |
| 187 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 188 | + private DocumentationClientImpl buildInnerClient() { |
| 189 | + this.validateClient(); |
| 190 | + String localEndpoint = (endpoint != null) ? endpoint : "http://localhost:3000"; |
| 191 | + HttpInstrumentationOptions localHttpInstrumentationOptions = this.httpInstrumentationOptions == null |
| 192 | + ? new HttpInstrumentationOptions() |
| 193 | + : this.httpInstrumentationOptions; |
| 194 | + SdkInstrumentationOptions sdkInstrumentationOptions |
| 195 | + = new SdkInstrumentationOptions(PROPERTIES.getOrDefault(SDK_NAME, "UnknownName")) |
| 196 | + .setSdkVersion(PROPERTIES.get(SDK_VERSION)) |
| 197 | + .setEndpoint(localEndpoint); |
| 198 | + Instrumentation instrumentation |
| 199 | + = Instrumentation.create(localHttpInstrumentationOptions, sdkInstrumentationOptions); |
| 200 | + DocumentationClientImpl client |
| 201 | + = new DocumentationClientImpl(createHttpPipeline(), instrumentation, localEndpoint); |
| 202 | + return client; |
| 203 | + } |
| 204 | + |
| 205 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 206 | + private void validateClient() { |
| 207 | + // This method is invoked from 'buildInnerClient'/'buildClient' method. |
| 208 | + // Developer can customize this method, to validate that the necessary conditions are met for the new client. |
| 209 | + } |
| 210 | + |
| 211 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 212 | + private HttpPipeline createHttpPipeline() { |
| 213 | + Configuration buildConfiguration |
| 214 | + = (configuration == null) ? Configuration.getGlobalConfiguration() : configuration; |
| 215 | + HttpInstrumentationOptions localHttpInstrumentationOptions = this.httpInstrumentationOptions == null |
| 216 | + ? new HttpInstrumentationOptions() |
| 217 | + : this.httpInstrumentationOptions; |
| 218 | + HttpPipelineBuilder httpPipelineBuilder = new HttpPipelineBuilder(); |
| 219 | + List<HttpPipelinePolicy> policies = new ArrayList<>(); |
| 220 | + policies.add(redirectOptions == null ? new HttpRedirectPolicy() : new HttpRedirectPolicy(redirectOptions)); |
| 221 | + policies.add(retryOptions == null ? new HttpRetryPolicy() : new HttpRetryPolicy(retryOptions)); |
| 222 | + this.pipelinePolicies.stream().forEach(p -> policies.add(p)); |
| 223 | + policies.add(new HttpInstrumentationPolicy(localHttpInstrumentationOptions)); |
| 224 | + policies.forEach(httpPipelineBuilder::addPolicy); |
| 225 | + return httpPipelineBuilder.httpClient(httpClient).build(); |
| 226 | + } |
| 227 | + |
| 228 | + /** |
| 229 | + * Builds an instance of ListsClient class. |
| 230 | + * |
| 231 | + * @return an instance of ListsClient. |
| 232 | + */ |
| 233 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 234 | + public ListsClient buildListsClient() { |
| 235 | + DocumentationClientImpl innerClient = buildInnerClient(); |
| 236 | + return new ListsClient(innerClient.getLists(), innerClient.getInstrumentation()); |
| 237 | + } |
| 238 | + |
| 239 | + /** |
| 240 | + * Builds an instance of TextFormattingClient class. |
| 241 | + * |
| 242 | + * @return an instance of TextFormattingClient. |
| 243 | + */ |
| 244 | + @Metadata(properties = { MetadataProperties.GENERATED }) |
| 245 | + public TextFormattingClient buildTextFormattingClient() { |
| 246 | + DocumentationClientImpl innerClient = buildInnerClient(); |
| 247 | + return new TextFormattingClient(innerClient.getTextFormattings(), innerClient.getInstrumentation()); |
| 248 | + } |
| 249 | +} |
0 commit comments