summaryrefslogtreecommitdiff
path: root/FireBase/Http/HttpClientExtensions.cs
blob: 5d15c5944f8d8e259a5bf8a465d4947584332539 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
namespace Firebase.Database.Http
{
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Threading.Tasks;

    using Newtonsoft.Json;
    using System.Net;

    /// <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);
            }
        }
    }
}