diff --git a/FireSharp.Tests/FirebaseClientTests.cs b/FireSharp.Tests/FirebaseClientTests.cs index 4c127a4..2a461d2 100644 --- a/FireSharp.Tests/FirebaseClientTests.cs +++ b/FireSharp.Tests/FirebaseClientTests.cs @@ -89,7 +89,7 @@ public void SetAsyncFailure() public async Task GetAsync() { _firebaseRequestManagerMock.Setup(firebaseRequestManager => - firebaseRequestManager.RequestAsync(HttpMethod.Get, "todos", null)) + firebaseRequestManager.RequestAsync(HttpMethod.Get, "todos", string.Empty, null)) .Returns(Task.FromResult(_expectedResponse)); var firebaseResponse = await _firebaseClient.GetAsync("todos"); @@ -101,7 +101,7 @@ public async Task GetAsync() public void GetAsyncFailure() { _firebaseRequestManagerMock.Setup( - firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Get, "todos", null)) + firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Get, "todos", string.Empty, null)) .ReturnsAsync(_failureResponse); Assert.Throws(async () => await _firebaseClient.GetAsync("todos")); @@ -111,7 +111,7 @@ public void GetAsyncFailure() public void GetFailure() { _firebaseRequestManagerMock.Setup( - firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Get, "todos", null)) + firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Get, "todos", string.Empty, null)) .ReturnsAsync(_failureResponse); Assert.Throws(() => _firebaseClient.Get("todos")); @@ -121,7 +121,7 @@ public void GetFailure() public async Task DeleteAsync() { _firebaseRequestManagerMock.Setup(firebaseRequestManager => - firebaseRequestManager.RequestAsync(HttpMethod.Delete, "todos", null)) + firebaseRequestManager.RequestAsync(HttpMethod.Delete, "todos", string.Empty, null)) .Returns(Task.FromResult(_expectedResponse)); var response = await _firebaseClient.DeleteAsync("todos"); @@ -132,7 +132,7 @@ public async Task DeleteAsync() public void DeleteAsyncFailure() { _firebaseRequestManagerMock.Setup( - firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Delete, "todos", null)) + firebaseRequestManager => firebaseRequestManager.RequestAsync(HttpMethod.Delete, "todos", string.Empty, null)) .ReturnsAsync(_failureResponse); Assert.Throws(async () => await _firebaseClient.DeleteAsync("todos")); diff --git a/FireSharp/FirebaseClient.cs b/FireSharp/FirebaseClient.cs index 6231f8b..b1b9edf 100644 --- a/FireSharp/FirebaseClient.cs +++ b/FireSharp/FirebaseClient.cs @@ -53,6 +53,21 @@ public FirebaseResponse Get(string path) } } + public FirebaseResponse Get(string path, string query) + { + try + { + HttpResponseMessage response = _requestManager.RequestAsync(HttpMethod.Get, path, query).Result; + string content = response.Content.ReadAsStringAsync().Result; + HandleIfErrorResponse(response.StatusCode, content); + return new FirebaseResponse(content, response.StatusCode); + } + catch (HttpRequestException ex) + { + throw new FirebaseException(ex); + } + } + public SetResponse Set(string path, T data) { try @@ -113,6 +128,21 @@ public FirebaseResponse Update(string path, T data) } } + public async Task GetAsync(string path, string query) + { + try + { + HttpResponseMessage response = await _requestManager.RequestAsync(HttpMethod.Get, path, query).ConfigureAwait(false); + string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); + HandleIfErrorResponse(response.StatusCode, content); + return new FirebaseResponse(content, response.StatusCode); + } + catch (HttpRequestException ex) + { + throw new FirebaseException(ex); + } + } + public async Task GetAsync(string path) { try diff --git a/FireSharp/Interfaces/IFirebaseClient.cs b/FireSharp/Interfaces/IFirebaseClient.cs index 3952056..9373c54 100644 --- a/FireSharp/Interfaces/IFirebaseClient.cs +++ b/FireSharp/Interfaces/IFirebaseClient.cs @@ -8,12 +8,14 @@ namespace FireSharp.Interfaces public interface IFirebaseClient { Task GetAsync(string path); + Task GetAsync(string path, string query); Task> OnChangeGetAsync(string path, ValueRootAddedEventHandler added = null); Task SetAsync(string path, T data); Task PushAsync(string path, T data); Task DeleteAsync(string path); Task UpdateAsync(string path, T data); + FirebaseResponse Get(string path, string query); FirebaseResponse Get(string path); SetResponse Set(string path, T data); PushResponse Push(string path, T data); diff --git a/FireSharp/Interfaces/IRequestManager.cs b/FireSharp/Interfaces/IRequestManager.cs index 95b2601..ef66041 100644 --- a/FireSharp/Interfaces/IRequestManager.cs +++ b/FireSharp/Interfaces/IRequestManager.cs @@ -8,5 +8,7 @@ internal interface IRequestManager : IDisposable { Task ListenAsync(string path); Task RequestAsync(HttpMethod method, string path, object payload = null); + Task ListenAsync(string path, string query); + Task RequestAsync(HttpMethod method, string path, string query, object payload = null); } } \ No newline at end of file diff --git a/FireSharp/RequestManager.cs b/FireSharp/RequestManager.cs index 37d3525..c7c49fb 100644 --- a/FireSharp/RequestManager.cs +++ b/FireSharp/RequestManager.cs @@ -40,7 +40,18 @@ public void Dispose() public async Task ListenAsync(string path) { HttpRequestMessage request; - var client = PrepareEventStreamRequest(path, out request); + var client = PrepareEventStreamRequest(path, string.Empty, out request); + + var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); + response.EnsureSuccessStatusCode(); + + return response; + } + + public async Task ListenAsync(string path, string query) + { + HttpRequestMessage request; + var client = PrepareEventStreamRequest(path, query, out request); var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead); response.EnsureSuccessStatusCode(); @@ -52,7 +63,7 @@ public Task RequestAsync(HttpMethod method, string path, ob { try { - var request = PrepareRequest(method, path, payload); + var request = PrepareRequest(method, path, string.Empty, payload); return GetClient().SendAsync(request, HttpCompletionOption.ResponseContentRead); } @@ -63,10 +74,25 @@ public Task RequestAsync(HttpMethod method, string path, ob } } - private HttpClient PrepareEventStreamRequest(string path, out HttpRequestMessage request) + public Task RequestAsync(HttpMethod method, string path, string query, object payload) + { + try + { + var request = PrepareRequest(method, path, query, payload); + + return GetClient().SendAsync(request, HttpCompletionOption.ResponseContentRead); + } + catch (Exception ex) + { + throw new FirebaseException( + string.Format("An error occured while execute request. Path : {0} , Method : {1}", path, method), ex); + } + } + + private HttpClient PrepareEventStreamRequest(string path, string query, out HttpRequestMessage request) { var client = GetClient(new HttpClientHandler { AllowAutoRedirect = true }); - var uri = PrepareUri(path); + var uri = PrepareUri(path,query); request = new HttpRequestMessage(HttpMethod.Get, uri); request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream")); @@ -74,9 +100,9 @@ private HttpClient PrepareEventStreamRequest(string path, out HttpRequestMessage return client; } - private HttpRequestMessage PrepareRequest(HttpMethod method, string path, object payload) + private HttpRequestMessage PrepareRequest(HttpMethod method, string path, string query, object payload) { - var uri = PrepareUri(path); + var uri = PrepareUri(path,query); var request = new HttpRequestMessage(method, uri); @@ -89,13 +115,17 @@ private HttpRequestMessage PrepareRequest(HttpMethod method, string path, object return request; } - private Uri PrepareUri(string path) + private Uri PrepareUri(string path, string query) { var authToken = !string.IsNullOrWhiteSpace(_config.AuthSecret) ? string.Format("{0}.json?auth={1}", path, _config.AuthSecret) : string.Format("{0}.json", path); - - var url = string.Format("{0}{1}", _config.BasePath, authToken); + string addl = string.Empty; + if (!string.IsNullOrEmpty(query)) + { + addl = "&" + query; + } + var url = string.Format("{0}{1}{2}", _config.BasePath, authToken, addl); return new Uri(url); }