CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-azure--azure-core

Azure Core provides shared primitives, abstractions, and helpers for modern Java Azure SDK client libraries

Pending
Overview
Eval results
Files

client-traits.mddocs/

Client Builder Traits

Trait interfaces providing consistent configuration patterns across Azure SDK client builders. These traits ensure uniform API design and enable composable builder functionality.

Capabilities

EndpointTrait

Trait for configuring service endpoints in client builders.

/**
 * Trait for Azure SDK client builders that support setting service endpoints.
 */
interface EndpointTrait<T> {
    /**
     * Sets the service endpoint URL.
     * @param endpoint The service endpoint URL
     * @return The updated builder object
     */
    T endpoint(String endpoint);
}

HttpTrait

Trait for HTTP-specific configuration including client, pipeline, and policies.

/**
 * Trait for Azure SDK client builders that support HTTP-specific configuration settings.
 */
interface HttpTrait<T> {
    /**
     * Sets the HTTP client to use for sending requests.
     * @param httpClient The HTTP client implementation
     * @return The updated builder object
     */
    T httpClient(HttpClient httpClient);
    
    /**
     * Sets the HTTP pipeline to use for sending requests.
     * @param pipeline The HTTP pipeline
     * @return The updated builder object
     */
    T pipeline(HttpPipeline pipeline);
    
    /**
     * Adds a pipeline policy to the client's pipeline.
     * @param pipelinePolicy The pipeline policy to add
     * @return The updated builder object
     */
    T addPolicy(HttpPipelinePolicy pipelinePolicy);
    
    /**
     * Sets the retry options for the client.
     * @param retryOptions The retry configuration
     * @return The updated builder object
     */
    T retryOptions(RetryOptions retryOptions);
    
    /**
     * Sets the HTTP logging options.
     * @param logOptions The HTTP logging configuration
     * @return The updated builder object
     */
    T httpLogOptions(HttpLogOptions logOptions);
    
    /**
     * Sets common client options.
     * @param clientOptions The client options
     * @return The updated builder object
     */
    T clientOptions(ClientOptions clientOptions);
}

TokenCredentialTrait

Trait for configuring Azure Active Directory token-based authentication.

/**
 * Trait for Azure SDK client builders that support TokenCredential authentication.
 */
interface TokenCredentialTrait<T> {
    /**
     * Sets the TokenCredential used to authorize requests sent by the service client.
     * @param credential TokenCredential used to authorize requests
     * @return The updated builder object
     */
    T credential(TokenCredential credential);
}

AzureKeyCredentialTrait

Trait for configuring Azure key-based authentication.

/**
 * Trait for Azure SDK client builders that support AzureKeyCredential authentication.
 */
interface AzureKeyCredentialTrait<T> {
    /**
     * Sets the AzureKeyCredential used to authorize requests sent by the service client.
     * @param credential AzureKeyCredential used to authorize requests
     * @return The updated builder object
     */
    T credential(AzureKeyCredential credential);
}

AzureNamedKeyCredentialTrait

Trait for configuring named key authentication where both key name and value are required.

/**
 * Trait for Azure SDK client builders that support AzureNamedKeyCredential authentication.
 */
interface AzureNamedKeyCredentialTrait<T> {
    /**
     * Sets the AzureNamedKeyCredential used to authorize requests sent by the service client.
     * @param credential AzureNamedKeyCredential used to authorize requests
     * @return The updated builder object
     */
    T credential(AzureNamedKeyCredential credential);
}

AzureSasCredentialTrait

Trait for configuring Shared Access Signature authentication.

/**
 * Trait for Azure SDK client builders that support AzureSasCredential authentication.
 */
interface AzureSasCredentialTrait<T> {
    /**
     * Sets the AzureSasCredential used to authorize requests sent by the service client.
     * @param credential AzureSasCredential used to authorize requests
     * @return The updated builder object
     */
    T credential(AzureSasCredential credential);
}

KeyCredentialTrait

Trait for configuring generic key-based authentication.

/**
 * Trait for Azure SDK client builders that support KeyCredential authentication.
 */
interface KeyCredentialTrait<T> {
    /**
     * Sets the KeyCredential used to authorize requests sent by the service client.
     * @param credential KeyCredential used to authorize requests
     * @return The updated builder object
     */
    T credential(KeyCredential credential);
}

ConnectionStringTrait

Trait for configuring connection string-based authentication and configuration.

