birschick-bq commented on code in PR #2014:
URL: https://github.com/apache/arrow-adbc/pull/2014#discussion_r1685087879
##########
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'");
+
+ private static Uri GetBaseAddress(string? hostName, string? scheme,
string? path, string? port)
+ {
+ bool isPortSet = !string.IsNullOrEmpty(port);
+ bool isValidPortNumber = int.TryParse(port, out int portNumber) &&
portNumber > 0;
+ bool isDefaultHttpsPort = !isPortSet || (isValidPortNumber &&
portNumber == 443);
+ string uriScheme = Uri.CheckSchemeName(scheme)
+ ? scheme!
+ : isDefaultHttpsPort
+ ? Uri.UriSchemeHttps
+ : Uri.UriSchemeHttp;
+ int uriPort = isValidPortNumber ? portNumber : -1;
Review Comment:
Good catch. Changed to throw an error if port number is invalid/out of range.
--
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]