2023-05-09 08:47:52 +02:00
using System ;
2022-08-30 15:42:25 +02:00
using Serilog ;
2024-04-09 12:53:23 +02:00
using ShareeBike.Model.Connector ;
using ShareeBike.Repository.Response ;
2021-05-13 20:03:07 +02:00
2024-04-09 12:53:23 +02:00
namespace ShareeBike.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
2023-05-09 08:47:52 +02:00
/// <summary> Deserializes response JSON if response is of supported version or provides default response otherwise. </summary>
2022-09-06 16:08:19 +02:00
/// <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
{
2023-11-21 15:26:57 +01:00
// Get COPRI version from response.
2022-09-06 16:08:19 +02:00
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
2023-11-21 15:26:57 +01:00
/// <summary> Deserializes response JSON if response is of supported version or throws an exception. </summary>
2022-09-06 16:08:19 +02:00
/// <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>
2023-11-21 15:26:57 +01:00
public static T DeserializeResponse < T > (
this string response ,
Func < string , System . Exception > unsupportedVersionExectpion = null ) where T : class
2022-09-06 16:08:19 +02:00
{
2021-06-26 20:57:55 +02:00
2023-11-21 15:26:57 +01:00
// Get COPRI version from response.
2022-09-06 16:08:19 +02:00
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
}