/**
 * Trait for Azure SDK client builders that support connection string configuration.
 */
interface ConnectionStringTrait<T> {
    /**
     * Sets the connection string for the service.
     * @param connectionString Connection string containing service configuration
     * @return The updated builder object
     */
    T connectionString(String connectionString);
}

ConfigurationTrait

Trait for configuring application configuration settings.

/**
 * Trait for Azure SDK client builders that support configuration settings.
 */
interface ConfigurationTrait<T> {
    /**
     * Sets the configuration instance to use for the client.
     * @param configuration Configuration instance containing settings
     * @return The updated builder object
     */
    T configuration(Configuration configuration);
}

Usage Examples

Implementing Multiple Traits

import com.azure.core.client.traits.*;
import com.azure.core.http.HttpClient;
import com.azure.core.credential.TokenCredential;

@ServiceClientBuilder(serviceClients = {MyServiceClient.class})
@Fluent
class MyServiceClientBuilder implements 
    HttpTrait<MyServiceClientBuilder>,
    TokenCredentialTrait<MyServiceClientBuilder>,
    EndpointTrait<MyServiceClientBuilder>,
    ConfigurationTrait<MyServiceClientBuilder> {
    
    private HttpClient httpClient;
    private HttpPipeline pipeline;
    private TokenCredential credential;
    private String endpoint;
    private Configuration configuration;
    private List<HttpPipelinePolicy> policies = new ArrayList<>();
    private RetryOptions retryOptions;
    private HttpLogOptions httpLogOptions;
    private ClientOptions clientOptions;
    
    @Override
    public MyServiceClientBuilder httpClient(HttpClient httpClient) {
        this.httpClient = httpClient;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder pipeline(HttpPipeline pipeline) {
        this.pipeline = pipeline;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder addPolicy(HttpPipelinePolicy pipelinePolicy) {
        this.policies.add(pipelinePolicy);
        return this;
    }
    
    @Override
    public MyServiceClientBuilder retryOptions(RetryOptions retryOptions) {
        this.retryOptions = retryOptions;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder httpLogOptions(HttpLogOptions logOptions) {
        this.httpLogOptions = logOptions;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder clientOptions(ClientOptions clientOptions) {
        this.clientOptions = clientOptions;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder credential(TokenCredential credential) {
        this.credential = credential;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder endpoint(String endpoint) {
        this.endpoint = endpoint;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder configuration(Configuration configuration) {
        this.configuration = configuration;
        return this;
    }
    
    public MyServiceClient buildClient() {
        HttpPipeline pipeline = buildPipeline();
        return new MyServiceClient(pipeline, endpoint);
    }
    
    private HttpPipeline buildPipeline() {
        if (pipeline != null) {
            return pipeline;
        }
        
        List<HttpPipelinePolicy> allPolicies = new ArrayList<>();
        
        // Add user agent policy
        allPolicies.add(new UserAgentPolicy("MyService/1.0"));
        
        // Add authentication policy
        if (credential != null) {
            allPolicies.add(new BearerTokenAuthenticationPolicy(
                credential, "https://myservice.azure.com/.default"));
        }
        
        // Add user-defined policies
        allPolicies.addAll(policies);
        
        // Add retry policy
        if (retryOptions != null) {
            allPolicies.add(new RetryPolicy(retryOptions));
        } else {
            allPolicies.add(new RetryPolicy());
        }
        
        // Add logging policy
        if (httpLogOptions != null) {
            allPolicies.add(new HttpLoggingPolicy(httpLogOptions));
        }
        
        return new HttpPipelineBuilder()
            .httpClient(httpClient != null ? httpClient : HttpClient.createDefault())
            .policies(allPolicies.toArray(new HttpPipelinePolicy[0]))
            .build();
    }
}

Using Different Credential Traits

// Builder supporting multiple credential types
@ServiceClientBuilder(serviceClients = {StorageClient.class})
class StorageClientBuilder implements 
    AzureKeyCredentialTrait<StorageClientBuilder>,
    AzureSasCredentialTrait<StorageClientBuilder>,
    TokenCredentialTrait<StorageClientBuilder>,
    ConnectionStringTrait<StorageClientBuilder> {
    
    private Object credential;
    private String connectionString;
    
    @Override
    public StorageClientBuilder credential(AzureKeyCredential credential) {
        this.credential = credential;
        return this;
    }
    
    @Override
    public StorageClientBuilder credential(AzureSasCredential credential) {
        this.credential = credential;
        return this;
    }
    
    @Override
    public StorageClientBuilder credential(TokenCredential credential) {
        this.credential = credential;
        return this;
    }
    
    @Override
    public StorageClientBuilder connectionString(String connectionString) {
        this.connectionString = connectionString;
        return this;
    }
    
    public StorageClient buildClient() {
        // Build client based on credential type
        if (connectionString != null) {
            return StorageClient.fromConnectionString(connectionString);
        } else if (credential instanceof TokenCredential) {
            return new StorageClient((TokenCredential) credential);
        } else if (credential instanceof AzureKeyCredential) {
            return new StorageClient((AzureKeyCredential) credential);
        } else if (credential instanceof AzureSasCredential) {
            return new StorageClient((AzureSasCredential) credential);
        } else {
            throw new IllegalStateException("No credential or connection string provided");
        }
    }
}

Fluent Configuration Chain

import com.azure.core.http.policy.*;

// Example of fluent builder configuration
MyServiceClient client = new MyServiceClientBuilder()
    .endpoint("https://myservice.azure.com")
    .credential(new DefaultAzureCredentialBuilder().build())
    .httpClient(HttpClient.createDefault())
    .addPolicy(new UserAgentPolicy("MyApp/1.0"))
    .addPolicy(new RequestIdPolicy())
    .retryOptions(new RetryOptions()
        .setMaxRetries(5)
        .setDelay(Duration.ofSeconds(1))
        .setMaxDelay(Duration.ofSeconds(30)))
    .httpLogOptions(new HttpLogOptions()
        .setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)
        .addAllowedHeaderName("x-custom-header"))
    .clientOptions(new ClientOptions()
        .setApplicationId("MyApplication")
        .setHeaders(Map.of("x-client-name", "MyApp")))
    .configuration(Configuration.getGlobalConfiguration())
    .buildClient();

Custom Configuration Trait

// Custom trait for service-specific configuration
interface MyServiceConfigurationTrait<T> {
    T region(String region);
    T enableCaching(boolean enableCaching);
    T timeout(Duration timeout);
}

class MyServiceClientBuilder implements 
    HttpTrait<MyServiceClientBuilder>,
    TokenCredentialTrait<MyServiceClientBuilder>,
    MyServiceConfigurationTrait<MyServiceClientBuilder> {
    
    private String region;
    private boolean enableCaching = true;
    private Duration timeout = Duration.ofMinutes(5);
    
    @Override
    public MyServiceClientBuilder region(String region) {
        this.region = region;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder enableCaching(boolean enableCaching) {
        this.enableCaching = enableCaching;
        return this;
    }
    
    @Override
    public MyServiceClientBuilder timeout(Duration timeout) {
        this.timeout = timeout;
        return this;
    }
    
    // ... other trait implementations
}

Multi-Service Builder Pattern

// Builder that can create multiple service clients
@ServiceClientBuilder(serviceClients = {ServiceAClient.class, ServiceBClient.class})
class MultiServiceClientBuilder implements
    HttpTrait<MultiServiceClientBuilder>,
    TokenCredentialTrait<MultiServiceClientBuilder>,
    EndpointTrait<MultiServiceClientBuilder> {
    
    // Shared configuration
    private HttpClient httpClient;
    private TokenCredential credential;
    private String baseEndpoint;
    
    public ServiceAClient buildServiceAClient() {
        String endpoint = baseEndpoint + "/serviceA";
        HttpPipeline pipeline = buildPipeline();
        return new ServiceAClient(pipeline, endpoint);
    }
    
    public ServiceBClient buildServiceBClient() {
        String endpoint = baseEndpoint + "/serviceB";
        HttpPipeline pipeline = buildPipeline();
        return new ServiceBClient(pipeline, endpoint);
    }
    
    // ... trait implementations and pipeline building
}

// Usage
MultiServiceClientBuilder builder = new MultiServiceClientBuilder()
    .endpoint("https://api.myservice.com")
    .credential(credential)
    .httpClient(httpClient);

ServiceAClient clientA = builder.buildServiceAClient();
ServiceBClient clientB = builder.buildServiceBClient();

Install with Tessl CLI

npx tessl i tessl/maven-com-azure--azure-core

docs

annotations.md

authentication.md

client-traits.md

cryptography.md

exceptions.md

http-client.md

http-policies.md

index.md

models.md

serialization.md

utilities.md

tile.json