using System.Linq;
using TINK.Repository.Exception;
using TINK.MultilingualResources;
namespace TINK.Repository.Response
{
public static class ResponseHelper
{
public const string RESPONSE_OK = "OK";
/// Holds the description of the action return bike.
public const string RESPONSE_AUTHCOOKIE_EXPRIED = "Failure 1001:";
/// Holds the description of the action logout.
public const string BIKES_LOGOUT_ACTIONTEXT = "Abmeldung fehlgeschlagen.";
/// Holds the description of the action get stations available.
public const string STATIONS_AVAILABLE_ACTIONTEXT = "Abfrage der verfügbaren Stationen fehlgeschlagen.";
/// Holds the description of the action get bikes available.
public const string BIKES_AVAILABLE_ACTIONTEXT = "Abfrage der verfügbaren Fahrräder fehlgeschlagen.";
/// Holds the description of the action get bikes occupied.
public const string BIKES_OCCUPIED_ACTIONTEXT = "Abfrage der reservierten/ gebuchten Fahrräder fehlgeschlagen.";
/// Holds the description of the action cancel reservation.
public const string BIKES_CANCELREQUEST_ACTIONTEXT = "Aufheben der Reservierung fehlgeschlagen.";
/// Holds the description of the action return bike.
public const string BIKES_RETURNBIKE_ACTIONTEXT = "Rückgabe des Rads fehlgeschlagen.";
///
/// Checks if log in response is ok.
///
/// Response to check whether it is valid.
/// Mail addess used to create details error message in case log in response is invalid.
///
public static AuthorizationResponse GetIsResponseOk(this AuthorizationResponse response, string mail)
{
if (response == null)
{
throw new InvalidResponseException("Anmeldung fehlgeschlagen.", null);
}
if (response.response_state.ToUpper() == InvalidAuthorizationResponseException.AUTH_FAILURE_STATUS_MESSAGE_UPPERCASE)
{
throw new InvalidAuthorizationResponseException(mail, response);
}
else if (!response.response_state.Trim().ToUpper().StartsWith(RESPONSE_OK))
{
throw new InvalidResponseException($"Anmeldung {mail} fehlgeschlagen.\r\nServer Antwort: {response.response_text}", response);
}
return response;
}
///
/// Checks if log out response is ok.
///
/// Response to check whether it is valid.
///
public static AuthorizationoutResponse GetIsResponseOk(this AuthorizationoutResponse p_oResponse)
{
if (AuthcookieNotDefinedException.IsAuthcookieNotDefined(p_oResponse, BIKES_LOGOUT_ACTIONTEXT, out AuthcookieNotDefinedException exception))
{
throw exception;
}
GetIsResponseOk(p_oResponse, BIKES_LOGOUT_ACTIONTEXT);
if (p_oResponse.authcookie != "1")
{
throw new InvalidResponseException(
BIKES_LOGOUT_ACTIONTEXT,
p_oResponse);
}
return p_oResponse;
}
/// Gets if a call to reserve bike succeeded or not by checking a booking response.
/// Id of bike which should be booked.
/// Sessiong cookie of logged in user.
/// Response to check.
///
public static BikeInfoReservedOrBooked GetIsReserveResponseOk(
this ReservationBookingResponse bookingResponse,
string bikeId)
{
GetIsResponseOk(bookingResponse, string.Format(AppResources.ExceptionTextReservationBikeFailedGeneral, bikeId));
if (BookingDeclinedException.IsBookingDeclined(bookingResponse.response_state, out BookingDeclinedException exception))
{
throw exception;
}
// Get bike which has to be booked.
var bikeInfoRequestedOccupied = bookingResponse?.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
if (bikeInfoRequestedOccupied == null)
{
throw new System.Exception(string.Format(
AppResources.ExceptionTextReservationBikeFailedUnavailalbe,
bikeId,
!string.IsNullOrWhiteSpace(bookingResponse?.response_text) ? $"\r\n{bookingResponse.response_text}" : string.Empty));
}
return bikeInfoRequestedOccupied;
}
/// Gets if a booking call succeeded or not by checking a booking response.
/// Id of bike which should be booked.
/// Response to check.
///
public static BikeInfoReservedOrBooked GetIsBookingResponseOk(
this ReservationBookingResponse bookingResponse,
string bikeId)
{
GetIsResponseOk(bookingResponse, string.Format(AppResources.ExceptionTextRentingBikeFailedGeneral, bikeId));
// Get bike which has to be booked.
var bikeInfoRequestedOccupied = bookingResponse?.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
if (bikeInfoRequestedOccupied == null)
{
throw new System.Exception(string.Format(
AppResources.ExceptionTextRentingBikeFailedUnavailalbe,
bikeId,
!string.IsNullOrWhiteSpace(bookingResponse?.response_text) ? $"\r\n{bookingResponse.response_text}" : string.Empty));
}
return bikeInfoRequestedOccupied;
}
/// Gets if request is ok.
/// Response to verify.
/// Text describing request which is shown if validation fails.
/// Verified response.
public static BikesReservedOccupiedResponse GetIsResponseOk(this BikesReservedOccupiedResponse response, string textOfAction)
{
if (response == null || response.response_state == null)
{
throw new InvalidResponseException(textOfAction, null);
}
if (AuthcookieNotDefinedException.IsAuthcookieNotDefined(response, textOfAction, out AuthcookieNotDefinedException exception))
{
throw exception;
}
if (response.response_state.Trim().ToUpper().StartsWith(RESPONSE_AUTHCOOKIE_EXPRIED.ToUpper()))
{
throw new AuthcookieNotDefinedException(
$"{textOfAction}\r\n{AppResources.ExceptionTextSessionExpired}",
response);
}
GetIsResponseOk((ResponseBase)response, textOfAction);
return response;
}
/// Gets if request is ok.
/// Response to verify.
/// Text describing request which is shown if validation fails.
/// Verified response.
public static T GetIsResponseOk(this T response, string textOfAction) where T : ResponseBase
{
if (response == null || response.response_state == null)
{
throw new InvalidResponseException(textOfAction, null);
}
if (AuthcookieNotDefinedException.IsAuthcookieNotDefined(response, textOfAction, out AuthcookieNotDefinedException exception))
{
throw exception;
}
if (!response.response_state.Trim().ToUpper().StartsWith(RESPONSE_OK))
{
throw new InvalidResponseException(
$"{textOfAction}\r\nServer Antwort: {response.response_text}",
response);
}
return response;
}
/// Gets if cancel or booking request is ok.
/// Response to verify.
/// Text describing request which is shown if validation fails.
/// Id of bike.
/// Verified response.
public static ReservationCancelReturnResponse GetIsCancelReservationResponseOk(
this ReservationCancelReturnResponse cancelBookingResponse,
string bikeId)
{
GetIsResponseOk(cancelBookingResponse, BIKES_CANCELREQUEST_ACTIONTEXT);
// Get bike which has to be booked.
var l_oBikeInfo = cancelBookingResponse?.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
if (l_oBikeInfo != null)
{
throw new ReturnBikeException(
cancelBookingResponse,
$"{BIKES_CANCELREQUEST_ACTIONTEXT} Aufruf wurde erfolgreich ausgeführt, aber Rad ist noch in Liste der reservierten Räder enthalten.");
}
return cancelBookingResponse;
}
/// Gets if return bike request is ok.
/// Response to verify.
/// Text describing request which is shown if validation fails.
/// Id of bike.
/// Verified response.
public static DoReturnResponse GetIsReturnBikeResponseOk(
this DoReturnResponse returnBikeResponse,
string bikeId)
{
// Check if bike is at station.
if (NotAtStationException.IsNotAtStation(returnBikeResponse.response_state.ToUpper(), out NotAtStationException notAtStationException))
{
throw notAtStationException;
}
// Check if GPS data was send to copri.
if (NoGPSDataException.IsNoGPSData(returnBikeResponse.response_state.ToUpper(), out NoGPSDataException noGPSDataException))
{
throw noGPSDataException;
}
GetIsResponseOk(returnBikeResponse, BIKES_RETURNBIKE_ACTIONTEXT);
// Get bike which has to be booked.
var l_oBikeInfo = returnBikeResponse?.bikes_occupied?.Values?.FirstOrDefault(x => x.bike == bikeId);
if (l_oBikeInfo != null)
{
throw new ReturnBikeException(
returnBikeResponse,
$"{BIKES_RETURNBIKE_ACTIONTEXT} Aufruf wurde erfolgreich ausgeführt, aber Rad ist noch in Liste der gemieteten Räder enthalten.");
}
return returnBikeResponse;
}
///
/// Gets the response for bikes occupied request with no bikes reserved.
///
///
///
public static BikesReservedOccupiedResponse GetBikesOccupiedNone(string p_strSesstionCookie = null)
{
var l_oJson = CopriCallsMonkeyStore.BIKESOCCUPIED.Replace(@"""authcookie"": """"", @"""authcookie"": """ + (p_strSesstionCookie ?? string.Empty) + @"""");
return CopriCallsStatic.DeserializeResponse(@"{ ""shareejson"" : " + l_oJson + "}", (version) => new BikesReservedOccupiedResponse());
}
}
}