diff options
author | Dennis Kobert <d-kobert@web.de> | 2019-06-11 23:38:13 +0200 |
---|---|---|
committer | Dennis Kobert <d-kobert@web.de> | 2019-06-11 23:38:13 +0200 |
commit | 2fa4a0e50ebfc97059c8b84dbd17e79f9afc8a8d (patch) | |
tree | c3b34ccb2737e347a73768536895cbbaab13cc01 /dsa/FireBase/Http/HttpClientExtensions.cs | |
parent | ec991104f56e90d7bb2878da2fe6ed4e585dfc46 (diff) | |
parent | af74efccf8d21e6151022b71f3cacd3fa83024ee (diff) |
Merge branch 'rework-backend'
Diffstat (limited to 'dsa/FireBase/Http/HttpClientExtensions.cs')
-rw-r--r-- | dsa/FireBase/Http/HttpClientExtensions.cs | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/dsa/FireBase/Http/HttpClientExtensions.cs b/dsa/FireBase/Http/HttpClientExtensions.cs new file mode 100644 index 0000000..6582769 --- /dev/null +++ b/dsa/FireBase/Http/HttpClientExtensions.cs @@ -0,0 +1,123 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Net.Http; +using System.Threading.Tasks; +using Newtonsoft.Json; + +namespace Firebase.Database.Http +{ + /// <summary> + /// The http client extensions for object deserializations. + /// </summary> + internal static class HttpClientExtensions + { + /// <summary> + /// The get object collection async. + /// </summary> + /// <param name="client"> The client. </param> + /// <param name="requestUri"> The request uri. </param> + /// <param name="jsonSerializerSettings"> The specific JSON Serializer Settings. </param> + /// <typeparam name="T"> The type of entities the collection should contain. </typeparam> + /// <returns> The <see cref="Task" />. </returns> + public static async Task<IReadOnlyCollection<FirebaseObject<T>>> GetObjectCollectionAsync<T>( + this HttpClient client, string requestUri, + JsonSerializerSettings jsonSerializerSettings) + { + var responseData = string.Empty; + var statusCode = HttpStatusCode.OK; + + try + { + var response = await client.GetAsync(requestUri).ConfigureAwait(false); + statusCode = response.StatusCode; + responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + var dictionary = + JsonConvert.DeserializeObject<Dictionary<string, T>>(responseData, jsonSerializerSettings); + + if (dictionary == null) return new FirebaseObject<T>[0]; + + return dictionary.Select(item => new FirebaseObject<T>(item.Key, item.Value)).ToList(); + } + catch (Exception ex) + { + throw new FirebaseException(requestUri, string.Empty, responseData, statusCode, ex); + } + } + + /*/// <summary> + /// The get object collection async. + /// </summary> + /// <param name="client"> The client. </param> + /// <param name="requestUri"> The request uri. </param> + /// /// <param name="dataType"> The Data Type. </param> + /// <param name="jsonSerializerSettings"> The specific JSON Serializer Settings. </param> + /// <typeparam name="T"> The type of entities the collection should contain. </typeparam> + /// <returns> The <see cref="Task"/>. </returns> + public static async Task<IReadOnlyCollection<FirebaseObject<object>>> GetObjectCollectionAsync(this HttpClient client, string requestUri, + JsonSerializerSettings jsonSerializerSettings, Type dataType) + { + var responseData = string.Empty; + var statusCode = HttpStatusCode.OK; + + try + { + var response = await client.GetAsync(requestUri).ConfigureAwait(false); + statusCode = response.StatusCode; + responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + + response.EnsureSuccessStatusCode(); + + Type dicType = typeof(Dictionary<,>).MakeGenericType(typeof(string), dataType); + + var dictionary = JsonConvert.DeserializeObject(responseData,dicType, jsonSerializerSettings) as Dictionary<string, object>; + + if (dictionary == null) + { + return new FirebaseObject<object>[0]; + } + + return dictionary.Select(item => new FirebaseObject<object>(item.Key, item.Value)).ToList(); + } + catch (Exception ex) + { + throw new FirebaseException(requestUri, string.Empty, responseData, statusCode, ex); + } + }*/ + + /// <summary> + /// The get object collection async. + /// </summary> + /// <param name="data"> The json data. </param> + /// <param name="elementType"> The type of entities the collection should contain. </param> + /// <returns> The <see cref="Task" />. </returns> + public static IEnumerable<FirebaseObject<object>> GetObjectCollection(this string data, Type elementType) + { + var dictionaryType = typeof(Dictionary<,>).MakeGenericType(typeof(string), elementType); + IDictionary dictionary = null; + + if (data.StartsWith("[")) + { + var listType = typeof(List<>).MakeGenericType(elementType); + var list = JsonConvert.DeserializeObject(data, listType) as IList; + dictionary = Activator.CreateInstance(dictionaryType) as IDictionary; + var index = 0; + foreach (var item in list) dictionary.Add(index++.ToString(), item); + } + else + { + dictionary = JsonConvert.DeserializeObject(data, dictionaryType) as IDictionary; + } + + if (dictionary == null) yield break; + + foreach (DictionaryEntry item in dictionary) + yield return new FirebaseObject<object>((string) item.Key, item.Value); + } + } +}
\ No newline at end of file |