From e6181c24124d97f2fbc932b8a68311e625463156 Mon Sep 17 00:00:00 2001 From: uzvkl Date: Tue, 11 Jun 2019 23:05:52 +0200 Subject: Move dsa related stuff to subfolder --- dsa/FireBase/Http/HttpClientExtensions.cs | 123 ++++++++++++++++++++++++++++++ dsa/FireBase/Http/PostResult.cs | 13 ++++ 2 files changed, 136 insertions(+) create mode 100644 dsa/FireBase/Http/HttpClientExtensions.cs create mode 100644 dsa/FireBase/Http/PostResult.cs (limited to 'dsa/FireBase/Http') 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 +{ + /// + /// The http client extensions for object deserializations. + /// + internal static class HttpClientExtensions + { + /// + /// The get object collection async. + /// + /// The client. + /// The request uri. + /// The specific JSON Serializer Settings. + /// The type of entities the collection should contain. + /// The . + public static async Task>> GetObjectCollectionAsync( + 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>(responseData, jsonSerializerSettings); + + if (dictionary == null) return new FirebaseObject[0]; + + return dictionary.Select(item => new FirebaseObject(item.Key, item.Value)).ToList(); + } + catch (Exception ex) + { + throw new FirebaseException(requestUri, string.Empty, responseData, statusCode, ex); + } + } + + /*/// + /// The get object collection async. + /// + /// The client. + /// The request uri. + /// /// The Data Type. + /// The specific JSON Serializer Settings. + /// The type of entities the collection should contain. + /// The . + public static async Task>> 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; + + if (dictionary == null) + { + return new FirebaseObject[0]; + } + + return dictionary.Select(item => new FirebaseObject(item.Key, item.Value)).ToList(); + } + catch (Exception ex) + { + throw new FirebaseException(requestUri, string.Empty, responseData, statusCode, ex); + } + }*/ + + /// + /// The get object collection async. + /// + /// The json data. + /// The type of entities the collection should contain. + /// The . + public static IEnumerable> 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((string) item.Key, item.Value); + } + } +} \ No newline at end of file diff --git a/dsa/FireBase/Http/PostResult.cs b/dsa/FireBase/Http/PostResult.cs new file mode 100644 index 0000000..15a4894 --- /dev/null +++ b/dsa/FireBase/Http/PostResult.cs @@ -0,0 +1,13 @@ +namespace Firebase.Database.Http +{ + /// + /// Represents data returned after a successful POST to firebase server. + /// + public class PostResult + { + /// + /// Gets or sets the generated key after a successful post. + /// + public string Name { get; set; } + } +} \ No newline at end of file -- cgit v1.2.3-54-g00ecf