Skip to content

[WiP] Code cleanup before big work #51

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 29 additions & 5 deletions FireSharp/Config/FirebaseConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace FireSharp.Config
public class FirebaseConfig : IFirebaseConfig
{
private string _basePath;
private ISerializer _serializer;

public FirebaseConfig()
{
Expand All @@ -14,18 +15,41 @@ public FirebaseConfig()

public string BasePath
{
get
{
return _basePath.EndsWith("/") ? _basePath : $"{_basePath}/";
}
get { return _basePath.EndsWith("/") ? _basePath : $"{_basePath}/"; }
set { _basePath = value; }
}

public string Host { get; set; }

public string AuthSecret { get; set; }

/// <summary>
/// Gets or sets the request timeout.
/// </summary>
/// <value>
/// The request timeout.
/// </value>
public TimeSpan? RequestTimeout { get; set; }

public ISerializer Serializer { get; set; }
/// <summary>
/// Gets or sets the serializer instance.
/// </summary>
/// <value>
/// The currently used serializer.
/// </value>
/// <exception cref="System.ArgumentNullException">If an attempt to set a <code>null</code> instance is attempted</exception>
public ISerializer Serializer
{
get { return _serializer; }
set
{
if (value == null)
{
throw new ArgumentNullException(nameof(value));
}

_serializer = value;
}
}
}
}
1 change: 1 addition & 0 deletions FireSharp/FireSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
<Compile Include="Response\FirebaseResponse.cs" />
<Compile Include="Response\PushResponse.cs" />
<Compile Include="Response\SetResponse.cs" />
<Compile Include="ServerValues.cs" />
</ItemGroup>
<ItemGroup>
<Reference Include="Microsoft.Threading.Tasks">
Expand Down
47 changes: 27 additions & 20 deletions FireSharp/FirebaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,12 @@ public SetResponse Set<T>(string path, T data)
{
try
{
var response = _requestManager.RequestAsync(HttpMethod.Put, path, data).Result;
var content = response.Content.ReadAsStringAsync().Result;
HandleIfErrorResponse(response.StatusCode, content);
return new SetResponse(content, response.StatusCode);
using (var response = _requestManager.RequestAsync(HttpMethod.Put, path, data).Result)
{
var content = response.Content.ReadAsStringAsync().Result;
HandleIfErrorResponse(response.StatusCode, content);
return new SetResponse(content, response.StatusCode);
}
}
catch (HttpRequestException ex)
{
Expand All @@ -104,10 +106,12 @@ public PushResponse Push<T>(string path, T data)
{
try
{
var response = _requestManager.RequestAsync(HttpMethod.Post, path, data).Result;
var content = response.Content.ReadAsStringAsync().Result;
HandleIfErrorResponse(response.StatusCode, content);
return new PushResponse(content, response.StatusCode);
using (var response = _requestManager.RequestAsync(HttpMethod.Post, path, data).Result)
{
var content = response.Content.ReadAsStringAsync().Result;
HandleIfErrorResponse(response.StatusCode, content);
return new PushResponse(content, response.StatusCode);
}
}
catch (HttpRequestException ex)
{
Expand All @@ -119,10 +123,12 @@ public FirebaseResponse Delete(string path)
{
try
{
var response = _requestManager.RequestAsync(HttpMethod.Delete, path).Result;
var content = response.Content.ReadAsStringAsync().Result;
HandleIfErrorResponse(response.StatusCode, content);
return new FirebaseResponse(content, response.StatusCode);
using (var response = _requestManager.RequestAsync(HttpMethod.Delete, path).Result)
{
var content = response.Content.ReadAsStringAsync().Result;
HandleIfErrorResponse(response.StatusCode, content);
return new FirebaseResponse(content, response.StatusCode);
}
}
catch (HttpRequestException ex)
{
Expand Down Expand Up @@ -185,10 +191,12 @@ public async Task<SetResponse> SetAsync<T>(string path, T data)
{
try
{
var response = await _requestManager.RequestAsync(HttpMethod.Put, path, data).ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
HandleIfErrorResponse(response.StatusCode, content);
return new SetResponse(content, response.StatusCode);
using (var response = await _requestManager.RequestAsync(HttpMethod.Put, path, data).ConfigureAwait(false))
{
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
HandleIfErrorResponse(response.StatusCode, content);
return new SetResponse(content, response.StatusCode);
}
}
catch (HttpRequestException ex)
{
Expand Down Expand Up @@ -360,12 +368,11 @@ public async Task<FirebaseResponse> UpdateAsync<T>(string path, T data)
}

[Obsolete("This method is obsolete use OnAsync instead.")]
public async Task<EventStreamResponse> ListenAsync(string path, ValueAddedEventHandler added = null,
public Task<EventStreamResponse> ListenAsync(string path, ValueAddedEventHandler added = null,
ValueChangedEventHandler changed = null,
ValueRemovedEventHandler removed = null)
{
return new EventStreamResponse(await _requestManager.ListenAsync(path).ConfigureAwait(false), added, changed,
removed);
return OnAsync(path, added, changed, removed);
}

public async Task<EventRootResponse<T>> OnChangeGetAsync<T>(string path,
Expand Down Expand Up @@ -396,4 +403,4 @@ private void HandleIfErrorResponse(HttpStatusCode statusCode, string content,
}
}
}
}
}
18 changes: 18 additions & 0 deletions FireSharp/ServerValues.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System.Collections.Generic;

namespace FireSharp
{
/// <summary>
/// This class contains .NET representations of Firebase Server values.
/// </summary>
public static class ServerValues
{
/// <summary>
/// The time since UNIX epoch, in milliseconds.
/// </summary>
public static IDictionary<string, string> Timestamp => new Dictionary<string, string>
{
[".sv"] = "timestamp"
};
}
}