diff --git a/FireSharp.WebApp/Controllers/HomeController.cs b/FireSharp.WebApp/Controllers/HomeController.cs index c452489..27ee2de 100644 --- a/FireSharp.WebApp/Controllers/HomeController.cs +++ b/FireSharp.WebApp/Controllers/HomeController.cs @@ -29,5 +29,26 @@ public async Task CallFirebase() return RedirectToAction("Index"); } + + public ActionResult CallFirebaseSync() + { + _client.Push("chat/", new + { + name = "someone", + text = "Hello from backend :" + DateTime.Now.ToString("f") + }); + + return Content("hello"); + } + + public ActionResult CallFirebaseSync2() + { + _client.PushAsync("chat/", new { + name = "someone", + text = "Hello from backend :" + DateTime.Now.ToString("f") + }).Wait(); + + return Content("hello"); + } } } \ No newline at end of file diff --git a/FireSharp/FirebaseClient.cs b/FireSharp/FirebaseClient.cs index f9a2827..14563c8 100644 --- a/FireSharp/FirebaseClient.cs +++ b/FireSharp/FirebaseClient.cs @@ -42,7 +42,7 @@ public FirebaseResponse Get(string path) { try { - HttpResponseMessage response = _requestManager.Get(path); + HttpResponseMessage response = _requestManager.GetAsync(path).Result; string content = response.Content.ReadAsStringAsync().Result; HandleIfErrorResponse(response.StatusCode, content); return new FirebaseResponse(content, response.StatusCode); @@ -57,7 +57,7 @@ public SetResponse Set(string path, T data) { try { - HttpResponseMessage response = _requestManager.Put(path, data); + HttpResponseMessage response = _requestManager.PutAsync(path, data).Result; string content = response.Content.ReadAsStringAsync().Result; HandleIfErrorResponse(response.StatusCode, content); return new SetResponse(content, response.StatusCode); @@ -72,7 +72,7 @@ public PushResponse Push(string path, T data) { try { - HttpResponseMessage response = _requestManager.Post(path, data); + HttpResponseMessage response = _requestManager.PostAsync(path, data).Result; string content = response.Content.ReadAsStringAsync().Result; HandleIfErrorResponse(response.StatusCode, content); return new PushResponse(content, response.StatusCode); @@ -87,7 +87,7 @@ public DeleteResponse Delete(string path) { try { - HttpResponseMessage response = _requestManager.Delete(path); + HttpResponseMessage response = _requestManager.DeleteAsync(path).Result; string content = response.Content.ReadAsStringAsync().Result; HandleIfErrorResponse(response.StatusCode, content); return new DeleteResponse(content, response.StatusCode); @@ -102,7 +102,7 @@ public FirebaseResponse Update(string path, T data) { try { - HttpResponseMessage response = _requestManager.Patch(path, data); + HttpResponseMessage response = _requestManager.PatchAsync(path, data).Result; string content = response.Content.ReadAsStringAsync().Result; HandleIfErrorResponse(response.StatusCode, content); return new FirebaseResponse(content, response.StatusCode); @@ -117,8 +117,8 @@ public async Task GetAsync(string path) { try { - HttpResponseMessage response = await _requestManager.GetAsync(path); - string content = await response.Content.ReadAsStringAsync(); + HttpResponseMessage response = await _requestManager.GetAsync(path).ConfigureAwait(false); + string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); HandleIfErrorResponse(response.StatusCode, content); return new FirebaseResponse(content, response.StatusCode); } @@ -132,8 +132,8 @@ public async Task SetAsync(string path, T data) { try { - HttpResponseMessage response = await _requestManager.PutAsync(path, data); - string content = await response.Content.ReadAsStringAsync(); + HttpResponseMessage response = await _requestManager.PutAsync(path, data).ConfigureAwait(false); + string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); HandleIfErrorResponse(response.StatusCode, content); return new SetResponse(content, response.StatusCode); } @@ -147,8 +147,8 @@ public async Task PushAsync(string path, T data) { try { - HttpResponseMessage response = await _requestManager.PostAsync(path, data); - string content = await response.Content.ReadAsStringAsync(); + HttpResponseMessage response = await _requestManager.PostAsync(path, data).ConfigureAwait(false); + string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); HandleIfErrorResponse(response.StatusCode, content); return new PushResponse(content, response.StatusCode); } @@ -162,8 +162,8 @@ public async Task DeleteAsync(string path) { try { - HttpResponseMessage response = await _requestManager.DeleteAsync(path); - string content = await response.Content.ReadAsStringAsync(); + HttpResponseMessage response = await _requestManager.DeleteAsync(path).ConfigureAwait(false); + string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); HandleIfErrorResponse(response.StatusCode, content); return new DeleteResponse(content, response.StatusCode); } @@ -177,8 +177,8 @@ public async Task UpdateAsync(string path, T data) { try { - HttpResponseMessage response = await _requestManager.PatchAsync(path, data); - string content = await response.Content.ReadAsStringAsync(); + HttpResponseMessage response = await _requestManager.PatchAsync(path, data).ConfigureAwait(false); + string content = await response.Content.ReadAsStringAsync().ConfigureAwait(false); HandleIfErrorResponse(response.StatusCode, content); return new FirebaseResponse(content, response.StatusCode); } @@ -193,18 +193,18 @@ public async Task ListenAsync(string path, ValueAddedEventH ValueChangedEventHandler changed = null, ValueRemovedEventHandler removed = null) { - return new EventStreamResponse(await _requestManager.ListenAsync(path), added, changed, removed); + return new EventStreamResponse(await _requestManager.ListenAsync(path).ConfigureAwait(false), added, changed, removed); } public async Task> OnChangeGetAsync(string path, ValueRootAddedEventHandler added = null) { - return new EventRootResponse(await _requestManager.ListenAsync(path), added, _requestManager, path); + return new EventRootResponse(await _requestManager.ListenAsync(path).ConfigureAwait(false), added, _requestManager, path); } public async Task OnAsync(string path, ValueAddedEventHandler added = null, ValueChangedEventHandler changed = null, ValueRemovedEventHandler removed = null) { - return new EventStreamResponse(await _requestManager.ListenAsync(path), added, changed, removed); + return new EventStreamResponse(await _requestManager.ListenAsync(path).ConfigureAwait(false), added, changed, removed); } private void HandleIfErrorResponse(HttpStatusCode statusCode, string content, Action errorHandler = null) diff --git a/FireSharp/Interfaces/IRequestManager.cs b/FireSharp/Interfaces/IRequestManager.cs index 791a1e3..04eb954 100644 --- a/FireSharp/Interfaces/IRequestManager.cs +++ b/FireSharp/Interfaces/IRequestManager.cs @@ -12,11 +12,5 @@ internal interface IRequestManager :IDisposable Task PostAsync(string path, T data); Task DeleteAsync(string path); Task PatchAsync(string path, T data); - HttpResponseMessage Listen(string path); - HttpResponseMessage Get(string path); - HttpResponseMessage Put(string path, T data); - HttpResponseMessage Post(string path, T data); - HttpResponseMessage Delete(string path); - HttpResponseMessage Patch(string path, T data); } } \ No newline at end of file diff --git a/FireSharp/RequestManager.cs b/FireSharp/RequestManager.cs index 1a7f834..035d431 100644 --- a/FireSharp/RequestManager.cs +++ b/FireSharp/RequestManager.cs @@ -35,66 +35,29 @@ public void Dispose() { } - public async Task GetAsync(string path) + public Task GetAsync(string path) { - return await ProcessRequestAsync(HttpMethod.Get, path, null); + return ProcessRequestAsync(HttpMethod.Get, path, null); } - public async Task PutAsync(string path, T data) + public Task PutAsync(string path, T data) { - return await ProcessRequestAsync(HttpMethod.Put, path, data); + return ProcessRequestAsync(HttpMethod.Put, path, data); } - public async Task PostAsync(string path, T data) + public Task PostAsync(string path, T data) { - return await ProcessRequestAsync(HttpMethod.Post, path, data); + return ProcessRequestAsync(HttpMethod.Post, path, data); } - public async Task DeleteAsync(string path) + public Task DeleteAsync(string path) { - return await ProcessRequestAsync(HttpMethod.Delete, path, null); + return ProcessRequestAsync(HttpMethod.Delete, path, null); } - public async Task PatchAsync(string path, T data) + public Task PatchAsync(string path, T data) { - return await ProcessRequestAsync(new HttpMethod("PATCH"), path, data); - } - - - public HttpResponseMessage Get(string path) - { - return ProcessRequest(HttpMethod.Get, path, null); - } - - public HttpResponseMessage Put(string path, T data) - { - return ProcessRequest(HttpMethod.Put, path, data); - } - - public HttpResponseMessage Post(string path, T data) - { - return ProcessRequest(HttpMethod.Post, path, data); - } - - public HttpResponseMessage Delete(string path) - { - return ProcessRequest(HttpMethod.Delete, path, null); - } - - public HttpResponseMessage Patch(string path, T data) - { - return ProcessRequest(new HttpMethod("PATCH"), path, data); - } - - public HttpResponseMessage Listen(string path) - { - HttpRequestMessage request; - var client = PrepareEventStreamRequest(path, out request); - - var response = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result; - response.EnsureSuccessStatusCode(); - - return response; + return ProcessRequestAsync(new HttpMethod("PATCH"), path, data); } public async Task ListenAsync(string path) @@ -108,28 +71,13 @@ public async Task ListenAsync(string path) return response; } - private async Task ProcessRequestAsync(HttpMethod method, string path, object payload, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead) - { - try - { - var request = PrepareRequest(method, path, payload); - - return await GetClient().SendAsync(request, httpCompletionOption); - } - catch (Exception ex) - { - throw new FirebaseException( - string.Format("An error occured while execute request. Path : {0} , Method : {1}", path, method), ex); - } - } - - private HttpResponseMessage ProcessRequest(HttpMethod method, string path, object payload, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead) + private Task ProcessRequestAsync(HttpMethod method, string path, object payload, HttpCompletionOption httpCompletionOption = HttpCompletionOption.ResponseContentRead) { try { var request = PrepareRequest(method, path, payload); - return GetClient().SendAsync(request, httpCompletionOption).Result; + return GetClient().SendAsync(request, httpCompletionOption); } catch (Exception ex) {