aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Web/TangoWebClientv2.cs
diff options
context:
space:
mode:
authorAvi Levkovich <avi@twine-s.com>2020-02-17 17:00:29 +0200
committerAvi Levkovich <avi@twine-s.com>2020-02-17 17:00:29 +0200
commitcbc80e71fac7d2896ac496b0fbf22051c0cdcff7 (patch)
tree9f898e565748f5581e900491ca36ce21c6462846 /Software/Visual_Studio/Tango.Web/TangoWebClientv2.cs
parentef657b4a3ae76e99ab077d6b4fc19691c2a0da4a (diff)
downloadTango-cbc80e71fac7d2896ac496b0fbf22051c0cdcff7.tar.gz
Tango-cbc80e71fac7d2896ac496b0fbf22051c0cdcff7.zip
merge
Diffstat (limited to 'Software/Visual_Studio/Tango.Web/TangoWebClientv2.cs')
-rw-r--r--Software/Visual_Studio/Tango.Web/TangoWebClientv2.cs125
1 files changed, 125 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Web/TangoWebClientv2.cs b/Software/Visual_Studio/Tango.Web/TangoWebClientv2.cs
new file mode 100644
index 000000000..3a4ff7756
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Web/TangoWebClientv2.cs
@@ -0,0 +1,125 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Security.Authentication;
+using System.Text;
+using System.Threading.Tasks;
+using Tango.Transport.Web;
+using Tango.Web.Security;
+
+namespace Tango.Web
+{
+ public abstract class TangoWebClientV2<TLoginRequest, TLoginResponse> : IDisposable where TLoginRequest : WebRequestMessage where TLoginResponse : WebTokenResponse
+ {
+ private WebTransportClient _client;
+ private bool _disposed;
+ private TLoginRequest _lastLoginRequest;
+
+ public String Address { get; set; }
+ public String Controller { get; private set; }
+ public String Token { get; private set; }
+ public WebToken WebToken { get; private set; }
+ public bool IsAuthenticated { get; private set; }
+
+ public TimeSpan RequestTimeout
+ {
+ get { return _client.RequestTimeout; }
+ set { _client.RequestTimeout = value; }
+ }
+
+ public TangoWebClientV2(String address, String controller)
+ {
+ _client = new WebTransportClient();
+ Controller = controller;
+ Address = address;
+ }
+
+ public TangoWebClientV2(String address, String controller, String token) : this(address, controller)
+ {
+ Token = token;
+ }
+
+ public TangoWebClientV2(TangoWebClientV2<TLoginRequest, TLoginResponse> cloned)
+ {
+ _client = new WebTransportClient();
+ Address = cloned.Address;
+ Controller = cloned.Controller;
+ IsAuthenticated = cloned.IsAuthenticated;
+ RequestTimeout = cloned.RequestTimeout;
+ WebToken = cloned.WebToken;
+ _lastLoginRequest = cloned._lastLoginRequest;
+ Token = cloned.Token;
+ }
+
+ public T Clone<T>() where T : TangoWebClientV2<TLoginRequest, TLoginResponse>
+ {
+ return Activator.CreateInstance(typeof(T), new object[] { this }) as T;
+ }
+
+ public async Task<TLoginResponse> Login(TLoginRequest request)
+ {
+ var response = await _client.PostJson<TLoginRequest, TLoginResponse>(GetActionAddress("Login"), request);
+ Token = response.AccessToken;
+ _client.AuthenticationToken = Token;
+
+ WebToken = WebToken.FromToken(Token);
+
+ _lastLoginRequest = request;
+ IsAuthenticated = true;
+ return response;
+ }
+
+ protected virtual async Task<TResponse> Post<TRequest, TResponse>(String action, TRequest request) where TRequest : class, IWebRequestMessage where TResponse : class, IWebResponseMessage
+ {
+ if (IsAuthenticated)
+ {
+ if (DateTime.UtcNow >= WebToken.Expiration)
+ {
+ await Login(_lastLoginRequest);
+ }
+ }
+
+ try
+ {
+ var response = await _client.PostJson<TRequest, TResponse>(GetActionAddress(action), request);
+ return response;
+ }
+ catch (TokenExpiredException ex)
+ {
+ try
+ {
+ await Login(_lastLoginRequest);
+ var response = await _client.PostJson<TRequest, TResponse>(GetActionAddress(action), request);
+ return response;
+ }
+ catch
+ {
+ throw;
+ }
+ }
+ catch (Exception ex)
+ {
+ throw;
+ }
+ }
+
+ private String GetActionAddress(String action)
+ {
+ return GetServiceAddress() + action;
+ }
+
+ protected virtual String GetServiceAddress()
+ {
+ return Address + $"/api/{Controller}/";
+ }
+
+ public virtual void Dispose()
+ {
+ if (!_disposed)
+ {
+ _disposed = true;
+ _client.Dispose();
+ }
+ }
+ }
+}