Skip to content

Commit

Permalink
Adds query support for items like orderBy, startAt, endAt, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
tillerstarredwards committed Jul 14, 2015
1 parent 000978c commit e11ede7
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 14 deletions.
10 changes: 5 additions & 5 deletions FireSharp.Tests/FirebaseClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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<FirebaseException>(async () => await _firebaseClient.GetAsync("todos"));
Expand All @@ -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<FirebaseException>(() => _firebaseClient.Get("todos"));
Expand All @@ -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");
Expand All @@ -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<FirebaseException>(async () => await _firebaseClient.DeleteAsync("todos"));
Expand Down
30 changes: 30 additions & 0 deletions FireSharp/FirebaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(string path, T data)
{
try
Expand Down Expand Up @@ -113,6 +128,21 @@ public FirebaseResponse Update<T>(string path, T data)
}
}

public async Task<FirebaseResponse> 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<FirebaseResponse> GetAsync(string path)
{
try
Expand Down
2 changes: 2 additions & 0 deletions FireSharp/Interfaces/IFirebaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ namespace FireSharp.Interfaces
public interface IFirebaseClient
{
Task<FirebaseResponse> GetAsync(string path);
Task<FirebaseResponse> GetAsync(string path, string query);

Task<EventRootResponse<T>> OnChangeGetAsync<T>(string path, ValueRootAddedEventHandler<T> added = null);
Task<SetResponse> SetAsync<T>(string path, T data);
Task<PushResponse> PushAsync<T>(string path, T data);
Task<FirebaseResponse> DeleteAsync(string path);
Task<FirebaseResponse> UpdateAsync<T>(string path, T data);
FirebaseResponse Get(string path, string query);
FirebaseResponse Get(string path);
SetResponse Set<T>(string path, T data);
PushResponse Push<T>(string path, T data);
Expand Down
2 changes: 2 additions & 0 deletions FireSharp/Interfaces/IRequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@ internal interface IRequestManager : IDisposable
{
Task<HttpResponseMessage> ListenAsync(string path);
Task<HttpResponseMessage> RequestAsync(HttpMethod method, string path, object payload = null);
Task<HttpResponseMessage> ListenAsync(string path, string query);
Task<HttpResponseMessage> RequestAsync(HttpMethod method, string path, string query, object payload = null);
}
}
48 changes: 39 additions & 9 deletions FireSharp/RequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,18 @@ public void Dispose()
public async Task<HttpResponseMessage> 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<HttpResponseMessage> ListenAsync(string path, string query)
{
HttpRequestMessage request;
var client = PrepareEventStreamRequest(path, query, out request);

var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
Expand All @@ -52,7 +63,7 @@ public Task<HttpResponseMessage> 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);
}
Expand All @@ -63,20 +74,35 @@ public Task<HttpResponseMessage> RequestAsync(HttpMethod method, string path, ob
}
}

private HttpClient PrepareEventStreamRequest(string path, out HttpRequestMessage request)
public Task<HttpResponseMessage> 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"));

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);

Expand All @@ -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);
}
Expand Down

0 comments on commit e11ede7

Please sign in to comment.