Skip to content

Commit

Permalink
feat(ConfigurationApi): Adjust the return value of GetRawByKeyAsync m…
Browse files Browse the repository at this point in the history
…ethod under adjust ConfigurationApiClient (#320)

* feat(ConfigurationApi): Adjust the return value of GetRawByKeyAsync method under adjust ConfigurationApiClient

* fix: fix dcc test build error

* chore: remove commented code

* chore: Code format

Co-authored-by: yanpengju <[email protected]>
  • Loading branch information
codding-y and yanpengju authored Nov 2, 2022
1 parent d38e634 commit 4f88639
Show file tree
Hide file tree
Showing 13 changed files with 116 additions and 176 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Configuration.ConfigurationApi.Dcc.Internal;
namespace Masa.BuildingBlocks.StackSdks.Dcc.Contracts.Enum;

internal enum ConfigFormats
public enum ConfigFormats
{
Properties = 1,
Raw,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

namespace Masa.Contrib.Configuration.ConfigurationApi.Dcc.Internal.Model;
namespace Masa.BuildingBlocks.StackSdks.Dcc.Contracts.Model;

internal class PublishRelease
public class PublishReleaseModel
{
private ConfigFormats _configFormat;
public ConfigFormats ConfigFormat
Expand All @@ -13,7 +13,7 @@ public ConfigFormats ConfigFormat
try
{
if (_configFormat == 0 && !string.IsNullOrWhiteSpace(FormatLabelCode))
_configFormat = (ConfigFormats)Enum.Parse(typeof(ConfigFormats), FormatLabelCode);
_configFormat = (ConfigFormats)System.Enum.Parse(typeof(ConfigFormats), FormatLabelCode);
}
catch (Exception ex)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) MASA Stack All rights reserved.
// Licensed under the MIT License. See LICENSE.txt in the project root for license information.

global using Masa.BuildingBlocks.StackSdks.Dcc.Contracts.Enum;
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,18 @@ private async Task<dynamic> GetDynamicInternalAsync(string key, Action<string, d
protected virtual async Task<(string Raw, ConfigurationTypes ConfigurationType)> GetRawByKeyAsync(string key,
Action<string>? valueChanged)
{
var raw = await _client.GetAsync<string>(key, value =>
var publishRelease = await _client.GetAsync<PublishReleaseModel>(key, value =>
{
var result = FormatRaw(value, key);
valueChanged?.Invoke(result.Raw);
});

return FormatRaw(raw, key);
return FormatRaw(publishRelease, key);
}

protected virtual (string Raw, ConfigurationTypes ConfigurationType) FormatRaw(string? raw, string paramName)
protected virtual (string Raw, ConfigurationTypes ConfigurationType) FormatRaw(PublishReleaseModel? publishRelease, string key)
{
PublishRelease result = GetPublishRelease(raw, paramName);
PublishReleaseModel result = FormatPublishRelease(publishRelease, key);

switch (result.ConfigFormat)
{
Expand All @@ -158,7 +158,7 @@ protected virtual (string Raw, ConfigurationTypes ConfigurationType) FormatRaw(s
catch (Exception exception)
{
_logger?.LogWarning(exception,
"Dcc.ConfigurationApiClient: configObject invalid, {ParamName} is not a valid Properties type", paramName);
"Dcc.ConfigurationApiClient: configObject invalid, {ParamName} is not a valid Properties type", key);
throw new ArgumentException("configObject invalid");
}

Expand All @@ -171,7 +171,7 @@ protected virtual (string Raw, ConfigurationTypes ConfigurationType) FormatRaw(s
catch (Exception exception)
{
_logger?.LogWarning(exception, "Dcc.ConfigurationApiClient: configObject invalid, {ParamName} is not a valid Xml type",
paramName);
key);
throw new ArgumentException("configObject invalid");
}

Expand All @@ -186,7 +186,7 @@ protected virtual (string Raw, ConfigurationTypes ConfigurationType) FormatRaw(s
catch (Exception exception)
{
_logger?.LogWarning(exception, "Dcc.ConfigurationApiClient: configObject invalid, {ParamName} is not a valid Yaml type",
paramName);
key);
throw new ArgumentException("configObject invalid");
}

Expand All @@ -198,35 +198,24 @@ protected virtual (string Raw, ConfigurationTypes ConfigurationType) FormatRaw(s
private string FomartKey(string environment, string cluster, string appId, string configObject)
=> $"{GetEnvironment(environment)}-{GetCluster(cluster)}-{GetAppId(appId)}-{GetConfigObject(configObject)}".ToLower();

private PublishRelease GetPublishRelease(string? raw, string paramName)
private PublishReleaseModel FormatPublishRelease(PublishReleaseModel? publishRelease, string key)
{
if (raw == null)
throw new ArgumentException($"configObject invalid, {paramName} is not null");
if (publishRelease == null)
throw new ArgumentException($"configObject invalid, {key} is not null");

PublishRelease? result;
try
{
result = JsonSerializer.Deserialize<PublishRelease>(raw, _jsonSerializerOptions);
}
catch (Exception exception)
{
_logger?.LogWarning(exception, "Dcc.ConfigurationApiClient: configObject invalid, {ParamName} is not a valid response value",
paramName);
throw new ArgumentException($"Dcc.ConfigurationApiClient: configObject invalid, {paramName} is not a valid response value");
}
if (result == null || result.ConfigFormat == 0)
throw new ArgumentException($"Dcc.ConfigurationApiClient: configObject invalid, {paramName} is an unsupported type");
if (publishRelease.ConfigFormat == 0)
throw new ArgumentException($"Dcc.ConfigurationApiClient: configObject invalid, {key} is an unsupported type");

if (result.Encryption)
if (publishRelease.Encryption)
{
if (string.IsNullOrEmpty(_dccOptions.ConfigObjectSecret))
{
throw new ArgumentNullException(_dccOptions.ConfigObjectSecret, nameof(_dccOptions.ConfigObjectSecret));
}
result.Content = DecryptContent(_dccOptions.ConfigObjectSecret, result.Content);
publishRelease.Content = DecryptContent(_dccOptions.ConfigObjectSecret, publishRelease.Content);
}

return result;
return publishRelease;
}

private static string? DecryptContent(string secret, string? content)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

<ItemGroup>
<ProjectReference Include="..\..\..\..\BuildingBlocks\Configuration\Masa.BuildingBlocks.Configuration\Masa.BuildingBlocks.Configuration.csproj" />
<ProjectReference Include="..\..\..\..\BuildingBlocks\StackSdks\Dcc\Masa.BuildingBlocks.StackSdks.Dcc.Contracts\Masa.BuildingBlocks.StackSdks.Dcc.Contracts.csproj" />
<ProjectReference Include="..\..\..\Caching\Distributed\Masa.Contrib.Caching.Distributed.StackExchangeRedis\Masa.Contrib.Caching.Distributed.StackExchangeRedis.csproj" />
<ProjectReference Include="..\..\..\Caching\Masa.Contrib.Caching.MultilevelCache\Masa.Contrib.Caching.MultilevelCache.csproj" />
<ProjectReference Include="..\..\..\..\Utils\Security\Masa.Utils.Security.Cryptography\Masa.Utils.Security.Cryptography.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
global using Masa.BuildingBlocks.Data;
global using Masa.BuildingBlocks.Service.Caller;
global using Masa.BuildingBlocks.Service.Caller.Options;
global using Masa.BuildingBlocks.StackSdks.Dcc.Contracts.Enum;
global using Masa.BuildingBlocks.StackSdks.Dcc.Contracts.Model;
global using Masa.Contrib.Caching.Distributed.StackExchangeRedis;
global using Masa.Contrib.Caching.MultilevelCache;
global using Masa.Contrib.Configuration.ConfigurationApi.Dcc.Internal;
global using Masa.Contrib.Configuration.ConfigurationApi.Dcc.Internal.Model;
global using Masa.Contrib.Configuration.ConfigurationApi.Dcc.Internal.Parser;
Expand Down
Loading

0 comments on commit 4f88639

Please sign in to comment.