using Serilog; using System; using TINK.Model.Connector; using TINK.Repository.Response; namespace TINK.Repository { public static class CopriCallsStatic { #if !USCSHARP9 private static Version UNSUPPORTEDFUTURECOPRIVERSIONLOWER = new Version(4, 1); #else private static Version UNSUPPORTEDFUTURECOPRIVERSIONLOWER = new(4, 1); #endif #if !USCSHARP9 private static Version UNSUPPORTEDFUTURECOPRIVERSIONUPPER = new Version(4, 2); #else private static Version UNSUPPORTEDFUTURECOPRIVERSIONUPPER = new(4, 2); #endif public static Version UnsupportedVersionLower => UNSUPPORTEDFUTURECOPRIVERSIONLOWER; public static Version UnsupportedVersionUpper => UNSUPPORTEDFUTURECOPRIVERSIONUPPER; /// Deserializes reponse JSON if response is of supported version or provides default response otherwise. /// Type of response object. /// Response JSON. /// Factory providing default delegate. /// Response object. public static T DeserializeResponse(this string response, Func emptyResponseFactory) where T: class { // Get COPRI version from respone. var bikeInfoBase = JsonConvertRethrow.DeserializeObject(response)?.shareejson; if (bikeInfoBase.GetCopriVersion() < UNSUPPORTEDFUTURECOPRIVERSIONLOWER || bikeInfoBase.GetCopriVersion() >= UNSUPPORTEDFUTURECOPRIVERSIONUPPER) { return emptyResponseFactory?.Invoke(bikeInfoBase.copri_version) ?? null; } return JsonConvertRethrow.DeserializeObject>(response)?.shareejson; } /// Deserializes reponse JSON if response is of supported version or throws an exception. /// Type of response object. /// Response JSON. /// Exception to fire. /// Response object. public static T DeserializeResponse(this string response, Func unsupportedVersionExectpion = null) where T : class { // Get COPRI version from respone. var bikeInfoBase = JsonConvertRethrow.DeserializeObject(response)?.shareejson; if (bikeInfoBase.GetCopriVersion() < UNSUPPORTEDFUTURECOPRIVERSIONLOWER || bikeInfoBase.GetCopriVersion() >= UNSUPPORTEDFUTURECOPRIVERSIONUPPER) { Log.Error($"Unsupported copri version {bikeInfoBase.copri_version} detected on attempt to log in."); throw unsupportedVersionExectpion?.Invoke(bikeInfoBase.copri_version) ?? new System.Exception($"Unsupported COPRI version {bikeInfoBase.copri_version} detected."); } return JsonConvertRethrow.DeserializeObject>(response)?.shareejson; } } }