using System; using System.Collections.Generic; using System.Threading.Tasks; using TINK.Model; using TINK.Model.Device; using TINK.Repository; using TINK.Repository.Request; using TINK.Repository.Response; namespace TestShareeLib.Repository { /// Provides functionality for keeping a set of COPRI responses. public abstract class CopriCallMemoryBase { private string BikesAvailableResponse { get; } private string BikesOccupiedResponse { get; } private string AuthResponse { get; } private string AuthOutResponse { get; } private string Stations { get; } private string BookingRequestResponse { get; } private string CancelBookingRequestResponse { get; } private IRequestBuilder requestBuilder; public CopriCallMemoryBase( string bikesAvailableResponse = null, string bikesOccupiedResponse = null, string authResponse = null, string authOutResponse = null, string stations = null, string bookingRequestResponse = null, string cancelBookingRequestResponse = null, string sessionCookie = null) { SessionCookie = sessionCookie; BikesAvailableResponse = bikesAvailableResponse; BikesOccupiedResponse = bikesOccupiedResponse; AuthResponse = authResponse; AuthOutResponse = authOutResponse; BookingRequestResponse = bookingRequestResponse; CancelBookingRequestResponse = cancelBookingRequestResponse; Stations = stations; requestBuilder = string.IsNullOrEmpty(sessionCookie) ? new RequestBuilder(MerchantId) as IRequestBuilder : new RequestBuilderLoggedIn(MerchantId, sessionCookie); } /// Holds the session id of the logged in user, null otherwise. public string SessionCookie { get; private set; } /// Logs user in. /// User to log in. /// Id specifying user and hardware. /// Mailaddress of user to log in. /// Password to log in. /// Response which holds auth cookie public async Task DoAuthorizationAsync( string mailAddress, string password, string deviceId) => await Task.Run(() => DoAuthorize(AuthResponse, mailAddress, password, deviceId)); /// Logs user out. /// User to log in. /// Response which holds auth cookie public async Task DoAuthoutAsync() => await Task.Run(() => DoAuthout(AuthOutResponse, SessionCookie)); /// /// Gets list of bikes from memory. /// /// public async Task GetBikesAvailableAsync() => await Task.Run(() => GetBikesAvailable(BikesAvailableResponse, null, SessionCookie)); /// /// Gets a list of bikes reserved/ booked by acctive user from Copri. /// /// Cookie to authenticate user. /// Response holding list of bikes. public async Task GetBikesOccupiedAsync() { try { requestBuilder.GetBikesOccupied(); // Non mock implementation if ICopriServer call this member as well. To ensure comparable behaviour this member is called here as well. } catch (NotSupportedException) { // No user logged in. await Task.CompletedTask; return ResponseHelper.GetBikesOccupiedNone(); } return await Task.Run(() => GetBikesOccupied(BikesOccupiedResponse, SessionCookie)); } /// /// Get list of stations from file. /// /// Auto cookie of user if user is logged in. /// List of files. public async Task GetStationsAsync() => await Task.Run(() => GetStationsAll(Stations, null, SessionCookie)); /// /// Gets booking request response. /// /// Id of the bike to book. /// Booking response. public async Task DoReserveAsync(string bikeId, Uri operatorUri) => await Task.Run(() => DoReserve(BookingRequestResponse, bikeId, SessionCookie)); /// /// Gets canel booking request response. /// /// Id of the bike to book. /// Cookie of the logged in user. /// Response on cancel booking request. public async Task DoCancelReservationAsync(string bikeId, Uri operatorUri) => await Task.Run(() => DoCancelReservation(CancelBookingRequestResponse, bikeId, SessionCookie)); /// Gets the merchant id. public string MerchantId => TinkApp.MerchantId; /// Returns false because cached values are returned. public bool IsConnected => false; /// Logs user in. /// User to log in. /// Id specifying user and hardware. /// Mailaddress of user to log in. /// Password to log in. /// Response which holds auth cookie public static AuthorizationResponse DoAuthorize( string DoAuthResponse, string p_strMailAddress, string p_strPassword, string p_strDeviceId) { return p_strMailAddress == "javaminister@gmail.com" && p_strPassword == "*********" && p_strDeviceId == "HwId1000000000000" ? JsonConvertRethrow.DeserializeObject>(DoAuthResponse).shareejson : JsonConvertRethrow.DeserializeObject>(DO_AUTH_Unknown_User_FILE).shareejson; } /// Logs user in. /// Response which holds auth cookie public static AuthorizationoutResponse DoAuthout( string authOutResponse, string sessionCookie) { // Response contains auth cookie of user "JavaministerHardwareNr1" // For this reason do not return answer if mail and pwd do not match. return !string.IsNullOrEmpty(sessionCookie) ? JsonConvertRethrow.DeserializeObject>(authOutResponse).shareejson : throw new NotSupportedException(); } /// /// Gets list of bikes from memory. /// /// Id of the merchant. /// Auto cookie of user if user is logged in. /// Set of samples. /// Index of the stage. /// public static BikesAvailableResponse GetBikesAvailable( string BikesAvailableResponse, string p_strMerchantId, string p_strSessionCookie = null) => CopriCallsStatic.DeserializeResponse(BikesAvailableResponse); /// /// Gets stations response. /// /// Id of the merchant. /// Auto cookie of user if user is logged in. /// /// /// public static StationsAvailableResponse GetStationsAll( string stations, string merchantId, string cookie = null) => JsonConvertRethrow.DeserializeObject>(stations).shareejson; /// /// Gets booking request response. /// /// Id of the bike. /// Identifies the logged in user. /// Sample set to use. /// Index of the stage. /// public static ReservationBookingResponse DoReserve( string bookingRequestResponse, string bikeId, string sessionCookie) => JsonConvertRethrow.DeserializeObject>(bookingRequestResponse).shareejson; /// /// Gets canel booking request response. /// /// Id of the bike to book. /// Cookie of the logged in user. /// Response on cancel booking request. public static ReservationCancelReturnResponse DoCancelReservation( string cancelBookingRequestResponse, string bikeId, string cookie) => JsonConvertRethrow.DeserializeObject>(cancelBookingRequestResponse).shareejson; public Task CalculateAuthKeysAsync(string bikeId, Uri operatorUri) => null; public Task UpdateLockingStateAsync( string bikeId, LocationDto geolocation, lock_state state, double batteryLevel, Uri operatorUri) => null; public Task DoBookAsync(string bikeId, Guid guid, double batteryPercentage, Uri operatorUri) => null; public Task DoReturn( string bikeId, LocationDto geolocation, ISmartDevice smartDevice, Uri operatorUri) => null; public Task DoSubmitFeedback(string bikeId, string message, bool isBikeBroken, Uri operatorUri) => null; /// Submits mini survey to copri server. /// Collection of answers. public Task DoSubmitMiniSurvey(IDictionary answers) => null; /// /// Gets a list of bikes reserved/ booked by acctive user from Copri. /// /// Cookie to authenticate user. /// Sample set to use. /// Index of the stage. /// Response holding list of bikes. public static BikesReservedOccupiedResponse GetBikesOccupied( string bikesOccupied, string sessionCookie = null) { var response = CopriCallsStatic.DeserializeResponse(bikesOccupied); return sessionCookie != null && (response?.authcookie?.Contains(sessionCookie) ?? false) ? response : ResponseHelper.GetBikesOccupiedNone(sessionCookie); } public const string DO_AUTH_Unknown_User_FILE = @" { ""shareejson"" : { ""response"" : ""authorization"", ""authcookie"" : 0, ""response_state"" : ""Failure: cannot generate authcookie"", ""apiserver"" : ""https://tinkwwp.copri-bike.de"" } }"; } }