Explorar el Código

Bugfix für bestehende Session-Variablen

Arne Diekmann hace 5 años
padre
commit
55d666e811

+ 28 - 0
GreenTree.Banking.API.comdirect/Extension/DictionaryExtension.cs

@@ -0,0 +1,28 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace GreenTree.Banking.API.comdirect.Extension
+{
+    public static class DictionaryExtension
+    {
+        /// <summary>
+        /// Adds a new or updates an existing value to the dictionary
+        /// </summary>
+        /// <typeparam name="TKey">Type of the key.</typeparam>
+        /// <typeparam name="TValue">Type of the value.</typeparam>
+        /// <param name="dictionary">The dictionary.</param>
+        /// <param name="key">The key.</param>
+        /// <param name="value">The value.</param>
+        public static void AddOrUpdate<TKey, TValue>(this Dictionary<TKey, TValue> dictionary, TKey key, TValue value)
+        {
+            if (dictionary == null)
+                throw new ArgumentNullException("dictionary", "The dictionary cannot be NULL");
+
+            if (dictionary.ContainsKey(key))
+                dictionary[key] = value;
+            else
+                dictionary.Add(key, value);
+        }
+    }
+}

+ 8 - 8
GreenTree.Banking.API.comdirect/Web/Session.cs

@@ -42,7 +42,7 @@ namespace GreenTree.Banking.API.comdirect.Web
         private RestClient authRestClient;
 
         // The variables which shall be 
-        private Dictionary<string, string> sessionVariables = new Dictionary<string, string>();
+        private readonly Dictionary<string, string> sessionVariables = new Dictionary<string, string>();
 
         // The cookies to be used on any request
         private IList<RestResponseCookie> cookies;
@@ -129,10 +129,10 @@ namespace GreenTree.Banking.API.comdirect.Web
             {
                 var responseData = JsonConvert.DeserializeObject<OAuth2Response>(response.Content);
 
-                sessionVariables.Add("access_token", responseData.AccessToken.ToString());
-                sessionVariables.Add("refresh_token", responseData.RefreshToken.ToString());
-                sessionVariables.Add("session_id", Guid.NewGuid().ToString());
-                sessionVariables.Add("request_id", GenerateRequestId());
+                sessionVariables.AddOrUpdate("access_token", responseData.AccessToken.ToString());
+                sessionVariables.AddOrUpdate("refresh_token", responseData.RefreshToken.ToString());
+                sessionVariables.AddOrUpdate("session_id", Guid.NewGuid().ToString());
+                sessionVariables.AddOrUpdate("request_id", GenerateRequestId());
             }
             else
             {
@@ -170,7 +170,7 @@ namespace GreenTree.Banking.API.comdirect.Web
             {
                 var responseData = JsonConvert.DeserializeObject<SessionResponse[]>(response.Content);
 
-                sessionVariables.Add("session_uuid", responseData[0].Identifier);
+                sessionVariables.AddOrUpdate("session_uuid", responseData[0].Identifier);
             }
             else
             {
@@ -216,7 +216,7 @@ namespace GreenTree.Banking.API.comdirect.Web
                     {
                         var responseData = JsonConvert.DeserializeObject<SessionTANResponse>(responseHeader.Value.ToString());
 
-                        sessionVariables.Add("challange_id", responseData.Id.ToString());
+                        sessionVariables.AddOrUpdate("challange_id", responseData.Id.ToString());
                     }
                 }
             }
@@ -392,7 +392,7 @@ namespace GreenTree.Banking.API.comdirect.Web
             {
                 var responseData = JsonConvert.DeserializeObject<Depot>(response.Content);
 
-                sessionVariables.Add("depotUUID", responseData.Values[0].DepotId);
+                sessionVariables.AddOrUpdate("depotUUID", responseData.Values[0].DepotId);
             }
             else
             {