CurtHagenlocher commented on code in PR #2014:
URL: https://github.com/apache/arrow-adbc/pull/2014#discussion_r1682071919
##########
csharp/src/Drivers/Apache/Spark/SparkConnection.cs:
##########
@@ -269,32 +271,58 @@ protected override async ValueTask<TProtocol>
CreateProtocolAsync()
Trace.TraceError($"key = {property} value =
{properties[property]}");
}
- string hostName = properties[SparkParameters.HostName];
- string path = properties[SparkParameters.Path];
- string token;
-
- if (properties.ContainsKey(SparkParameters.Token))
- token = properties[SparkParameters.Token];
- else
- token = properties[SparkParameters.Password];
-
- HttpClient httpClient = new HttpClient();
- httpClient.BaseAddress = new UriBuilder(Uri.UriSchemeHttps,
hostName, -1, path).Uri;
- httpClient.DefaultRequestHeaders.Authorization = new
AuthenticationHeaderValue("Bearer", token);
+ properties.TryGetValue(SparkParameters.HostName, out string?
hostName);
+ properties.TryGetValue(SparkParameters.Scheme, out string? scheme);
+ properties.TryGetValue(SparkParameters.Path, out string? path);
+ properties.TryGetValue(SparkParameters.AuthType, out string?
authType);
+ properties.TryGetValue(SparkParameters.Token, out string? token);
+ properties.TryGetValue(SparkParameters.Username, out string?
username);
+ properties.TryGetValue(SparkParameters.Password, out string?
password);
+ properties.TryGetValue(SparkParameters.Port, out string? port);
+
+ Uri baseAddress = GetBaseAddress(hostName, scheme, path, port);
+ AuthenticationHeaderValue authenticationHeaderValue =
GetAuthenticationHeaderValue(authType, token, username, password);
+
+ HttpClient httpClient = new();
+ httpClient.BaseAddress = baseAddress;
+ httpClient.DefaultRequestHeaders.Authorization =
authenticationHeaderValue;
httpClient.DefaultRequestHeaders.UserAgent.ParseAdd(UserAgent);
httpClient.DefaultRequestHeaders.AcceptEncoding.Clear();
httpClient.DefaultRequestHeaders.AcceptEncoding.Add(new
StringWithQualityHeaderValue("identity"));
httpClient.DefaultRequestHeaders.ExpectContinue = false;
- TConfiguration config = new TConfiguration();
-
- ThriftHttpTransport transport = new
ThriftHttpTransport(httpClient, config);
+ TConfiguration config = new();
+ ThriftHttpTransport transport = new(httpClient, config);
// can switch to the one below if want to use the experimental one
with IPeekableTransport
// ThriftHttpTransport transport = new
ThriftHttpTransport(httpClient, config);
await transport.OpenAsync(CancellationToken.None);
return new TBinaryProtocol(transport);
}
+ private static AuthenticationHeaderValue
GetAuthenticationHeaderValue(string? authType, string? token, string? username,
string? password) =>
+ !string.IsNullOrEmpty(token)
+ && (string.IsNullOrEmpty(authType) ||
SparkAuthTypeConstants.AuthTypeToken.Equals(authType,
StringComparison.OrdinalIgnoreCase))
+ ? new AuthenticationHeaderValue("Bearer", token)
+ : !string.IsNullOrEmpty(username) &&
!string.IsNullOrEmpty(password)
+ && (string.IsNullOrEmpty(authType) ||
SparkAuthTypeConstants.AuthTypeBasic.Equals(authType,
StringComparison.OrdinalIgnoreCase))
+ ? new AuthenticationHeaderValue("Basic",
Convert.ToBase64String(Encoding.UTF8.GetBytes($"{username}:{password}")))
+ : throw new AdbcException("Missing connection properties.
Must contain 'token' or 'username' and 'password'");
Review Comment:
Nested ternary operators with complex conditions are hard to read. Consider
rewriting as an if/else if chain.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]