diff --git a/src/Tgstation.Server.Client/ApiClient.cs b/src/Tgstation.Server.Client/ApiClient.cs index 26aac71d13..ffffd1bf25 100644 --- a/src/Tgstation.Server.Client/ApiClient.cs +++ b/src/Tgstation.Server.Client/ApiClient.cs @@ -405,7 +405,7 @@ namespace Tgstation.Server.Client if (loggingConfigureAction != null) hubConnectionBuilder.ConfigureLogging(loggingConfigureAction); - async ValueTask AttemptConnect() + async ValueTask AttemptConnect() { hubConnection = hubConnectionBuilder.Build(); try @@ -454,25 +454,7 @@ namespace Tgstation.Server.Client } } - try - { - return await AttemptConnect(); - } - catch (HttpRequestException ex) - { - // status code is not in netstandard - var propertyInfo = ex.GetType().GetProperty("StatusCode"); - if (propertyInfo != null) - { - var statusCode = (HttpStatusCode)propertyInfo.GetValue(ex); - if (statusCode != HttpStatusCode.Unauthorized) - throw; - } - - await RefreshToken(cancellationToken); - - return await AttemptConnect(); - } + return await WrapHubInitialConnectAuthRefresh(AttemptConnect, cancellationToken); } /// @@ -594,6 +576,35 @@ namespace Tgstation.Server.Client } #pragma warning restore CA1506 + /// + /// Wrap a hub connection attempt via a with proper token refreshing. + /// + /// The . + /// The for the operation. + /// A resulting in the connected . + async ValueTask WrapHubInitialConnectAuthRefresh(Func> connectFunc, CancellationToken cancellationToken) + { + try + { + return await connectFunc(); + } + catch (HttpRequestException ex) + { + // status code is not in netstandard + var propertyInfo = ex.GetType().GetProperty("StatusCode"); + if (propertyInfo != null) + { + var statusCode = (HttpStatusCode)propertyInfo.GetValue(ex); + if (statusCode != HttpStatusCode.Unauthorized) + throw; + } + + await RefreshToken(cancellationToken); + + return await connectFunc(); + } + } + /// /// Main request method. ///