-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #730 from xlegalles/CleanUpCode2
style: use file-scope namespace and primary constructor
- Loading branch information
Showing
20 changed files
with
728 additions
and
868 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,113 @@ | ||
using System; | ||
using Microsoft.Extensions.Logging; | ||
using Zeebe.Client.Api.Builder; | ||
using Zeebe.Client.Impl.Builder; | ||
|
||
namespace Zeebe.Client.Impl.Builder | ||
namespace Zeebe.Client.Impl.Builder; | ||
|
||
public class CamundaCloudClientBuilder : ICamundaCloudClientBuilder, ICamundaCloudClientBuilderStep1, ICamundaCloudClientBuilderStep2, ICamundaCloudClientBuilderFinalStep | ||
{ | ||
public class CamundaCloudClientBuilder : ICamundaCloudClientBuilder, ICamundaCloudClientBuilderStep1, ICamundaCloudClientBuilderStep2, ICamundaCloudClientBuilderFinalStep | ||
private const string ZeebeAddressEnvVar = "ZEEBE_ADDRESS"; | ||
private const string ZeebeClientIdEnvVar = "ZEEBE_CLIENT_ID"; | ||
private const string ZeebeClientSecretEnvVar = "ZEEBE_CLIENT_SECRET"; | ||
private const string ZeebeAuthServerEnvVar = "ZEEBE_AUTHORIZATION_SERVER_URL"; | ||
|
||
private readonly CamundaCloudTokenProviderBuilder camundaCloudTokenProviderBuilder; | ||
private string gatewayAddress; | ||
private ILoggerFactory loggerFactory; | ||
|
||
private CamundaCloudClientBuilder() | ||
{ | ||
private const string ZeebeAddressEnvVar = "ZEEBE_ADDRESS"; | ||
private const string ZeebeClientIdEnvVar = "ZEEBE_CLIENT_ID"; | ||
private const string ZeebeClientSecretEnvVar = "ZEEBE_CLIENT_SECRET"; | ||
private const string ZeebeAuthServerEnvVar = "ZEEBE_AUTHORIZATION_SERVER_URL"; | ||
camundaCloudTokenProviderBuilder = CamundaCloudTokenProvider.Builder(); | ||
} | ||
|
||
private readonly CamundaCloudTokenProviderBuilder camundaCloudTokenProviderBuilder; | ||
private string gatewayAddress; | ||
private ILoggerFactory loggerFactory; | ||
public static ICamundaCloudClientBuilder Builder() | ||
{ | ||
return new CamundaCloudClientBuilder(); | ||
} | ||
|
||
private CamundaCloudClientBuilder() | ||
{ | ||
camundaCloudTokenProviderBuilder = CamundaCloudTokenProvider.Builder(); | ||
} | ||
public ICamundaCloudClientBuilderStep1 UseClientId(string clientId) | ||
{ | ||
camundaCloudTokenProviderBuilder.UseClientId(clientId); | ||
return this; | ||
} | ||
|
||
public static ICamundaCloudClientBuilder Builder() | ||
{ | ||
return new CamundaCloudClientBuilder(); | ||
} | ||
public ICamundaCloudClientBuilderStep2 UseClientSecret(string clientSecret) | ||
{ | ||
camundaCloudTokenProviderBuilder.UseClientSecret(clientSecret); | ||
return this; | ||
} | ||
|
||
public ICamundaCloudClientBuilderFinalStep UseContactPoint(string contactPoint) | ||
{ | ||
_ = contactPoint ?? throw new ArgumentNullException(nameof(contactPoint)); | ||
|
||
public ICamundaCloudClientBuilderStep1 UseClientId(string clientId) | ||
if (!contactPoint.EndsWith(":443")) | ||
{ | ||
camundaCloudTokenProviderBuilder.UseClientId(clientId); | ||
return this; | ||
gatewayAddress = contactPoint + ":443"; | ||
camundaCloudTokenProviderBuilder.UseAudience(contactPoint); | ||
} | ||
|
||
public ICamundaCloudClientBuilderStep2 UseClientSecret(string clientSecret) | ||
else | ||
{ | ||
camundaCloudTokenProviderBuilder.UseClientSecret(clientSecret); | ||
return this; | ||
gatewayAddress = contactPoint; | ||
camundaCloudTokenProviderBuilder.UseAudience(contactPoint.Replace(":443", "")); | ||
} | ||
|
||
public ICamundaCloudClientBuilderFinalStep UseContactPoint(string contactPoint) | ||
{ | ||
_ = contactPoint ?? throw new ArgumentNullException(nameof(contactPoint)); | ||
|
||
if (!contactPoint.EndsWith(":443")) | ||
{ | ||
gatewayAddress = contactPoint + ":443"; | ||
camundaCloudTokenProviderBuilder.UseAudience(contactPoint); | ||
} | ||
else | ||
{ | ||
gatewayAddress = contactPoint; | ||
camundaCloudTokenProviderBuilder.UseAudience(contactPoint.Replace(":443", "")); | ||
} | ||
return this; | ||
} | ||
|
||
return this; | ||
} | ||
public ICamundaCloudClientBuilderFinalStep UseLoggerFactory(ILoggerFactory loggerFactory) | ||
{ | ||
this.loggerFactory = loggerFactory; | ||
camundaCloudTokenProviderBuilder.UseLoggerFactory(this.loggerFactory); | ||
return this; | ||
} | ||
|
||
public ICamundaCloudClientBuilderFinalStep UseLoggerFactory(ILoggerFactory loggerFactory) | ||
public ICamundaCloudClientBuilderFinalStep UseAuthServer(string url) | ||
{ | ||
if (url is null) | ||
{ | ||
this.loggerFactory = loggerFactory; | ||
camundaCloudTokenProviderBuilder.UseLoggerFactory(this.loggerFactory); | ||
// use default | ||
return this; | ||
} | ||
|
||
public ICamundaCloudClientBuilderFinalStep UseAuthServer(string url) | ||
{ | ||
if (url is null) | ||
{ | ||
// use default | ||
return this; | ||
} | ||
|
||
camundaCloudTokenProviderBuilder.UseAuthServer(url); | ||
return this; | ||
} | ||
camundaCloudTokenProviderBuilder.UseAuthServer(url); | ||
return this; | ||
} | ||
|
||
public ICamundaCloudClientBuilderFinalStep UsePersistedStoragePath(string path) | ||
public ICamundaCloudClientBuilderFinalStep UsePersistedStoragePath(string path) | ||
{ | ||
if (path is null) | ||
{ | ||
if (path is null) | ||
{ | ||
// use default | ||
return this; | ||
} | ||
|
||
camundaCloudTokenProviderBuilder.UsePath(path); | ||
// use default | ||
return this; | ||
} | ||
|
||
private string GetFromEnv(string key) | ||
{ | ||
char[] charsToTrim = { ' ', '\'' }; | ||
return Environment.GetEnvironmentVariable(key)?.Trim(charsToTrim); | ||
} | ||
camundaCloudTokenProviderBuilder.UsePath(path); | ||
return this; | ||
} | ||
|
||
public ICamundaCloudClientBuilderFinalStep FromEnv() | ||
{ | ||
this.UseClientId(GetFromEnv(ZeebeClientIdEnvVar)) | ||
.UseClientSecret(GetFromEnv(ZeebeClientSecretEnvVar)) | ||
.UseContactPoint(GetFromEnv(ZeebeAddressEnvVar)) | ||
.UseAuthServer(GetFromEnv(ZeebeAuthServerEnvVar)); | ||
return this; | ||
} | ||
private string GetFromEnv(string key) | ||
{ | ||
char[] charsToTrim = [' ', '\'']; | ||
return Environment.GetEnvironmentVariable(key)?.Trim(charsToTrim); | ||
} | ||
|
||
public IZeebeClient Build() | ||
{ | ||
return ZeebeClient.Builder() | ||
.UseLoggerFactory(loggerFactory) | ||
.UseGatewayAddress(gatewayAddress) | ||
.UseTransportEncryption() | ||
.UseAccessTokenSupplier(camundaCloudTokenProviderBuilder.Build()) | ||
.Build(); | ||
} | ||
public ICamundaCloudClientBuilderFinalStep FromEnv() | ||
{ | ||
UseClientId(GetFromEnv(ZeebeClientIdEnvVar)) | ||
.UseClientSecret(GetFromEnv(ZeebeClientSecretEnvVar)) | ||
.UseContactPoint(GetFromEnv(ZeebeAddressEnvVar)) | ||
.UseAuthServer(GetFromEnv(ZeebeAuthServerEnvVar)); | ||
return this; | ||
} | ||
|
||
public IZeebeClient Build() | ||
{ | ||
return ZeebeClient.Builder() | ||
.UseLoggerFactory(loggerFactory) | ||
.UseGatewayAddress(gatewayAddress) | ||
.UseTransportEncryption() | ||
.UseAccessTokenSupplier(camundaCloudTokenProviderBuilder.Build()) | ||
.Build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.