Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
Refs: #1150
  • Loading branch information
Phil91 committed Nov 20, 2024
1 parent 851f336 commit 41345b1
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 35 deletions.
3 changes: 2 additions & 1 deletion src/keycloak/Keycloak.Library/Localization/KeycloakClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,14 @@ public async Task<IEnumerable<KeyValuePair<string, string>>> GetLocaleAsync(stri

public async Task UpdateLocaleAsync(string realm, string locale, string key, string translation, CancellationToken cancellationToken)
{
using var content = new StringContent(translation, MediaTypeHeaderValue.Parse("text/plain"));
await (await GetBaseUrlAsync(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None))
.AppendPathSegment(AdminUrlSegment)
.AppendPathSegment(realm, true)
.AppendPathSegment(LocalizationUrlSegment)
.AppendPathSegment(locale, true)
.AppendPathSegment(key, true)
.PutAsync(new StringContent(translation, MediaTypeHeaderValue.Parse("text/plain")), cancellationToken: cancellationToken)
.PutAsync(content, cancellationToken: cancellationToken)
.ConfigureAwait(ConfigureAwaitOptions.None);
}

Expand Down
76 changes: 44 additions & 32 deletions src/keycloak/Keycloak.Seeding/BusinessLogic/LocalizationsUpdater.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/

using Org.Eclipse.TractusX.Portal.Backend.Framework.Linq;
using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Factory;
using Org.Eclipse.TractusX.Portal.Backend.Keycloak.Library;

Expand All @@ -43,44 +44,55 @@ public async Task UpdateLocalizations(string keycloakInstanceName, CancellationT
private static async Task UpdateLocaleTranslations(KeycloakClient keycloak, string realm, IEnumerable<string> locales,
IEnumerable<(string Locale, IEnumerable<KeyValuePair<string, string>> Translations)> translations, CancellationToken cancellationToken)
{
var localesToUpdate = locales.Join(
translations,
l => l,
trans => trans.Locale,
(l, trans) => (Locale: l, Update: trans));

if (!localesToUpdate.Any())
if (!await locales
.Join(
translations,
l => l,
trans => trans.Locale,
(l, trans) => (Locale: l, Update: trans))
.IfAnyAwait(async localesToUpdate =>
{
foreach (var (locale, update) in
localesToUpdate)
{
var localizations = await keycloak.GetLocaleAsync(realm, locale, cancellationToken: cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
await UpdateLocales(keycloak, realm, cancellationToken, update, localizations, locale).ConfigureAwait(ConfigureAwaitOptions.None);
await DeleteLocales(keycloak, realm, cancellationToken, localizations, update, locale).ConfigureAwait(ConfigureAwaitOptions.None);
}
}).ConfigureAwait(false))
{
await AddLocales(keycloak, realm, translations, cancellationToken);
return;
await AddLocales(keycloak, realm, translations, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
}
}

foreach (var (locale, update) in
localesToUpdate)
private static async Task DeleteLocales(KeycloakClient keycloak, string realm, CancellationToken cancellationToken,
IEnumerable<KeyValuePair<string, string>> localizations, (string Locale, IEnumerable<KeyValuePair<string, string>> Translations) update, string locale)
{
foreach (var deleteTranslation in
localizations.ExceptBy(update.Translations.Select(t => t.Key),
l => l.Key))
{
var localizations = await keycloak.GetLocaleAsync(realm, locale, cancellationToken: cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
foreach (var missingTranslation in update.Translations.ExceptBy(localizations.Select(loc => loc.Key),
locModel => locModel.Key))
{
await keycloak.UpdateLocaleAsync(realm, locale, missingTranslation.Key, missingTranslation.Value, cancellationToken).ConfigureAwait(false);
}
await keycloak.DeleteLocaleAsync(realm, locale, deleteTranslation.Key, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
}
}

foreach (var updateTranslation in
localizations.Join(
update.Translations,
l => l.Key,
trans => trans.Key,
(l, trans) => (Key: l.Key, Update: trans)))
{
await keycloak.UpdateLocaleAsync(realm, locale, updateTranslation.Key, updateTranslation.Update.Value, cancellationToken).ConfigureAwait(false);
}
private static async Task UpdateLocales(KeycloakClient keycloak, string realm, CancellationToken cancellationToken,
(string Locale, IEnumerable<KeyValuePair<string, string>> Translations) update, IEnumerable<KeyValuePair<string, string>> localizations, string locale)
{
foreach (var missingTranslation in update.Translations.ExceptBy(localizations.Select(loc => loc.Key),
locModel => locModel.Key))
{
await keycloak.UpdateLocaleAsync(realm, locale, missingTranslation.Key, missingTranslation.Value, cancellationToken).ConfigureAwait(false);
}

foreach (var deleteTranslation in
localizations.ExceptBy(update.Translations.Select(t => t.Key),
l => l.Key))
{
await keycloak.DeleteLocaleAsync(realm, locale, deleteTranslation.Key, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);
}
foreach (var updateTranslation in
localizations.Join(
update.Translations,
l => l.Key,
trans => trans.Key,
(l, trans) => (Key: l.Key, Update: trans)))
{
await keycloak.UpdateLocaleAsync(realm, locale, updateTranslation.Key, updateTranslation.Update.Value, cancellationToken).ConfigureAwait(false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public IReadOnlyDictionary<string, string> ClientsDictionary

public IEnumerable<(string Locale, IEnumerable<KeyValuePair<string, string>> Translations)> RealmLocalizations
{
get => _keycloakRealm?.LocalizationTexts?.FilterNotNullValues().Select(x => (x.Key, x.Value.Select(kvp => kvp))) ?? Enumerable.Empty<(string, IEnumerable<KeyValuePair<string, string>>)>();
get => _keycloakRealm?.LocalizationTexts?.FilterNotNullValues().Select(x => (x.Key, x.Value as IEnumerable<KeyValuePair<string, string>>)) ?? Enumerable.Empty<(string, IEnumerable<KeyValuePair<string, string>>)>();

Check warning

Code scanning / CodeQL

Useless upcast Warning

There is no need to upcast from
IDictionary<String,String>
to
IEnumerable<KeyValuePair<String,String>>
- the conversion can be done implicitly.
}

public async Task SetClientInternalIds(IAsyncEnumerable<(string ClientId, string Id)> clientInternalIds)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public async Task UpdateUserProfile(string keycloakInstanceName, CancellationTok

var userProfile = await keycloak.GetUsersProfile(realm, cancellationToken).ConfigureAwait(ConfigureAwaitOptions.None);

if (userProfiles == null || userProfiles.Count() != 1 || !userProfiles.Single().ComponentModel.Config.TryGetValue(UserProfileConfig, out var configs) || configs.Count() != 1)
if (userProfiles.Count() != 1 || !(userProfiles.Single().ComponentModel.Config?.TryGetValue(UserProfileConfig, out var configs) ?? false) || configs?.Count() != 1)
{
throw new ConflictException("There must be exactly one user profile");
}
Expand Down

0 comments on commit 41345b1

Please sign in to comment.