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

883 lines
35 KiB
C#
Raw Normal View History

2021-05-13 20:03:07 +02:00

using Serilog;
using System;
using System.IO;
using System.Net;
using System.Text;
using System.Threading.Tasks;
2021-06-26 20:57:55 +02:00
using TINK.Repository.Exception;
using TINK.Repository.Request;
2021-05-13 20:03:07 +02:00
using TINK.Repository.Response;
2021-06-26 20:57:55 +02:00
using TINK.Model.Logging;
using TINK.Model.Device;
2021-08-01 17:24:15 +02:00
using System.Collections.Generic;
2021-05-13 20:03:07 +02:00
2021-06-26 20:57:55 +02:00
namespace TINK.Repository
2021-05-13 20:03:07 +02:00
{
/// <summary> Object which manages calls to copri. </summary>
public class CopriCallsHttps : ICopriServer
{
/// <summary> Builds requests.</summary>
private IRequestBuilder requestBuilder;
/// <summary> Initializes a instance of the copri calls https object. </summary>
2021-06-26 20:57:55 +02:00
/// <param name="copriHost">Host to connect to. </param>
2022-01-04 18:54:03 +01:00
/// <param name="appContextInfo">Provides app related info (app name and version, merchantid) to pass to COPRI.</param>
2021-05-13 20:03:07 +02:00
/// <param name="sessionCookie">Session cookie if user is logged in, null otherwise.</param>
public CopriCallsHttps(
2021-06-26 20:57:55 +02:00
Uri copriHost,
2022-01-04 18:54:03 +01:00
AppContextInfo appContextInfo,
2021-05-13 20:03:07 +02:00
string sessionCookie = null)
{
2021-06-26 20:57:55 +02:00
m_oCopriHost = copriHost
2022-01-04 18:54:03 +01:00
?? throw new System.Exception($"Can not construct {GetType()}- object. Uri of copri host must not be null.");
2021-05-13 20:03:07 +02:00
2022-01-04 18:54:03 +01:00
UserAgent = appContextInfo != null
? appContextInfo.UserAgent
: throw new System.Exception($"Can not construct {GetType()}- object. User agent must not be null or empty.");
2021-05-13 20:03:07 +02:00
requestBuilder = string.IsNullOrEmpty(sessionCookie)
2022-01-04 18:54:03 +01:00
? new RequestBuilder(appContextInfo.MerchantId) as IRequestBuilder
: new RequestBuilderLoggedIn(appContextInfo.MerchantId, sessionCookie);
2021-05-13 20:03:07 +02:00
}
/// <summary> Holds the URL for rest calls.</summary>
private Uri m_oCopriHost;
/// <summary> Spacifies name and version of app. </summary>
private string UserAgent { get; }
/// <summary> Returns true because value requested form copri server are returned. </summary>
public bool IsConnected => true;
/// <summary> Gets the merchant id.</summary>
public string MerchantId => requestBuilder.MerchantId;
/// <summary> Gets the session cookie if user is logged in, an empty string otherwise. </summary>
public string SessionCookie => requestBuilder.SessionCookie;
/// <summary> Logs user in. </summary>
/// <param name="mailAddress">Mailaddress of user to log in.</param>
/// <param name="password">Password to log in.</param>
/// <param name="deviceId">Id specifying user and hardware.</param>
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
public async Task<AuthorizationResponse> DoAuthorizationAsync(
string mailAddress,
string password,
string deviceId)
{
return await DoAuthorizationAsync(
m_oCopriHost.AbsoluteUri,
requestBuilder.DoAuthorization(mailAddress, password, deviceId),
() => requestBuilder.DoAuthorization(mailAddress, "********", deviceId),
UserAgent);
}
/// <summary> Logs user out. </summary>
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
public async Task<AuthorizationoutResponse> DoAuthoutAsync()
{
return await DoAuthoutAsync(m_oCopriHost.AbsoluteUri, requestBuilder.DoAuthout(), UserAgent);
}
/// <summary>Gets bikes available.</summary>
/// <returns>Response holding list of bikes.</returns>
public async Task<BikesAvailableResponse> GetBikesAvailableAsync()
{
return await GetBikesAvailableAsync(m_oCopriHost.AbsoluteUri, requestBuilder.GetBikesAvailable(), UserAgent);
}
/// <summary> Gets a list of bikes reserved/ booked by acctive user. </summary>
/// <returns>Response holding list of bikes.</returns>
public async Task<BikesReservedOccupiedResponse> GetBikesOccupiedAsync()
{
try
{
return await GetBikesOccupiedAsync(m_oCopriHost.AbsoluteUri, requestBuilder.GetBikesOccupied(), UserAgent);
}
catch (NotSupportedException)
{
// No user logged in.
await Task.CompletedTask;
return ResponseHelper.GetBikesOccupiedNone();
}
}
/// <summary> Get list of stations. </summary>
/// <returns>List of files.</returns>
public async Task<StationsAvailableResponse> GetStationsAsync()
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
var stations = await GetStationsAsync(m_oCopriHost.AbsoluteUri, requestBuilder.GetStations(), UserAgent);
return stations;
2021-05-13 20:03:07 +02:00
}
/// <summary> Get authentication keys. </summary>
/// <param name="bikeId">Id of the bike to get keys for.</param>
/// <returns>Response holding authentication keys.</returns>
2021-06-26 20:57:55 +02:00
public async Task<ReservationBookingResponse> GetAuthKeys(string bikeId)
=> await GetAuthKeysAsync(m_oCopriHost.AbsoluteUri, requestBuilder.CalculateAuthParameters(bikeId), UserAgent);
2021-05-13 20:03:07 +02:00
/// <summary> Gets booking request response.</summary>
/// <param name="bikeId">Id of the bike to book.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Booking response.</returns>
public async Task<ReservationBookingResponse> DoReserveAsync(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
Uri operatorUri)
{
return await DoReserveAsync(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
requestBuilder.DoReserve(bikeId),
UserAgent);
}
/// <summary> Gets canel booking request response.</summary>
/// <param name="bikeId">Id of the bike to book.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Response on cancel booking request.</returns>
public async Task<ReservationCancelReturnResponse> DoCancelReservationAsync(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
Uri operatorUri)
{
return await DoCancelReservationAsync(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
requestBuilder.DoCancelReservation(bikeId),
UserAgent);
}
/// <summary> Get authentication keys. </summary>
/// <param name="bikeId">Id of the bike to get keys for.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Response holding authentication keys.</returns>
public async Task<ReservationBookingResponse> CalculateAuthKeysAsync(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
Uri operatorUri)
=> await GetAuthKeysAsync(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
2021-06-26 20:57:55 +02:00
requestBuilder.CalculateAuthParameters(bikeId),
2021-05-13 20:03:07 +02:00
UserAgent);
2022-04-10 17:38:34 +02:00
/// <summary> Notifies COPRI about start of returning sequence. </summary>
/// <remarks> Operator specific call.</remarks>
/// <param name="bikeId">Id of the bike to return.</param>+
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Response on notification about start of returning sequence.</returns>
public async Task<ResponseBase> StartReturningBike(
string bikeId,
Uri operatorUri)
=> await DoStartReturningBike(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
requestBuilder.StartReturningBike(bikeId),
UserAgent);
2021-05-13 20:03:07 +02:00
/// <summary> Updates lock state for a booked bike. </summary>
/// <param name="bikeId">Id of the bike to update locking state for.</param>
/// <param name="location">Geolocation of lock.</param>
/// <param name="state">New locking state.</param>
/// <param name="batteryPercentage">Holds the filling level percentage of the battery.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Response on updating locking state.</returns>
public async Task<ReservationBookingResponse> UpdateLockingStateAsync(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
LocationDto location,
lock_state state,
double batteryLevel,
2022-04-10 17:38:34 +02:00
Uri operatorUri)=>
await DoUpdateLockingStateAsync(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
requestBuilder.UpateLockingState(bikeId, location, state, batteryLevel),
UserAgent);
2021-05-13 20:03:07 +02:00
/// <summary> Gets booking request request. </summary>
/// <param name="bikeId">Id of the bike to book.</param>
/// <param name="guid">Used to publish GUID from app to copri. Used for initial setup of bike in copri.</param>
/// <param name="batteryPercentage">Holds the filling level percentage of the battery.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Requst on booking request.</returns>
public async Task<ReservationBookingResponse> DoBookAsync(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
Guid guid,
double batteryPercentage,
Uri operatorUri)
{
return await DoBookAsync(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
requestBuilder.DoBook(bikeId, guid, batteryPercentage),
UserAgent);
}
/// <summary> Returns a bike. </summary>
/// <param name="bikeId">Id of the bike to return.</param>
/// <param name="location">Geolocation of lock.</param>
2021-06-26 20:57:55 +02:00
/// <param name="smartDevice">Provides info about hard and software.</param>
2021-05-13 20:03:07 +02:00
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Response on returning request.</returns>
2021-12-08 17:57:30 +01:00
public async Task<DoReturnResponse> DoReturn(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
LocationDto location,
2021-06-26 20:57:55 +02:00
ISmartDevice smartDevice,
2021-05-13 20:03:07 +02:00
Uri operatorUri)
{
return await DoReturn(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
2021-06-26 20:57:55 +02:00
requestBuilder.DoReturn(bikeId, location, smartDevice),
2021-05-13 20:03:07 +02:00
UserAgent);
}
/// <summary> Submits feedback to copri server. </summary>
2021-06-26 20:57:55 +02:00
/// <param name="bikeId">Id of the bike to which the feedback is related to.</param>
2021-05-13 20:03:07 +02:00
/// <param name="isBikeBroken">True if bike is broken.</param>
/// <param name="message">General purpose message or error description.</param>
/// <param name="operatorUri">Holds the uri of the operator or null, in case of single operator setup.</param>
/// <returns>Response on submitting feedback request.</returns>
public async Task<SubmitFeedbackResponse> DoSubmitFeedback(
2021-06-26 20:57:55 +02:00
string bikeId,
2021-05-13 20:03:07 +02:00
string message,
bool isBikeBroken,
Uri operatorUri) =>
await DoSubmitFeedback(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
2021-06-26 20:57:55 +02:00
requestBuilder.DoSubmitFeedback(bikeId, message, isBikeBroken),
2021-05-13 20:03:07 +02:00
UserAgent);
2021-08-01 17:24:15 +02:00
/// <summary> Submits mini survey to copri server. </summary>
/// <param name="answers">Collection of answers.</param>
public Task<ResponseBase> DoSubmitMiniSurvey(IDictionary<string, string> answers)
=> DoSubmitMiniSurvey(
m_oCopriHost.AbsoluteUri,
requestBuilder.DoSubmitMiniSurvey(answers),
UserAgent);
2021-05-13 20:03:07 +02:00
/// <summary> Logs user in. </summary>
/// <param name="copriHost">Host to connect to. </param>
/// <param name="command">Command to log user in.</param>
/// <remarks>Response which holds auth cookie <see cref="ResponseBase.authcookie"/></remarks>
public static async Task<AuthorizationResponse> DoAuthorizationAsync(
string copriHost,
string command,
Func<string> displayCommand,
string userAgent = null)
{
#if !WINDOWS_UWP
/// Extract session cookie from response.
2021-06-26 20:57:55 +02:00
string response = string.Empty;
2021-05-13 20:03:07 +02:00
try
{
2021-06-26 20:57:55 +02:00
response = await PostAsync(
2021-05-13 20:03:07 +02:00
copriHost,
command,
userAgent,
displayCommand); // Do not include password into exception output when an error occurres.
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Login fehlgeschlagen aufgrund eines Netzwerkfehlers.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Login fehlgeschlagen aufgrund eines Netzwerkfehlers.", l_oException);
}
throw;
}
2021-06-26 20:57:55 +02:00
return CopriCallsStatic.DeserializeResponse<AuthorizationResponse>(response, (version) => new UnsupportedCopriVersionDetectedException());
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
/// <summary> Logs user out. </summary>
/// <param name="copriHost">Host to connect to. </param>
/// <param name="command">Command to log user out.</param>
public static async Task<AuthorizationoutResponse> DoAuthoutAsync(
string p_strCopriHost,
string p_oCommand,
string userAgent = null)
{
#if !WINDOWS_UWP
string l_oLogoutResponse;
try
{
l_oLogoutResponse = await PostAsync(p_strCopriHost, p_oCommand, userAgent);
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Login fehlgeschlagen wegen Netzwerkfehler.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Login fehlgeschlagen wegen Netzwerkfehler.", l_oException);
}
throw;
}
/// Extract session cookie from response.
2021-06-26 20:57:55 +02:00
return CopriCallsStatic.DeserializeResponse<AuthorizationoutResponse>(l_oLogoutResponse, (version) => new UnsupportedCopriVersionDetectedException());
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
/// <summary>
/// Get list of stations from file.
/// </summary>
/// <param name="p_strCopriHost">URL of the copri host to connect to.</param>
/// <param name="p_oCommand">Command to get stations.</param>
/// <returns>List of files.</returns>
public static async Task<StationsAvailableResponse> GetStationsAsync(
2021-05-13 20:03:07 +02:00
string p_strCopriHost,
string p_oCommand,
string userAgent = null)
{
#if !WINDOWS_UWP
2021-06-26 20:57:55 +02:00
string response;
2021-05-13 20:03:07 +02:00
try
{
2021-06-26 20:57:55 +02:00
response = await PostAsync(p_strCopriHost, p_oCommand, userAgent);
2021-05-13 20:03:07 +02:00
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Abfage der verfügbaren Räder fehlgeschlagen wegen Netzwerkfehler.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Abfage der verfügbaren Räder fehlgeschlagen wegen Netzwerkfehler.", l_oException);
}
throw;
}
// Extract bikes from response.
2021-06-26 20:57:55 +02:00
return CopriCallsStatic.DeserializeResponse(
response,
(version) => CopriCallsMonkeyStore.GetEmptyStationsAllResponse(version));
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
/// <summary> Gets a list of bikes from Copri. </summary>
/// <param name="p_strCopriHost">URL of the copri host to connect to.</param>
/// <param name="p_oCommand">Command to get bikes.</param>
/// <returns>Response holding list of bikes.</returns>
public static async Task<BikesAvailableResponse> GetBikesAvailableAsync(
string p_strCopriHost,
string l_oCommand,
string userAgent = null)
{
#if !WINDOWS_UWP
2021-06-26 20:57:55 +02:00
string response;
2021-05-13 20:03:07 +02:00
try
{
2021-06-26 20:57:55 +02:00
response = await PostAsync(p_strCopriHost, l_oCommand, userAgent);
2021-05-13 20:03:07 +02:00
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Abfage der verfügbaren Räder fehlgeschlagen wegen Netzwerkfehler.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Abfage der verfügbaren Räder fehlgeschlagen wegen Netzwerkfehler.", l_oException);
}
throw;
}
// Extract bikes from response.
2021-06-26 20:57:55 +02:00
return CopriCallsStatic.DeserializeResponse(
response,
(version) => CopriCallsMonkeyStore.GetEmptyBikesAvailableResponse(version));
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
/// <summary> Gets a list of bikes reserved/ booked by acctive user from Copri.</summary>
/// <param name="p_strCopriHost">URL of the copri host to connect to.</param>
/// <param name="p_oCommand">Command to post.</param>
/// <returns>Response holding list of bikes.</returns>
public static async Task<BikesReservedOccupiedResponse> GetBikesOccupiedAsync(
string p_strCopriHost,
string p_oCommand,
string userAgent = null)
{
#if !WINDOWS_UWP
2021-06-26 20:57:55 +02:00
string response;
2021-05-13 20:03:07 +02:00
try
{
2021-06-26 20:57:55 +02:00
response = await PostAsync(p_strCopriHost, p_oCommand, userAgent);
2021-05-13 20:03:07 +02:00
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Abfage der reservierten/ gebuchten Räder fehlgeschlagen wegen Netzwerkfehler.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Abfage der reservierten/ gebuchten Räder fehlgeschlagen wegen Netzwerkfehler.", l_oException);
}
throw;
}
// Extract bikes from response.
2021-06-26 20:57:55 +02:00
return CopriCallsStatic.DeserializeResponse(
response,
(version) => CopriCallsMonkeyStore.GetEmptyBikesReservedOccupiedResponse(version));
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
/// <summary> Get auth keys from COPRI. </summary>
/// <param name="copriHost">Host to connect to. </param>
/// <param name="command">Command to log user in.</param>
/// <returns>Response on booking request.</returns>
public static async Task<ReservationBookingResponse> GetAuthKeysAsync(
string p_strCopriHost,
string p_oCommand,
string userAgent = null)
{
#if !WINDOWS_UWP
string l_oBikesAvaialbeResponse;
try
{
l_oBikesAvaialbeResponse = await PostAsync(p_strCopriHost, p_oCommand, userAgent);
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Schlosssuche wegen Netzwerkfehler fehlgeschlagen.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Schlosssuche wegen Netzwerkfehler fehlgeschlagen.", l_oException);
}
throw;
}
// Extract bikes from response.
2021-06-26 20:57:55 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<ReservationBookingResponse>>(l_oBikesAvaialbeResponse)?.shareejson;
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
/// <summary> Gets booking request response. </summary>
/// <param name="copriHost">Host to connect to. </param>
/// <param name="command">Command to log user in.</param>
/// <returns>Response on booking request.</returns>
public static async Task<ReservationBookingResponse> DoReserveAsync(
string p_strCopriHost,
string p_oCommand,
string userAgent = null)
{
#if !WINDOWS_UWP
string l_oBikesAvaialbeResponse;
try
{
l_oBikesAvaialbeResponse = await PostAsync(p_strCopriHost, p_oCommand, userAgent);
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Reservierung des Fahrrads wegen Netzwerkfehler fehlgeschlagen.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Reservierung des Fahrrads wegen Netzwerkfehler fehlgeschlagen.", l_oException);
}
throw;
}
// Extract bikes from response.
2021-06-26 20:57:55 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<ReservationBookingResponse>>(l_oBikesAvaialbeResponse)?.shareejson;
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
/// <summary> Gets canel booking request response.</summary>
/// <param name="copriHost">Host to connect to. </param>
/// <param name="command">Command to log user in.</param>
/// <returns>Response on cancel booking request.</returns>
public static async Task<ReservationCancelReturnResponse> DoCancelReservationAsync(
string copriHost,
string command,
string userAgent = null)
{
#if !WINDOWS_UWP
string l_oBikesAvaialbeResponse;
try
{
l_oBikesAvaialbeResponse = await PostAsync(copriHost, command, userAgent);
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Reservierung des Fahrrads aufgrund eines Netzwerkfehlers fehlgeschlagen.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Reservierung des Fahrrads aufgrund eines Netzwerkfehlers fehlgeschlagen.", l_oException);
}
throw;
}
// Extract bikes from response.
2021-06-26 20:57:55 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<ReservationCancelReturnResponse>>(l_oBikesAvaialbeResponse)?.shareejson;
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
2022-04-10 17:38:34 +02:00
public static async Task<ResponseBase> DoStartReturningBike(
string copriHost,
string command,
string agent = null)
{
#if !WINDOWS_UWP
string response;
try
{
response = await PostAsync(copriHost, command, agent);
}
catch (System.Exception exception)
{
if (exception.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Benachrichtigung von Start der Rückgabe wegen Netzwerkfehler fehlgeschlagen.", exception);
}
if (exception.GetIsForbiddenException())
{
throw new WebForbiddenException("Benachrichtigung von Start der Rückgabe wegen Netzwerkfehler fehlgeschlagen.", exception);
}
throw;
}
// Extract bikes from response.
return JsonConvertRethrow.DeserializeObject<ResponseContainer<ResponseBase>>(response)?.shareejson;
#else
return null;
#endif
}
2021-05-13 20:03:07 +02:00
public static async Task<ReservationBookingResponse> DoUpdateLockingStateAsync(
string copriHost,
string command,
string agent = null)
{
#if !WINDOWS_UWP
2022-04-10 17:38:34 +02:00
string bikesAvaialbeResponse;
2021-05-13 20:03:07 +02:00
try
{
2022-04-10 17:38:34 +02:00
bikesAvaialbeResponse = await PostAsync(copriHost, command, agent);
2021-05-13 20:03:07 +02:00
}
2022-04-10 17:38:34 +02:00
catch (System.Exception exception)
2021-05-13 20:03:07 +02:00
{
2022-04-10 17:38:34 +02:00
if (exception.GetIsConnectFailureException())
2021-05-13 20:03:07 +02:00
{
2022-04-10 17:38:34 +02:00
throw new WebConnectFailureException("Aktualisierung des Schlossstatuses wegen Netzwerkfehler fehlgeschlagen.", exception);
2021-05-13 20:03:07 +02:00
}
2022-04-10 17:38:34 +02:00
if (exception.GetIsForbiddenException())
2021-05-13 20:03:07 +02:00
{
2022-04-10 17:38:34 +02:00
throw new WebForbiddenException("Aktualisierung des Schlossstatuses wegen Netzwerkfehler fehlgeschlagen.", exception);
2021-05-13 20:03:07 +02:00
}
throw;
}
// Extract bikes from response.
2022-04-10 17:38:34 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<ReservationBookingResponse>>(bikesAvaialbeResponse)?.shareejson;
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
public static async Task<ReservationBookingResponse> DoBookAsync(
string copriHost,
string command,
string agent = null)
{
#if !WINDOWS_UWP
string l_oBikesAvaialbeResponse;
try
{
l_oBikesAvaialbeResponse = await PostAsync(copriHost, command, agent);
}
2022-04-10 17:38:34 +02:00
catch (System.Exception exception)
2021-05-13 20:03:07 +02:00
{
2022-04-10 17:38:34 +02:00
if (exception.GetIsConnectFailureException())
2021-05-13 20:03:07 +02:00
{
2022-04-10 17:38:34 +02:00
throw new WebConnectFailureException("Buchung des Fahrrads wegen Netzwerkfehler fehlgeschlagen.", exception);
2021-05-13 20:03:07 +02:00
}
2022-04-10 17:38:34 +02:00
if (exception.GetIsForbiddenException())
2021-05-13 20:03:07 +02:00
{
2022-04-10 17:38:34 +02:00
throw new WebForbiddenException("Buchung des Fahrrads wegen Netzwerkfehler fehlgeschlagen.", exception);
2021-05-13 20:03:07 +02:00
}
throw;
}
// Extract bikes from response.
2021-06-26 20:57:55 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<ReservationBookingResponse>>(l_oBikesAvaialbeResponse)?.shareejson;
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
2021-12-08 17:57:30 +01:00
public static async Task<DoReturnResponse> DoReturn(
2021-05-13 20:03:07 +02:00
string copriHost,
string command,
string userAgent = null)
{
#if !WINDOWS_UWP
2022-01-04 18:54:03 +01:00
string doReturnResponse;
2021-05-13 20:03:07 +02:00
try
{
2022-01-04 18:54:03 +01:00
doReturnResponse = await PostAsync(copriHost, command, userAgent);
2021-05-13 20:03:07 +02:00
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Rückgabe des Fahrrads aufgrund eines Netzwerkfehlers fehlgeschlagen.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Rückgabe des Fahrrads aufgrund eines Netzwerkfehlers fehlgeschlagen.", l_oException);
}
throw;
}
// Extract bikes from response.
2022-01-04 18:54:03 +01:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<DoReturnResponse>>(doReturnResponse)?.shareejson;
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
public async Task<SubmitFeedbackResponse> DoSubmitFeedback(
string copriHost,
string command,
string userAgent = null)
{
#if !WINDOWS_UWP
string userFeedbackResponse;
try
{
userFeedbackResponse = await PostAsync(copriHost, command, userAgent);
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Senden der Rückmeldung aufgrund eines Netzwerkfehlers fehlgeschlagen.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Senden der Rückmeldung aufgrund eines Netzwerkfehlers fehlgeschlagen.", l_oException);
}
throw;
}
// Extract bikes from response.
2021-06-26 20:57:55 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<SubmitFeedbackResponse>>(userFeedbackResponse)?.shareejson;
2021-05-13 20:03:07 +02:00
#else
return null;
#endif
}
2021-08-01 17:24:15 +02:00
/// <summary> Submits mini survey to copri server. </summary>
public async Task<ResponseBase> DoSubmitMiniSurvey(
string copriHost,
string command,
string userAgent = null)
{
#if !WINDOWS_UWP
string miniSurveyResponse;
try
{
miniSurveyResponse = await PostAsync(copriHost, command, userAgent);
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Senden der Miniumfrage aufgrund eines Netzwerkfehlers fehlgeschlagen.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Senden der der Miniumfrage aufgrund eines Netzwerkfehlers fehlgeschlagen.", l_oException);
}
throw;
}
// Extract bikes from response.
return JsonConvertRethrow.DeserializeObject<ResponseContainer<ResponseBase>>(miniSurveyResponse)?.shareejson;
#else
return null;
#endif
}
2021-05-13 20:03:07 +02:00
/// <summary> http get- request.</summary>
/// <param name="Url">Ulr to get info from.</param>
/// <returns>response from server</returns>
public static async Task<string> Get(string Url)
{
string result = string.Empty;
HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(Url);
myRequest.Method = "GET";
using (var myResponse = await myRequest.GetResponseAsync())
{
using (var sr = new StreamReader(myResponse.GetResponseStream(), Encoding.UTF8))
{
result = sr.ReadToEnd();
}
}
return result;
}
/// <summary> http- post request.</summary>
2022-01-22 18:28:01 +01:00
/// <param name="command">Command to send.</param>
/// <param name="displayCommand">Command to display/ log used for error handling.</param>
2021-05-13 20:03:07 +02:00
/// <param name="uRL">Address of server to communicate with.</param>
/// <returns>Response as text.</returns>
/// <changelog> An unused member PostAsyncHttpClient using HttpClient for posting was removed 2020-04-02.</changelog>
private static async Task<string> PostAsync(
string uRL,
2022-01-22 18:28:01 +01:00
string command,
2021-05-13 20:03:07 +02:00
string userAgent = null,
2022-01-22 18:28:01 +01:00
Func<string> displayCommand = null)
2021-05-13 20:03:07 +02:00
{
2022-01-22 18:28:01 +01:00
if (string.IsNullOrEmpty(command))
2021-05-13 20:03:07 +02:00
{
Log.ForContext<CopriCallsHttps>().Fatal("Can not post command. Command must not be null or empty.");
throw new ArgumentException("Can not post command. Command must not be null or empty.");
}
if (string.IsNullOrEmpty(uRL))
{
Log.ForContext<CopriCallsHttps>().Fatal("Can not post command. Host must not be null or empty.");
throw new ArgumentException("Can not post command. Host must not be null or empty.");
}
// Get display version of command to used for display/ logging (password should never be included in output)
2022-01-22 18:28:01 +01:00
Func<string> displayCommandFunc = displayCommand ?? delegate () { return command; };
2021-05-13 20:03:07 +02:00
try
{
#if !WINDOWS_UWP
var l_strHost = uRL;
// Returns a http request.
2022-01-04 18:59:16 +01:00
var request = WebRequest.CreateHttp(l_strHost);
2021-05-13 20:03:07 +02:00
2022-01-04 18:59:16 +01:00
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.UserAgent = userAgent;
2021-05-13 20:03:07 +02:00
// Workaround for issue https://bugzilla.xamarin.com/show_bug.cgi?id=57705
// If not KeepAlive is set to true Stream.Write leads arbitrarily to an object disposed exception.
2022-01-04 18:59:16 +01:00
request.KeepAlive = true;
2021-05-13 20:03:07 +02:00
2022-01-22 18:28:01 +01:00
byte[] postData = Encoding.UTF8.GetBytes(command);
2021-05-13 20:03:07 +02:00
2022-01-22 18:28:01 +01:00
request.ContentLength = postData.Length;
2021-05-13 20:03:07 +02:00
// Get the request stream.
2022-01-22 18:28:01 +01:00
using (Stream dataStream = await request.GetRequestStreamAsync())
2021-05-13 20:03:07 +02:00
{
// Write the data to the request stream.
2022-01-22 18:28:01 +01:00
await dataStream.WriteAsync(postData, 0, postData.Length);
2021-05-13 20:03:07 +02:00
}
// Get the response.
2022-01-22 18:28:01 +01:00
var webResponse = await request.GetResponseAsync() as HttpWebResponse;
2021-05-13 20:03:07 +02:00
2022-01-22 18:28:01 +01:00
if (webResponse == null)
2021-05-13 20:03:07 +02:00
{
throw new System.Exception(string.Format("Reserve request failed. Response form from server was not of expected type."));
}
2022-01-22 18:28:01 +01:00
if (webResponse.StatusCode != HttpStatusCode.OK)
2021-05-13 20:03:07 +02:00
{
throw new CommunicationException(string.Format(
"Posting request {0} failed. Expected status code is {1} but was {2}.",
displayCommandFunc(),
HttpStatusCode.OK,
2022-01-22 18:28:01 +01:00
webResponse.StatusCode));
2021-05-13 20:03:07 +02:00
}
string response = string.Empty;
// Get the request stream.
2022-01-22 18:28:01 +01:00
using (Stream l_oDataStream = webResponse.GetResponseStream())
2021-05-13 20:03:07 +02:00
using (StreamReader l_oReader = new StreamReader(l_oDataStream))
{
// Read the content.
response = l_oReader.ReadToEnd();
// Display the content.
Console.WriteLine(response);
// Clean up the streams.
2022-01-22 18:28:01 +01:00
webResponse.Close();
2021-05-13 20:03:07 +02:00
}
Log.ForContext<CopriCallsHttps>().Verbose("Post command {DisplayCommand} to host {URL} received {ResponseText:j}.", displayCommandFunc(), uRL, response);
return response;
#else
return null;
#endif
}
2021-06-26 20:57:55 +02:00
catch (System.Exception exception)
2021-05-13 20:03:07 +02:00
{
2021-06-26 20:57:55 +02:00
Log.ForContext<CopriCallsHttps>().InformationOrError("Posting command {DisplayCommand} to host {URL} failed. {Exception}.", displayCommandFunc(), uRL, exception);
2021-05-13 20:03:07 +02:00
throw;
}
}
}
}