summaryrefslogtreecommitdiff
path: root/FireBase/Query/FirebaseQuery.cs
diff options
context:
space:
mode:
Diffstat (limited to 'FireBase/Query/FirebaseQuery.cs')
-rw-r--r--FireBase/Query/FirebaseQuery.cs162
1 files changed, 79 insertions, 83 deletions
diff --git a/FireBase/Query/FirebaseQuery.cs b/FireBase/Query/FirebaseQuery.cs
index 5e09795..60d0289 100644
--- a/FireBase/Query/FirebaseQuery.cs
+++ b/FireBase/Query/FirebaseQuery.cs
@@ -1,29 +1,27 @@
+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
{
- using System;
- using System.Collections.Generic;
- using System.Net.Http;
- using System.Reactive.Linq;
- using System.Threading.Tasks;
- using Http;
- using Offline;
- using Streaming;
- using Newtonsoft.Json;
- using System.Net;
-
/// <summary>
- /// Represents a firebase query.
+ /// Represents a firebase query.
/// </summary>
public abstract class FirebaseQuery : IFirebaseQuery, IDisposable
{
- protected TimeSpan DEFAULT_HTTP_CLIENT_TIMEOUT = new TimeSpan(0, 0, 180);
-
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>
+ /// 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>
@@ -34,16 +32,24 @@ namespace Firebase.Database.Query
}
/// <summary>
- /// Gets the client.
+ /// 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.
+ /// 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>
+ /// <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;
@@ -62,6 +68,39 @@ namespace Firebase.Database.Query
.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;
@@ -80,11 +119,11 @@ namespace Firebase.Database.Query
}*/
/// <summary>
- /// Assumes given query is pointing to a single object of type <typeparamref name="T"/> and retrieves it.
+ /// 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>
+ /// <returns> Single object of type <typeparamref name="T" />. </returns>
public async Task<T> OnceSingleAsync<T>(TimeSpan? timeout = null)
{
var responseData = string.Empty;
@@ -118,45 +157,12 @@ namespace Firebase.Database.Query
}
/// <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);
- }
-
- /// <summary>
- /// Posts given object to repository.
+ /// 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>
+ /// <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)
@@ -169,23 +175,21 @@ namespace Firebase.Database.Query
return new FirebaseObject<string>(key, data);
}
- else
- {
- 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);
- }
+ 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>
+ /// 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>
+ /// <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);
@@ -194,12 +198,12 @@ namespace Firebase.Database.Query
}
/// <summary>
- /// Sets or overwrites data at given location.
- /// </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>
+ /// <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);
@@ -208,10 +212,10 @@ namespace Firebase.Database.Query
}
/// <summary>
- /// Deletes data from given location.
+ /// Deletes data from given location.
/// </summary>
/// <param name="timeout"> Optional timeout value. </param>
- /// <returns> The <see cref="Task"/>. </returns>
+ /// <returns> The <see cref="Task" />. </returns>
public async Task DeleteAsync(TimeSpan? timeout = null)
{
var c = GetClient(timeout);
@@ -243,18 +247,10 @@ namespace Firebase.Database.Query
}
/// <summary>
- /// Disposes this instance.
- /// </summary>
- public void Dispose()
- {
- client?.Dispose();
- }
-
- /// <summary>
- /// Build the url segment of this child.
+ /// Build the url segment of this child.
/// </summary>
/// <param name="child"> The child of this query. </param>
- /// <returns> The <see cref="string"/>. </returns>
+ /// <returns> The <see cref="string" />. </returns>
protected abstract string BuildUrlSegment(FirebaseQuery child);
private string BuildUrl(FirebaseQuery child)