|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | +// Code generated by Microsoft (R) TypeSpec Code Generator. |
| 4 | + |
| 5 | +package azure.resourcemanager.largeheader; |
| 6 | + |
| 7 | +import azure.resourcemanager.largeheader.fluent.LargeHeaderClient; |
| 8 | +import azure.resourcemanager.largeheader.implementation.LargeHeaderClientBuilder; |
| 9 | +import azure.resourcemanager.largeheader.implementation.LargeHeadersImpl; |
| 10 | +import azure.resourcemanager.largeheader.models.LargeHeaders; |
| 11 | +import com.azure.core.credential.TokenCredential; |
| 12 | +import com.azure.core.http.HttpClient; |
| 13 | +import com.azure.core.http.HttpPipeline; |
| 14 | +import com.azure.core.http.HttpPipelineBuilder; |
| 15 | +import com.azure.core.http.HttpPipelinePosition; |
| 16 | +import com.azure.core.http.policy.AddDatePolicy; |
| 17 | +import com.azure.core.http.policy.AddHeadersFromContextPolicy; |
| 18 | +import com.azure.core.http.policy.BearerTokenAuthenticationPolicy; |
| 19 | +import com.azure.core.http.policy.HttpLogOptions; |
| 20 | +import com.azure.core.http.policy.HttpLoggingPolicy; |
| 21 | +import com.azure.core.http.policy.HttpPipelinePolicy; |
| 22 | +import com.azure.core.http.policy.HttpPolicyProviders; |
| 23 | +import com.azure.core.http.policy.RequestIdPolicy; |
| 24 | +import com.azure.core.http.policy.RetryOptions; |
| 25 | +import com.azure.core.http.policy.RetryPolicy; |
| 26 | +import com.azure.core.http.policy.UserAgentPolicy; |
| 27 | +import com.azure.core.management.profile.AzureProfile; |
| 28 | +import com.azure.core.util.Configuration; |
| 29 | +import com.azure.core.util.CoreUtils; |
| 30 | +import com.azure.core.util.logging.ClientLogger; |
| 31 | +import java.time.Duration; |
| 32 | +import java.time.temporal.ChronoUnit; |
| 33 | +import java.util.ArrayList; |
| 34 | +import java.util.List; |
| 35 | +import java.util.Map; |
| 36 | +import java.util.Objects; |
| 37 | +import java.util.stream.Collectors; |
| 38 | + |
| 39 | +/** |
| 40 | + * Entry point to LargeHeaderManager. |
| 41 | + * Arm Resource Provider management API. |
| 42 | + */ |
| 43 | +public final class LargeHeaderManager { |
| 44 | + private LargeHeaders largeHeaders; |
| 45 | + |
| 46 | + private final LargeHeaderClient clientObject; |
| 47 | + |
| 48 | + private LargeHeaderManager(HttpPipeline httpPipeline, AzureProfile profile, Duration defaultPollInterval) { |
| 49 | + Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null."); |
| 50 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 51 | + this.clientObject = new LargeHeaderClientBuilder().pipeline(httpPipeline) |
| 52 | + .endpoint(profile.getEnvironment().getResourceManagerEndpoint()) |
| 53 | + .subscriptionId(profile.getSubscriptionId()) |
| 54 | + .defaultPollInterval(defaultPollInterval) |
| 55 | + .buildClient(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Creates an instance of LargeHeader service API entry point. |
| 60 | + * |
| 61 | + * @param credential the credential to use. |
| 62 | + * @param profile the Azure profile for client. |
| 63 | + * @return the LargeHeader service API instance. |
| 64 | + */ |
| 65 | + public static LargeHeaderManager authenticate(TokenCredential credential, AzureProfile profile) { |
| 66 | + Objects.requireNonNull(credential, "'credential' cannot be null."); |
| 67 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 68 | + return configure().authenticate(credential, profile); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Creates an instance of LargeHeader service API entry point. |
| 73 | + * |
| 74 | + * @param httpPipeline the {@link HttpPipeline} configured with Azure authentication credential. |
| 75 | + * @param profile the Azure profile for client. |
| 76 | + * @return the LargeHeader service API instance. |
| 77 | + */ |
| 78 | + public static LargeHeaderManager authenticate(HttpPipeline httpPipeline, AzureProfile profile) { |
| 79 | + Objects.requireNonNull(httpPipeline, "'httpPipeline' cannot be null."); |
| 80 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 81 | + return new LargeHeaderManager(httpPipeline, profile, null); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Gets a Configurable instance that can be used to create LargeHeaderManager with optional configuration. |
| 86 | + * |
| 87 | + * @return the Configurable instance allowing configurations. |
| 88 | + */ |
| 89 | + public static Configurable configure() { |
| 90 | + return new LargeHeaderManager.Configurable(); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * The Configurable allowing configurations to be set. |
| 95 | + */ |
| 96 | + public static final class Configurable { |
| 97 | + private static final ClientLogger LOGGER = new ClientLogger(Configurable.class); |
| 98 | + private static final String SDK_VERSION = "version"; |
| 99 | + private static final Map<String, String> PROPERTIES |
| 100 | + = CoreUtils.getProperties("azure-resourcemanager-largeheader-generated.properties"); |
| 101 | + |
| 102 | + private HttpClient httpClient; |
| 103 | + private HttpLogOptions httpLogOptions; |
| 104 | + private final List<HttpPipelinePolicy> policies = new ArrayList<>(); |
| 105 | + private final List<String> scopes = new ArrayList<>(); |
| 106 | + private RetryPolicy retryPolicy; |
| 107 | + private RetryOptions retryOptions; |
| 108 | + private Duration defaultPollInterval; |
| 109 | + |
| 110 | + private Configurable() { |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Sets the http client. |
| 115 | + * |
| 116 | + * @param httpClient the HTTP client. |
| 117 | + * @return the configurable object itself. |
| 118 | + */ |
| 119 | + public Configurable withHttpClient(HttpClient httpClient) { |
| 120 | + this.httpClient = Objects.requireNonNull(httpClient, "'httpClient' cannot be null."); |
| 121 | + return this; |
| 122 | + } |
| 123 | + |
| 124 | + /** |
| 125 | + * Sets the logging options to the HTTP pipeline. |
| 126 | + * |
| 127 | + * @param httpLogOptions the HTTP log options. |
| 128 | + * @return the configurable object itself. |
| 129 | + */ |
| 130 | + public Configurable withLogOptions(HttpLogOptions httpLogOptions) { |
| 131 | + this.httpLogOptions = Objects.requireNonNull(httpLogOptions, "'httpLogOptions' cannot be null."); |
| 132 | + return this; |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Adds the pipeline policy to the HTTP pipeline. |
| 137 | + * |
| 138 | + * @param policy the HTTP pipeline policy. |
| 139 | + * @return the configurable object itself. |
| 140 | + */ |
| 141 | + public Configurable withPolicy(HttpPipelinePolicy policy) { |
| 142 | + this.policies.add(Objects.requireNonNull(policy, "'policy' cannot be null.")); |
| 143 | + return this; |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * Adds the scope to permission sets. |
| 148 | + * |
| 149 | + * @param scope the scope. |
| 150 | + * @return the configurable object itself. |
| 151 | + */ |
| 152 | + public Configurable withScope(String scope) { |
| 153 | + this.scopes.add(Objects.requireNonNull(scope, "'scope' cannot be null.")); |
| 154 | + return this; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Sets the retry policy to the HTTP pipeline. |
| 159 | + * |
| 160 | + * @param retryPolicy the HTTP pipeline retry policy. |
| 161 | + * @return the configurable object itself. |
| 162 | + */ |
| 163 | + public Configurable withRetryPolicy(RetryPolicy retryPolicy) { |
| 164 | + this.retryPolicy = Objects.requireNonNull(retryPolicy, "'retryPolicy' cannot be null."); |
| 165 | + return this; |
| 166 | + } |
| 167 | + |
| 168 | + /** |
| 169 | + * Sets the retry options for the HTTP pipeline retry policy. |
| 170 | + * <p> |
| 171 | + * This setting has no effect, if retry policy is set via {@link #withRetryPolicy(RetryPolicy)}. |
| 172 | + * |
| 173 | + * @param retryOptions the retry options for the HTTP pipeline retry policy. |
| 174 | + * @return the configurable object itself. |
| 175 | + */ |
| 176 | + public Configurable withRetryOptions(RetryOptions retryOptions) { |
| 177 | + this.retryOptions = Objects.requireNonNull(retryOptions, "'retryOptions' cannot be null."); |
| 178 | + return this; |
| 179 | + } |
| 180 | + |
| 181 | + /** |
| 182 | + * Sets the default poll interval, used when service does not provide "Retry-After" header. |
| 183 | + * |
| 184 | + * @param defaultPollInterval the default poll interval. |
| 185 | + * @return the configurable object itself. |
| 186 | + */ |
| 187 | + public Configurable withDefaultPollInterval(Duration defaultPollInterval) { |
| 188 | + this.defaultPollInterval |
| 189 | + = Objects.requireNonNull(defaultPollInterval, "'defaultPollInterval' cannot be null."); |
| 190 | + if (this.defaultPollInterval.isNegative()) { |
| 191 | + throw LOGGER |
| 192 | + .logExceptionAsError(new IllegalArgumentException("'defaultPollInterval' cannot be negative")); |
| 193 | + } |
| 194 | + return this; |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * Creates an instance of LargeHeader service API entry point. |
| 199 | + * |
| 200 | + * @param credential the credential to use. |
| 201 | + * @param profile the Azure profile for client. |
| 202 | + * @return the LargeHeader service API instance. |
| 203 | + */ |
| 204 | + public LargeHeaderManager authenticate(TokenCredential credential, AzureProfile profile) { |
| 205 | + Objects.requireNonNull(credential, "'credential' cannot be null."); |
| 206 | + Objects.requireNonNull(profile, "'profile' cannot be null."); |
| 207 | + |
| 208 | + String clientVersion = PROPERTIES.getOrDefault(SDK_VERSION, "UnknownVersion"); |
| 209 | + |
| 210 | + StringBuilder userAgentBuilder = new StringBuilder(); |
| 211 | + userAgentBuilder.append("azsdk-java") |
| 212 | + .append("-") |
| 213 | + .append("azure.resourcemanager.largeheader") |
| 214 | + .append("/") |
| 215 | + .append(clientVersion); |
| 216 | + if (!Configuration.getGlobalConfiguration().get("AZURE_TELEMETRY_DISABLED", false)) { |
| 217 | + userAgentBuilder.append(" (") |
| 218 | + .append(Configuration.getGlobalConfiguration().get("java.version")) |
| 219 | + .append("; ") |
| 220 | + .append(Configuration.getGlobalConfiguration().get("os.name")) |
| 221 | + .append("; ") |
| 222 | + .append(Configuration.getGlobalConfiguration().get("os.version")) |
| 223 | + .append("; auto-generated)"); |
| 224 | + } else { |
| 225 | + userAgentBuilder.append(" (auto-generated)"); |
| 226 | + } |
| 227 | + |
| 228 | + if (scopes.isEmpty()) { |
| 229 | + scopes.add(profile.getEnvironment().getManagementEndpoint() + "/.default"); |
| 230 | + } |
| 231 | + if (retryPolicy == null) { |
| 232 | + if (retryOptions != null) { |
| 233 | + retryPolicy = new RetryPolicy(retryOptions); |
| 234 | + } else { |
| 235 | + retryPolicy = new RetryPolicy("Retry-After", ChronoUnit.SECONDS); |
| 236 | + } |
| 237 | + } |
| 238 | + List<HttpPipelinePolicy> policies = new ArrayList<>(); |
| 239 | + policies.add(new UserAgentPolicy(userAgentBuilder.toString())); |
| 240 | + policies.add(new AddHeadersFromContextPolicy()); |
| 241 | + policies.add(new RequestIdPolicy()); |
| 242 | + policies.addAll(this.policies.stream() |
| 243 | + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_CALL) |
| 244 | + .collect(Collectors.toList())); |
| 245 | + HttpPolicyProviders.addBeforeRetryPolicies(policies); |
| 246 | + policies.add(retryPolicy); |
| 247 | + policies.add(new AddDatePolicy()); |
| 248 | + policies.add(new BearerTokenAuthenticationPolicy(credential, scopes.toArray(new String[0]))); |
| 249 | + policies.addAll(this.policies.stream() |
| 250 | + .filter(p -> p.getPipelinePosition() == HttpPipelinePosition.PER_RETRY) |
| 251 | + .collect(Collectors.toList())); |
| 252 | + HttpPolicyProviders.addAfterRetryPolicies(policies); |
| 253 | + policies.add(new HttpLoggingPolicy(httpLogOptions)); |
| 254 | + HttpPipeline httpPipeline = new HttpPipelineBuilder().httpClient(httpClient) |
| 255 | + .policies(policies.toArray(new HttpPipelinePolicy[0])) |
| 256 | + .build(); |
| 257 | + return new LargeHeaderManager(httpPipeline, profile, defaultPollInterval); |
| 258 | + } |
| 259 | + } |
| 260 | + |
| 261 | + /** |
| 262 | + * Gets the resource collection API of LargeHeaders. |
| 263 | + * |
| 264 | + * @return Resource collection API of LargeHeaders. |
| 265 | + */ |
| 266 | + public LargeHeaders largeHeaders() { |
| 267 | + if (this.largeHeaders == null) { |
| 268 | + this.largeHeaders = new LargeHeadersImpl(clientObject.getLargeHeaders(), this); |
| 269 | + } |
| 270 | + return largeHeaders; |
| 271 | + } |
| 272 | + |
| 273 | + /** |
| 274 | + * Gets wrapped service client LargeHeaderClient providing direct access to the underlying auto-generated API |
| 275 | + * implementation, based on Azure REST API. |
| 276 | + * |
| 277 | + * @return Wrapped service client LargeHeaderClient. |
| 278 | + */ |
| 279 | + public LargeHeaderClient serviceClient() { |
| 280 | + return this.clientObject; |
| 281 | + } |
| 282 | +} |
0 commit comments