/* * Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0) * See https://github.com/openiddict/openiddict-core for more information concerning * the license and the contributors participating to this project. */ namespace OpenIddict.Client; /// /// Exposes extensions simplifying the integration with the OpenIddict client services. /// public static class OpenIddictClientHelpers { /// /// Retrieves a property value from the client transaction using the specified name. /// /// The type of the property. /// The client transaction. /// The property name. /// The property value or if it couldn't be found. public static TProperty? GetProperty( this OpenIddictClientTransaction transaction, string name) where TProperty : class { if (transaction is null) { throw new ArgumentNullException(nameof(transaction)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); } if (transaction.Properties.TryGetValue(name, out var property) && property is TProperty result) { return result; } return null; } /// /// Sets a property in the client transaction using the specified name and value. /// /// The type of the property. /// The client transaction. /// The property name. /// The property value. /// The client transaction, so that calls can be easily chained. public static OpenIddictClientTransaction SetProperty( this OpenIddictClientTransaction transaction, string name, TProperty? value) where TProperty : class { if (transaction is null) { throw new ArgumentNullException(nameof(transaction)); } if (string.IsNullOrEmpty(name)) { throw new ArgumentException(SR.GetResourceString(SR.ID0106), nameof(name)); } if (value is null) { transaction.Properties.Remove(name); } else { transaction.Properties[name] = value; } return transaction; } }