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

841 lines
34 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>
/// <param name="merchantId">Id of the merchant.</param>
2021-05-13 20:03:07 +02:00
/// <param name="userAgent">Holds the name and version of the TINKApp.</param>
/// <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,
string merchantId,
2021-05-13 20:03:07 +02:00
string userAgent,
string sessionCookie = null)
{
2021-06-26 20:57:55 +02:00
m_oCopriHost = copriHost
2021-05-13 20:03:07 +02:00
?? throw new System.Exception($"Can not construct {GetType().ToString()}- object. Uri of copri host must not be null.");
UserAgent = !string.IsNullOrEmpty(userAgent)
? userAgent
: throw new System.Exception($"Can not construct {GetType().ToString()}- object. User agent must not be null or empty.");
requestBuilder = string.IsNullOrEmpty(sessionCookie)
2021-06-26 20:57:55 +02:00
? new RequestBuilder(merchantId) as IRequestBuilder
: new RequestBuilderLoggedIn(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);
/// <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,
Uri operatorUri)
{
return await DoUpdateLockingStateAsync(
operatorUri?.AbsoluteUri ?? m_oCopriHost.AbsoluteUri,
requestBuilder.UpateLockingState(bikeId, location, state, batteryLevel),
UserAgent);
}
/// <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>
public async Task<ReservationCancelReturnResponse> 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
}
public static async Task<ReservationBookingResponse> DoUpdateLockingStateAsync(
string copriHost,
string command,
string agent = null)
{
#if !WINDOWS_UWP
string l_oBikesAvaialbeResponse;
try
{
l_oBikesAvaialbeResponse = await PostAsync(copriHost, command, agent);
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Aktualisierung des Schlossstatuses wegen Netzwerkfehler fehlgeschlagen.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Aktualisierung des Schlossstatuses 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
}
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);
}
catch (System.Exception l_oException)
{
if (l_oException.GetIsConnectFailureException())
{
throw new WebConnectFailureException("Buchung des Fahrrads wegen Netzwerkfehler fehlgeschlagen.", l_oException);
}
if (l_oException.GetIsForbiddenException())
{
throw new WebForbiddenException("Buchung 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
}
public static async Task<ReservationCancelReturnResponse> DoReturn(
string copriHost,
string command,
string userAgent = null)
{
#if !WINDOWS_UWP
2021-08-01 17:24:15 +02:00
string cancelOrReturnResponse;
2021-05-13 20:03:07 +02:00
try
{
2021-08-01 17:24:15 +02:00
cancelOrReturnResponse = 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.
2021-08-01 17:24:15 +02:00
return JsonConvertRethrow.DeserializeObject<ResponseContainer<ReservationCancelReturnResponse>>(cancelOrReturnResponse)?.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>
/// <param name="p_strCommand">Command to send.</param>
/// <param name="p_oDisplayCommand">Command to display/ log used for error handling.</param>
/// <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,
string p_strCommand,
string userAgent = null,
Func<string> p_oDisplayCommand = null)
{
if (string.IsNullOrEmpty(p_strCommand))
{
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)
Func<string> displayCommandFunc = p_oDisplayCommand ?? delegate () { return p_strCommand; };
try
{
#if !WINDOWS_UWP
var l_strHost = uRL;
// Returns a http request.
var l_oRequest = WebRequest.CreateHttp(l_strHost);
l_oRequest.Method = "POST";
l_oRequest.ContentType = "application/x-www-form-urlencoded";
l_oRequest.UserAgent = userAgent;
// 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.
l_oRequest.KeepAlive = true;
byte[] l_oPostData = Encoding.UTF8.GetBytes(p_strCommand);
l_oRequest.ContentLength = l_oPostData.Length;
// Get the request stream.
using (Stream l_oDataStream = await l_oRequest.GetRequestStreamAsync())
{
// Write the data to the request stream.
await l_oDataStream.WriteAsync(l_oPostData, 0, l_oPostData.Length);
}
// Get the response.
var l_oResponse = await l_oRequest.GetResponseAsync() as HttpWebResponse;
if (l_oResponse == null)
{
throw new System.Exception(string.Format("Reserve request failed. Response form from server was not of expected type."));
}
if (l_oResponse.StatusCode != HttpStatusCode.OK)
{
throw new CommunicationException(string.Format(
"Posting request {0} failed. Expected status code is {1} but was {2}.",
displayCommandFunc(),
HttpStatusCode.OK,
l_oResponse.StatusCode));
}
string response = string.Empty;
// Get the request stream.
using (Stream l_oDataStream = l_oResponse.GetResponseStream())
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.
l_oResponse.Close();
}
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;
}
}
}
}