Skip to content

Commit

Permalink
Merge pull request bugthesystem#12 from pjhuck/configureawait
Browse files Browse the repository at this point in the history
Use configureAwait to allow use in non async paths
  • Loading branch information
bugthesystem committed Mar 23, 2015
2 parents e51c774 + c3d23ff commit 2afa283
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 88 deletions.
21 changes: 21 additions & 0 deletions FireSharp.WebApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,26 @@ public async Task<RedirectToRouteResult> 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");
}
}
}
36 changes: 18 additions & 18 deletions FireSharp/FirebaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -57,7 +57,7 @@ public SetResponse Set<T>(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);
Expand All @@ -72,7 +72,7 @@ public PushResponse Push<T>(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);
Expand All @@ -87,7 +87,7 @@ public FirebaseResponse 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 FirebaseResponse(content, response.StatusCode);
Expand All @@ -102,7 +102,7 @@ public FirebaseResponse Update<T>(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);
Expand All @@ -117,8 +117,8 @@ public async Task<FirebaseResponse> 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);
}
Expand All @@ -132,8 +132,8 @@ public async Task<SetResponse> SetAsync<T>(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);
}
Expand All @@ -147,8 +147,8 @@ public async Task<PushResponse> PushAsync<T>(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);
}
Expand All @@ -162,8 +162,8 @@ public async Task<FirebaseResponse> 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 FirebaseResponse(content, response.StatusCode);
}
Expand All @@ -177,8 +177,8 @@ public async Task<FirebaseResponse> UpdateAsync<T>(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);
}
Expand All @@ -193,18 +193,18 @@ public async Task<EventStreamResponse> 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<EventRootResponse<T>> OnChangeGetAsync<T>(string path, ValueRootAddedEventHandler<T> added = null)
{
return new EventRootResponse<T>(await _requestManager.ListenAsync(path), added, _requestManager, path);
return new EventRootResponse<T>(await _requestManager.ListenAsync(path).ConfigureAwait(false), added, _requestManager, path);
}

public async Task<EventStreamResponse> 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<HttpStatusCode, string> errorHandler = null)
Expand Down
6 changes: 0 additions & 6 deletions FireSharp/Interfaces/IRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,5 @@ internal interface IRequestManager :IDisposable
Task<HttpResponseMessage> PostAsync<T>(string path, T data);
Task<HttpResponseMessage> DeleteAsync(string path);
Task<HttpResponseMessage> PatchAsync<T>(string path, T data);
HttpResponseMessage Listen(string path);
HttpResponseMessage Get(string path);
HttpResponseMessage Put<T>(string path, T data);
HttpResponseMessage Post<T>(string path, T data);
HttpResponseMessage Delete(string path);
HttpResponseMessage Patch<T>(string path, T data);
}
}
76 changes: 12 additions & 64 deletions FireSharp/RequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,66 +35,29 @@ public void Dispose()
{
}

public async Task<HttpResponseMessage> GetAsync(string path)
public Task<HttpResponseMessage> GetAsync(string path)
{
return await ProcessRequestAsync(HttpMethod.Get, path, null);
return ProcessRequestAsync(HttpMethod.Get, path, null);
}

public async Task<HttpResponseMessage> PutAsync<T>(string path, T data)
public Task<HttpResponseMessage> PutAsync<T>(string path, T data)
{
return await ProcessRequestAsync(HttpMethod.Put, path, data);
return ProcessRequestAsync(HttpMethod.Put, path, data);
}

public async Task<HttpResponseMessage> PostAsync<T>(string path, T data)
public Task<HttpResponseMessage> PostAsync<T>(string path, T data)
{
return await ProcessRequestAsync(HttpMethod.Post, path, data);
return ProcessRequestAsync(HttpMethod.Post, path, data);
}

public async Task<HttpResponseMessage> DeleteAsync(string path)
public Task<HttpResponseMessage> DeleteAsync(string path)
{
return await ProcessRequestAsync(HttpMethod.Delete, path, null);
return ProcessRequestAsync(HttpMethod.Delete, path, null);
}

public async Task<HttpResponseMessage> PatchAsync<T>(string path, T data)
public Task<HttpResponseMessage> PatchAsync<T>(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<T>(string path, T data)
{
return ProcessRequest(HttpMethod.Put, path, data);
}

public HttpResponseMessage Post<T>(string path, T data)
{
return ProcessRequest(HttpMethod.Post, path, data);
}

public HttpResponseMessage Delete(string path)
{
return ProcessRequest(HttpMethod.Delete, path, null);
}

public HttpResponseMessage Patch<T>(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<HttpResponseMessage> ListenAsync(string path)
Expand All @@ -108,28 +71,13 @@ public async Task<HttpResponseMessage> ListenAsync(string path)
return response;
}

private async Task<HttpResponseMessage> 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<HttpResponseMessage> 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)
{
Expand Down

0 comments on commit 2afa283

Please sign in to comment.