Created
May 24, 2019 12:03
-
-
Save dylanberry/b7e68619137a1ba2679e6ab4f0a94e5b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Http; | |
namespace VersionCheck.API.VersionCheck | |
{ | |
public interface IVersionCheckService | |
{ | |
void PerformVersionCheck(HttpRequest request); | |
string MinimumClientVersion { get; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
using System.Reflection; | |
using Microsoft.AspNetCore.Http; | |
using Microsoft.Extensions.Options; | |
using Microsoft.Extensions.Primitives; | |
using VersionCheck.API.Config; | |
using VersionCheck.Common; | |
namespace VersionCheck.API.VersionCheck | |
{ | |
public class VersionCheckService : IVersionCheckService | |
{ | |
public VersionCheckService(IOptions<AppSettings> appSettings) | |
{ | |
_appSettings = appSettings.Value; | |
} | |
public static readonly string ApiVersionString = Assembly.GetExecutingAssembly().GetName().Version.ToString(); | |
private readonly AppSettings _appSettings; | |
public string MinimumClientVersion => _appSettings.MinimumSupportedClientVersion; | |
public void PerformVersionCheck(HttpRequest request) | |
{ | |
var minimumSupportedClientVersionString = _appSettings.MinimumSupportedClientVersion; | |
if (request.Headers.TryGetValue(Common.HeaderKeys.ClientVersion, out StringValues clientVersionString) && | |
!string.IsNullOrWhiteSpace(minimumSupportedClientVersionString)) | |
{ | |
var clientVersion = new Version(clientVersionString.SingleOrDefault()); | |
var minimumSupportedAppVersion = new Version(minimumSupportedClientVersionString); | |
if (clientVersion.CompareTo(minimumSupportedAppVersion) < 0) | |
{ | |
// min supported version is > client version | |
throw new ClientVersionNotSupportedException(minimumSupportedClientVersionString, ApiVersionString, | |
clientVersionString); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment