Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replaced custom methods with string methods #2801

Merged
merged 2 commits into from
Sep 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ private void SetKey(byte[] key)

private void SetKeyFromBase64String(string key)
{
if (key.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentNullException(nameof(key));
}
Expand All @@ -99,7 +99,7 @@ private void SetKeyFromBase64String(string key)

private void SetDeviceId(string deviceId)
{
if (deviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public IotHubConnectionStringBuilder Populate(IotHubConnectionStringBuilder iotH

private void SetDeviceId(string deviceId)
{
if (deviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
Expand All @@ -87,7 +87,7 @@ private void SetDeviceId(string deviceId)

private void SetKey(string key)
{
if (key.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentNullException(nameof(key));
}
Expand All @@ -102,7 +102,7 @@ private void SetKey(string key)

private void SetPolicyName(string policyName)
{
if (policyName.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(policyName))
{
throw new ArgumentNullException(nameof(policyName));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public IotHubConnectionStringBuilder Populate(IotHubConnectionStringBuilder iotH

private void SetDeviceId(string deviceId)
{
if (deviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
Expand All @@ -75,7 +75,7 @@ private void SetDeviceId(string deviceId)

private void SetToken(string token)
{
if (token.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(token))
{
throw new ArgumentNullException(nameof(token));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public DeviceAuthenticationWithTokenRefresh(
SetSasTokenRenewalBufferPercentage(timeBufferPercentage),
disposeWithClient)
{
if (deviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public IotHubConnectionStringBuilder Populate(IotHubConnectionStringBuilder iotH

private void SetDeviceId(string deviceId)
{
if (deviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public IotHubConnectionString(IotHubConnectionStringBuilder builder)
}
else if (!string.IsNullOrEmpty(SharedAccessKey))
{
if (ModuleId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(ModuleId))
{
// Since the SDK creates the instance of disposable DeviceAuthenticationWithSakRefresh, the SDK needs to
// dispose it once the client is disposed.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public static IotHubConnectionStringBuilder Create(string hostname, string gatew
/// <returns>A new instance of the <see cref="IotHubConnectionStringBuilder"/> class with a populated connection string.</returns>
public static IotHubConnectionStringBuilder Create(string iotHubConnectionString)
{
if (iotHubConnectionString.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(iotHubConnectionString))
{
throw new ArgumentNullException(nameof(iotHubConnectionString));
}
Expand Down Expand Up @@ -253,12 +253,12 @@ private void Parse(string iotHubConnectionString)

private void Validate()
{
if (DeviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(DeviceId))
{
throw new ArgumentException("DeviceId must be specified in connection string");
}

if (!(SharedAccessKey.IsNullOrWhiteSpace() ^ SharedAccessSignature.IsNullOrWhiteSpace()))
if (!string.IsNullOrWhiteSpace(SharedAccessKey) ^ string.IsNullOrWhiteSpace(SharedAccessSignature))
{
if (!(UsingX509Cert || AuthenticationMethod is AuthenticationWithTokenRefresh))
{
Expand All @@ -268,19 +268,19 @@ private void Validate()
}

if ((UsingX509Cert || Certificate != null) &&
(!SharedAccessKey.IsNullOrWhiteSpace()
|| !SharedAccessSignature.IsNullOrWhiteSpace()))
(!string.IsNullOrWhiteSpace(SharedAccessKey)
|| !string.IsNullOrWhiteSpace(SharedAccessSignature)))
{
throw new ArgumentException(
"Should not specify either SharedAccessKey or SharedAccessSignature if X.509 certificate is used");
}

if (IotHubName.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(IotHubName))
{
throw new FormatException("Missing IoT hub name");
}

if (!SharedAccessKey.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(SharedAccessKey))
{
Convert.FromBase64String(SharedAccessKey);
}
Expand Down Expand Up @@ -313,7 +313,7 @@ private void Validate()

private void SetHostName(string hostname)
{
if (hostname.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(hostname))
{
throw new ArgumentNullException(nameof(hostname));
}
Expand All @@ -332,7 +332,7 @@ private void SetIotHubName()
// For transparent gateway scenarios, we can simplify the input credentials to only specify the gateway device hostname,
// instead of having to specify both the IoT hub hostname and the gateway device hostname.
// In this case, the hostname will be of the format "myGatewayDevice", and will not have ".azure-devices.net" suffix.
if (IotHubName.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(IotHubName))
{
if (Logging.IsEnabled)
Logging.Info(this, $"Connecting to a gateway device with hostname=[{HostName}]");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void SetKey(byte[] key)

private void SetKeyFromBase64String(string key)
{
if (key.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(key))
{
throw new ArgumentNullException(nameof(key));
}
Expand All @@ -112,7 +112,7 @@ private void SetKeyFromBase64String(string key)

private void SetDeviceId(string deviceId)
{
if (deviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
Expand All @@ -122,7 +122,7 @@ private void SetDeviceId(string deviceId)

private void SetModuleId(string moduleId)
{
if (moduleId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(moduleId))
{
throw new ArgumentNullException(nameof(moduleId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public IotHubConnectionStringBuilder Populate(IotHubConnectionStringBuilder iotH

private void SetDeviceId(string deviceId)
{
if (deviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
Expand All @@ -88,7 +88,7 @@ private void SetDeviceId(string deviceId)

private void SetModuleId(string moduleId)
{
if (moduleId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(moduleId))
{
throw new ArgumentNullException(nameof(moduleId));
}
Expand All @@ -98,7 +98,7 @@ private void SetModuleId(string moduleId)

private void SetToken(string token)
{
if (token.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(token))
{
throw new ArgumentNullException(nameof(token));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public ModuleAuthenticationWithTokenRefresh(
SetSasTokenRenewalBufferPercentage(timeBufferPercentage),
disposeWithClient)
{
if (moduleId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(moduleId))
{
throw new ArgumentNullException(nameof(moduleId));
}

if (deviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(deviceId))
{
throw new ArgumentNullException(nameof(deviceId));
}
Expand Down
5 changes: 0 additions & 5 deletions iothub/device/src/Common/Extensions/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,6 @@ public static void AppendKeyValuePairIfNotEmpty(this StringBuilder builder, stri
}
}

public static bool IsNullOrWhiteSpace(this string value)
{
return string.IsNullOrWhiteSpace(value);
}

public static int NthIndexOf(this string str, char value, int startIndex, int n)
{
if (str == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ private string BuildSignature(string keyName, string key, string target, TimeSpa
SharedAccessSignatureConstants.SignatureFieldName, WebUtility.UrlEncode(signature),
SharedAccessSignatureConstants.ExpiryFieldName, WebUtility.UrlEncode(expiresOn));

if (!keyName.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(keyName))
{
buffer.AppendFormat(CultureInfo.InvariantCulture, "&{0}={1}",
SharedAccessSignatureConstants.KeyNameFieldName, WebUtility.UrlEncode(keyName));
Expand Down
2 changes: 1 addition & 1 deletion iothub/device/src/Transport/Amqp/AmqpUnit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ public async Task<AmqpIotOutcome> DisposeMessageAsync(string lockToken, AmqpIotD
Logging.Enter(this, lockToken, nameof(DisposeMessageAsync));

AmqpIotOutcome disposeOutcome;
if (_deviceIdentity.IotHubConnectionString.ModuleId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(_deviceIdentity.IotHubConnectionString.ModuleId))
{
await EnsureMessageReceivingLinkIsOpenAsync(cancellationToken).ConfigureAwait(false);

Expand Down
4 changes: 2 additions & 2 deletions iothub/device/src/Transport/Amqp/DeviceIdentity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ internal DeviceIdentity(

private static string CreateAudience(IotHubConnectionString connectionString)
{
if (connectionString.SharedAccessKeyName.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(connectionString.SharedAccessKeyName))
{
return connectionString.ModuleId.IsNullOrWhiteSpace()
return string.IsNullOrWhiteSpace(connectionString.ModuleId)
? $"{connectionString.HostName}/devices/{WebUtility.UrlEncode(connectionString.DeviceId)}"
: $"{connectionString.HostName}/devices/{WebUtility.UrlEncode(connectionString.DeviceId)}/modules/{WebUtility.UrlEncode(connectionString.ModuleId)}";
}
Expand Down
4 changes: 2 additions & 2 deletions iothub/device/src/Transport/AmqpIot/AmqpIotSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ private static async Task<AmqpIotSendingLink> OpenSendingAmqpLinkAsync(
amqpLinkSettings.AddProperty(AmqpIotConstants.ChannelCorrelationId, correlationId);
}

if (!deviceIdentity.AmqpTransportSettings.AuthenticationChain.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(deviceIdentity.AmqpTransportSettings.AuthenticationChain))
{
amqpLinkSettings.AddProperty(AmqpIotConstants.AuthChain, deviceIdentity.AmqpTransportSettings.AuthenticationChain);
}
Expand Down Expand Up @@ -306,7 +306,7 @@ private static async Task<AmqpIotReceivingLink> OpenReceivingAmqpLinkAsync(
amqpLinkSettings.AddProperty(AmqpIotConstants.ClientVersion, deviceIdentity.ProductInfo.ToString());
amqpLinkSettings.AddProperty(AmqpIotConstants.ApiVersion, ClientApiVersionHelper.ApiVersionString);

if (!deviceIdentity.AmqpTransportSettings.AuthenticationChain.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(deviceIdentity.AmqpTransportSettings.AuthenticationChain))
{
amqpLinkSettings.AddProperty(AmqpIotConstants.AuthChain, deviceIdentity.AmqpTransportSettings.AuthenticationChain);
}
Expand Down
4 changes: 2 additions & 2 deletions iothub/device/src/Transport/Mqtt/MqttIotHubAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,14 +341,14 @@ private async Task ConnectAsync(IChannelHandlerContext context)

string usernameString = $"{_iotHubHostName}/{id}/?{ClientApiVersionHelper.ApiVersionQueryStringLatest}&{DeviceClientTypeParam}={Uri.EscapeDataString(_productInfo.ToString())}";

if (!_mqttTransportSettings.AuthenticationChain.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(_mqttTransportSettings.AuthenticationChain))
{
usernameString += $"&{AuthChainParam}={Uri.EscapeDataString(_mqttTransportSettings.AuthenticationChain)}";
}

// This check is added to enable the device or module client to available plug and play features. For devices or modules that pass in the model Id,
// the SDK will enable plug and play features by appending the model Id to the MQTT CONNECT packet (in the username).
if (!(_options?.ModelId).IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(_options?.ModelId))
{
usernameString += $"&{ModelIdParam}={Uri.EscapeDataString(_options.ModelId)}";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,12 @@ internal void Parse(string iotHubConnectionString)

internal void Validate()
{
if (SharedAccessKeyName.IsNullOrWhiteSpace() && DeviceId.IsNullOrWhiteSpace())
if (string.IsNullOrWhiteSpace(SharedAccessKeyName) && string.IsNullOrWhiteSpace(DeviceId))
{
throw new ArgumentException("Should specify either SharedAccessKeyName or DeviceId");
}

if (!(SharedAccessKey.IsNullOrWhiteSpace() ^ SharedAccessSignature.IsNullOrWhiteSpace()))
if (!string.IsNullOrWhiteSpace(SharedAccessKey) ^ string.IsNullOrWhiteSpace(SharedAccessSignature))
{
throw new ArgumentException("Should specify either SharedAccessKey or SharedAccessSignature");
}
Expand All @@ -205,7 +205,7 @@ internal void Validate()
throw new FormatException("Missing IoT hub name");
}

if (!SharedAccessKey.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(SharedAccessKey))
{
Convert.FromBase64String(SharedAccessKey);
}
Expand All @@ -216,15 +216,15 @@ internal void Validate()
}

ValidateFormat(HostName, HostNamePropertyName, s_hostNameRegex);
if (!SharedAccessKeyName.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(SharedAccessKeyName))
{
ValidateFormatIfSpecified(SharedAccessKeyName, SharedAccessKeyNamePropertyName, s_sharedAccessKeyNameRegex);
}
if (!DeviceId.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(DeviceId))
{
ValidateFormatIfSpecified(DeviceId, DeviceIdPropertyName, s_idRegex);
}
if (!ModuleId.IsNullOrWhiteSpace())
if (!string.IsNullOrWhiteSpace(ModuleId))
{
ValidateFormatIfSpecified(ModuleId, ModuleIdPropertyName, s_idRegex);
}
Expand Down
10 changes: 0 additions & 10 deletions iothub/service/src/Common/Extensions/CommonExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -244,16 +244,6 @@ public static void AppendKeyValuePairIfNotEmpty(this StringBuilder builder, stri
}
}

/// <summary>
/// Check if the value is null or empty.
/// </summary>
/// <param name="value">The value to check.</param>
/// <returns>Returns true if the value is null or empty, otherwise returns false.</returns>
public static bool IsNullOrWhiteSpace(this string value)
{
return string.IsNullOrWhiteSpace(value);
}

/// <summary>
/// Removes white spaces from a string.
/// </summary>
Expand Down