sharee.bike-App/TINKLib/Repository/CopriCallsStatic.cs

66 lines
2.8 KiB
C#
Raw Normal View History

2022-08-30 15:42:25 +02:00
using System;
using Serilog;
2021-06-26 20:57:55 +02:00
using TINK.Model.Connector;
2021-05-13 20:03:07 +02:00
using TINK.Repository.Response;
2021-06-26 20:57:55 +02:00
namespace TINK.Repository
2021-05-13 20:03:07 +02:00
{
2022-09-06 16:08:19 +02:00
public static class CopriCallsStatic
{
2021-06-26 20:57:55 +02:00
#if !USCSHARP9
2022-09-06 16:08:19 +02:00
private static Version UNSUPPORTEDFUTURECOPRIVERSIONLOWER = new Version(4, 1);
2021-06-26 20:57:55 +02:00
#else
private static Version UNSUPPORTEDFUTURECOPRIVERSIONLOWER = new(4, 1);
#endif
#if !USCSHARP9
2022-09-06 16:08:19 +02:00
private static Version UNSUPPORTEDFUTURECOPRIVERSIONUPPER = new Version(4, 2);
2021-06-26 20:57:55 +02:00
#else
private static Version UNSUPPORTEDFUTURECOPRIVERSIONUPPER = new(4, 2);
#endif
2022-09-06 16:08:19 +02:00
public static Version UnsupportedVersionLower => UNSUPPORTEDFUTURECOPRIVERSIONLOWER;
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
public static Version UnsupportedVersionUpper => UNSUPPORTEDFUTURECOPRIVERSIONUPPER;
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
/// <summary> Deserializes reponse 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;
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
if (bikeInfoBase.GetCopriVersion() < UNSUPPORTEDFUTURECOPRIVERSIONLOWER
|| bikeInfoBase.GetCopriVersion() >= UNSUPPORTEDFUTURECOPRIVERSIONUPPER)
{
return emptyResponseFactory?.Invoke(bikeInfoBase.copri_version) ?? null;
}
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<T>>(response)?.shareejson;
}
2021-05-13 20:03:07 +02:00
2022-09-06 16:08:19 +02:00
/// <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
{
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
// Get COPRI version from respone.
var bikeInfoBase = JsonConvertRethrow.DeserializeObject<VersionindependentResponse>(response)?.shareejson;
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
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.");
}
2021-06-26 20:57:55 +02:00
2022-09-06 16:08:19 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<T>>(response)?.shareejson;
}
}
2021-05-13 20:03:07 +02:00
}