summaryrefslogtreecommitdiff
path: root/dsa/FireBase/Query/FirebaseQuery.cs
blob: 91e7afa3156d938f4f0ec412c9bc74db963cefaa (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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Firebase.Database.Http;
using Firebase.Database.Streaming;
using Newtonsoft.Json;

namespace Firebase.Database.Query {
    /// <summary>
    ///     Represents a firebase query.
    /// </summary>
    public abstract class FirebaseQuery : IFirebaseQuery, IDisposable {
        protected readonly FirebaseQuery Parent;

        private HttpClient client;
        protected TimeSpan DEFAULT_HTTP_CLIENT_TIMEOUT = new TimeSpan(0, 0, 180);

        /// <summary>
        ///     Initializes a new instance of the <see cref="FirebaseQuery" /> class.
        /// </summary>
        /// <param name="parent"> The parent of this query. </param>
        /// <param name="client"> The owning client. </param>
        protected FirebaseQuery(FirebaseQuery parent, FirebaseClient client) {
            Client = client;
            Parent = parent;
        }

        /// <summary>
        ///     Disposes this instance.
        /// </summary>
        public void Dispose() {
            client?.Dispose();
        }

        /// <summary>
        ///     Gets the client.
        /// </summary>
        public FirebaseClient Client { get; }

        /// <summary>
        ///     Queries the firebase server once returning collection of items.
        /// </summary>
        /// <param name="timeout"> Optional timeout value. </param>
        /// <typeparam name="T"> Type of elements. </typeparam>
        /// <returns> Collection of <see cref="FirebaseObject{T}" /> holding the entities returned by server. </returns>
        public async Task<IReadOnlyCollection<FirebaseObject<T>>> OnceAsync<T>(TimeSpan? timeout = null) {
            var url = string.Empty;

            try {
                url = await BuildUrlAsync().ConfigureAwait(false);
            }
            catch (Exception ex) {
                throw new FirebaseException("Couldn't build the url", string.Empty, string.Empty, HttpStatusCode.OK,
                    ex);
            }

            return await GetClient(timeout).GetObjectCollectionAsync<T>(url, Client.Options.JsonSerializerSettings)
                .ConfigureAwait(false);
        }

        /// <summary>
        ///     Starts observing this query watching for changes real time sent by the server.
        /// </summary>
        /// <typeparam name="T"> Type of elements. </typeparam>
        /// <param name="elementRoot"> Optional custom root element of received json items. </param>
        /// <returns> Observable stream of <see cref="FirebaseEvent{T}" />. </returns>
        public IObservable<FirebaseEvent<T>> AsObservable<T>(
            EventHandler<ExceptionEventArgs<FirebaseException>> exceptionHandler = null, string elementRoot = "") {
            return Observable.Create<FirebaseEvent<T>>(observer => {
                var sub = new FirebaseSubscription<T>(observer, this, elementRoot, new FirebaseCache<T>());
                sub.ExceptionThrown += exceptionHandler;
                return sub.Run();
            });
        }

        /// <summary>
        ///     Builds the actual URL of this query.
        /// </summary>
        /// <returns> The <see cref="string" />. </returns>
        public async Task<string> BuildUrlAsync() {
            // if token factory is present on the parent then use it to generate auth token
            if (Client.Options.AuthTokenAsyncFactory != null) {
                var token = await Client.Options.AuthTokenAsyncFactory().ConfigureAwait(false);
                return this.WithAuth(token).BuildUrl(null);
            }

            return BuildUrl(null);
        }

        /*public async Task<IReadOnlyCollection<FirebaseObject<Object>>> OnceAsync(Type dataType, TimeSpan? timeout = null)
        {
            var url = string.Empty;

            try
            {
                url = await this.BuildUrlAsync().ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                throw new FirebaseException("Couldn't build the url", string.Empty, string.Empty, HttpStatusCode.OK, ex);
            }

            return await this.GetClient(timeout).GetObjectCollectionAsync(url, Client.Options.JsonSerializerSettings, dataType)
                .ConfigureAwait(false);
        }*/

        /// <summary>
        ///     Assumes given query is pointing to a single object of type <typeparamref name="T" /> and retrieves it.
        /// </summary>
        /// <param name="timeout"> Optional timeout value. </param>
        /// <typeparam name="T"> Type of elements. </typeparam>
        /// <returns> Single object of type <typeparamref name="T" />. </returns>
        public async Task<T> OnceSingleAsync<T>(TimeSpan? timeout = null) {
            var responseData = string.Empty;
            var statusCode = HttpStatusCode.OK;
            var url = string.Empty;

            try {
                url = await BuildUrlAsync().ConfigureAwait(false);
            }
            catch (Exception ex) {
                throw new FirebaseException("Couldn't build the url", string.Empty, responseData, statusCode, ex);
            }

            try {
                var response = await GetClient(timeout).GetAsync(url).ConfigureAwait(false);
                statusCode = response.StatusCode;
                responseData = await response.Content.ReadAsStringAsync().ConfigureAwait(false);

                response.EnsureSuccessStatusCode();
                response.Dispose();

                return JsonConvert.DeserializeObject<T>(responseData, Client.Options.JsonSerializerSettings);
            }
            catch (Exception ex) {
                throw new FirebaseException(url, string.Empty, responseData, statusCode, ex);
            }
        }

        /// <summary>
        ///     Posts given object to repository.
        /// </summary>
        /// <param name="obj"> The object. </param>
        /// <param name="generateKeyOffline"> Specifies whether the key should be generated offline instead of online. </param>
        /// <param name="timeout"> Optional timeout value. </param>
        /// <typeparam name="T"> Type of <see cref="obj" /> </typeparam>
        /// <returns> Resulting firebase object with populated key. </returns>
        public async Task<FirebaseObject<string>> PostAsync(string data, bool generateKeyOffline = true,
            TimeSpan? timeout = null) {
            // post generates a new key server-side, while put can be used with an already generated local key
            if (generateKeyOffline) {
                var key = FirebaseKeyGenerator.Next();
                await new ChildQuery(this, () => key, Client).PutAsync(data).ConfigureAwait(false);

                return new FirebaseObject<string>(key, data);
            }

            var c = GetClient(timeout);
            var sendData = await SendAsync(c, data, HttpMethod.Post).ConfigureAwait(false);
            var result = JsonConvert.DeserializeObject<PostResult>(sendData, Client.Options.JsonSerializerSettings);

            return new FirebaseObject<string>(result.Name, data);
        }

        /// <summary>
        ///     Patches data at given location instead of overwriting them.
        /// </summary>
        /// <param name="obj"> The object. </param>
        /// <param name="timeout"> Optional timeout value. </param>
        /// <typeparam name="T"> Type of <see cref="obj" /> </typeparam>
        /// <returns> The <see cref="Task" />. </returns>
        public async Task PatchAsync(string data, TimeSpan? timeout = null) {
            var c = GetClient(timeout);

            await this.Silent().SendAsync(c, data, new HttpMethod("PATCH")).ConfigureAwait(false);
        }

        /// <summary>
        ///     Sets or overwrites data at given location.
        /// </summary>
        /// <param name="obj"> The object. </param>
        /// <param name="timeout"> Optional timeout value. </param>
        /// <typeparam name="T"> Type of <see cref="obj" /> </typeparam>
        /// <returns> The <see cref="Task" />. </returns>
        public async Task PutAsync(string data, TimeSpan? timeout = null) {
            var c = GetClient(timeout);

            await this.Silent().SendAsync(c, data, HttpMethod.Put).ConfigureAwait(false);
        }

        /// <summary>
        ///     Deletes data from given location.
        /// </summary>
        /// <param name="timeout"> Optional timeout value. </param>
        /// <returns> The <see cref="Task" />. </returns>
        public async Task DeleteAsync(TimeSpan? timeout = null) {
            var c = GetClient(timeout);
            var url = string.Empty;
            var responseData = string.Empty;
            var statusCode = HttpStatusCode.OK;

            try {
                url = await BuildUrlAsync().ConfigureAwait(false);
            }
            catch (Exception ex) {
                throw new FirebaseException("Couldn't build the url", string.Empty, responseData, statusCode, ex);
            }

            try {
                var result = await c.DeleteAsync(url).ConfigureAwait(false);
                statusCode = result.StatusCode;
                responseData = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                result.EnsureSuccessStatusCode();
            }
            catch (Exception ex) {
                throw new FirebaseException(url, string.Empty, responseData, statusCode, ex);
            }
        }

        /// <summary>
        ///     Build the url segment of this child.
        /// </summary>
        /// <param name="child"> The child of this query. </param>
        /// <returns> The <see cref="string" />. </returns>
        protected abstract string BuildUrlSegment(FirebaseQuery child);

        private string BuildUrl(FirebaseQuery child) {
            var url = BuildUrlSegment(child);

            if (Parent != null) url = Parent.BuildUrl(this) + url;

            return url;
        }

        private HttpClient GetClient(TimeSpan? timeout = null) {
            if (client == null) client = new HttpClient();

            if (!timeout.HasValue)
                client.Timeout = DEFAULT_HTTP_CLIENT_TIMEOUT;
            else
                client.Timeout = timeout.Value;

            return client;
        }

        private async Task<string> SendAsync(HttpClient client, string data, HttpMethod method) {
            var responseData = string.Empty;
            var statusCode = HttpStatusCode.OK;
            var requestData = data;
            var url = string.Empty;

            try {
                url = await BuildUrlAsync().ConfigureAwait(false);
            }
            catch (Exception ex) {
                throw new FirebaseException("Couldn't build the url", requestData, responseData, statusCode, ex);
            }

            var message = new HttpRequestMessage(method, url) {
                Content = new StringContent(requestData)
            };

            try {
                var result = await client.SendAsync(message).ConfigureAwait(false);
                statusCode = result.StatusCode;
                responseData = await result.Content.ReadAsStringAsync().ConfigureAwait(false);

                result.EnsureSuccessStatusCode();

                return responseData;
            }
            catch (Exception ex) {
                throw new FirebaseException(url, requestData, responseData, statusCode, ex);
            }
        }
    }
}