mirror of
https://dev.azure.com/TeilRad/sharee.bike%20App/_git/Code
synced 2024-11-16 15:16:34 +01:00
65 lines
2.8 KiB
C#
65 lines
2.8 KiB
C#
using System;
|
|
using Serilog;
|
|
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;
|
|
|
|
/// <summary> Deserializes response JSON if response is of supported version or provides default response otherwise. </summary>
|
|
/// <typeparam name="T">Type of response object.</typeparam>
|
|
/// <param name="response">Response JSON.</param>
|
|
/// <param name="emptyResponseFactory">Factory providing default delegate.</param>
|
|
/// <returns>Response object.</returns>
|
|
public static T DeserializeResponse<T>(this string response, Func<string, T> emptyResponseFactory) where T : class
|
|
{
|
|
// Get COPRI version from respone.
|
|
var bikeInfoBase = JsonConvertRethrow.DeserializeObject<VersionindependentResponse>(response)?.shareejson;
|
|
|
|
if (bikeInfoBase.GetCopriVersion() < UNSUPPORTEDFUTURECOPRIVERSIONLOWER
|
|
|| bikeInfoBase.GetCopriVersion() >= UNSUPPORTEDFUTURECOPRIVERSIONUPPER)
|
|
{
|
|
return emptyResponseFactory?.Invoke(bikeInfoBase.copri_version) ?? null;
|
|
}
|
|
|
|
return JsonConvertRethrow.DeserializeObject<ResponseContainer<T>>(response)?.shareejson;
|
|
}
|
|
|
|
/// <summary> Deserializes reponse JSON if response is of supported version or throws an exception. </summary>
|
|
/// <typeparam name="T">Type of response object.</typeparam>
|
|
/// <param name="response">Response JSON.</param>
|
|
/// <param name="unsupportedVersionExectpion">Exception to fire.</param>
|
|
/// <returns>Response object.</returns>
|
|
public static T DeserializeResponse<T>(this string response, Func<string, System.Exception> unsupportedVersionExectpion = null) where T : class
|
|
{
|
|
|
|
// Get COPRI version from respone.
|
|
var bikeInfoBase = JsonConvertRethrow.DeserializeObject<VersionindependentResponse>(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<ResponseContainer<T>>(response)?.shareejson;
|
|
}
|
|
}
|
|
}
|