Skip to content

Commit

Permalink
Code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
bugthesystem committed Mar 15, 2015
1 parent 03169de commit 9a8842a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 64 deletions.
17 changes: 14 additions & 3 deletions FireSharp.Test.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FireSharp.Config;
using System;
using FireSharp.Config;

namespace FireSharp.Test.Console
{
Expand Down Expand Up @@ -32,13 +33,23 @@ private static async void Crud()

private static async void EventStreaming()
{
await _client.DeleteAsync("chat");

await _client.OnAsync("chat",
added: (sender, args) => { System.Console.WriteLine(args.Data + "1"); },
added: async (sender, args) =>
{
System.Console.WriteLine(args.Data + "-> 1\n");
await _client.PushAsync("chat/", new
{
name = "someone",
text = "Console 1:" + DateTime.Now.ToString("f")
});
},
changed: (sender, args) => { System.Console.WriteLine(args.Data); },
removed: (sender, args) => { System.Console.WriteLine(args.Path); });

await _client.OnAsync("chat",
added: (sender, args) => { System.Console.WriteLine(args.Data + "2"); },
added: (sender, args) => { System.Console.WriteLine(args.Data + " -> 2\n"); },
changed: (sender, args) => { System.Console.WriteLine(args.Data); },
removed: (sender, args) => { System.Console.WriteLine(args.Path); });
}
Expand Down
8 changes: 4 additions & 4 deletions FireSharp.WebApp/Controllers/HomeController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace FireSharp.WebApp.Controllers
{
public class HomeController : Controller
{
private readonly IFirebaseClient _firebaseClient;
private readonly IFirebaseClient _client;

public HomeController(IFirebaseClient firebaseClient)
public HomeController(IFirebaseClient client)
{
_firebaseClient = firebaseClient;
_client = client;
}

public ActionResult Index()
Expand All @@ -21,7 +21,7 @@ public ActionResult Index()

public async Task<RedirectToRouteResult> CallFirebase()
{
await _firebaseClient.PushAsync("chat/", new
await _client.PushAsync("chat/", new
{
name = "someone",
text = "Hello from backend :" + DateTime.Now.ToString("f")
Expand Down
26 changes: 0 additions & 26 deletions FireSharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireSharp.Serialization.Jso
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireSharp.Serialization.ServiceStack", "FireSharp.Serialization.ServiceStack\FireSharp.Serialization.ServiceStack.csproj", "{7B0E3BC1-4C1D-4748-B678-3197E8E194FC}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FireSharp.Sample.Windows8App", "Samples\FireSharp.Sample.Windows8App\FireSharp.Sample.Windows8App.csproj", "{DEE14420-0A35-4E47-BB42-33C3EC691A8E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -96,30 +94,6 @@ Global
{7B0E3BC1-4C1D-4748-B678-3197E8E194FC}.Release|ARM.ActiveCfg = Release|Any CPU
{7B0E3BC1-4C1D-4748-B678-3197E8E194FC}.Release|x64.ActiveCfg = Release|Any CPU
{7B0E3BC1-4C1D-4748-B678-3197E8E194FC}.Release|x86.ActiveCfg = Release|Any CPU
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|ARM.ActiveCfg = Debug|ARM
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|ARM.Build.0 = Debug|ARM
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|ARM.Deploy.0 = Debug|ARM
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x64.ActiveCfg = Debug|x64
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x64.Build.0 = Debug|x64
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x64.Deploy.0 = Debug|x64
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x86.ActiveCfg = Debug|x86
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x86.Build.0 = Debug|x86
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Debug|x86.Deploy.0 = Debug|x86
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|Any CPU.Build.0 = Release|Any CPU
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|Any CPU.Deploy.0 = Release|Any CPU
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|ARM.ActiveCfg = Release|ARM
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|ARM.Build.0 = Release|ARM
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|ARM.Deploy.0 = Release|ARM
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x64.ActiveCfg = Release|x64
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x64.Build.0 = Release|x64
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x64.Deploy.0 = Release|x64
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x86.ActiveCfg = Release|x86
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x86.Build.0 = Release|x86
{DEE14420-0A35-4E47-BB42-33C3EC691A8E}.Release|x86.Deploy.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
7 changes: 4 additions & 3 deletions FireSharp/Config/FirebaseConfig.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
using FireSharp.Interfaces;
using System;
using FireSharp.Interfaces;

namespace FireSharp.Config
{
public class FirebaseConfig : IFirebaseConfig
{
public FirebaseConfig()
{
TimeoutInMinute = 1;
Serializer = new JsonNetSerializer();
}

public string BasePath { get; set; }
public string AuthSecret { get; set; }
public int TimeoutInMinute { get; set; }

public TimeSpan? RequestTimeout { get; set; }

public ISerializer Serializer { get; set; }
}
Expand Down
5 changes: 3 additions & 2 deletions FireSharp/Config/IFirebaseConfig.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using FireSharp.Interfaces;
using System;
using FireSharp.Interfaces;

namespace FireSharp.Config
{
public interface IFirebaseConfig
{
string BasePath { get; set; }
string AuthSecret { get; set; }
int TimeoutInMinute { get; set; }
TimeSpan? RequestTimeout { get; set; }
ISerializer Serializer { get; set; }
}
}
2 changes: 1 addition & 1 deletion FireSharp/FireSharp.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>FireSharp</id>
<version>2.0.1-beta3</version>
<version>2.0.1-beta4</version>
<title>Fire#</title>
<authors>ziyasal</authors>
<owners>ziyasal</owners>
Expand Down
49 changes: 24 additions & 25 deletions FireSharp/RequestManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ internal RequestManager(IFirebaseConfig config)
_client = GetClient();
}

private HttpClient GetClient()
private HttpClient GetClient(HttpClientHandler handler = null)
{
return new HttpClient
var client = handler == null ? new HttpClient() : new HttpClient(handler, true);

client.BaseAddress = new Uri(_config.BasePath);

if (_config.RequestTimeout.HasValue)
{
BaseAddress = new Uri(_config.BasePath),
Timeout = TimeSpan.FromMinutes(_config.TimeoutInMinute)
};
client.Timeout = _config.RequestTimeout.Value;
}

return client;
}

public void Dispose()
Expand Down Expand Up @@ -88,35 +93,19 @@ public HttpResponseMessage Patch<T>(string path, T data)

public HttpResponseMessage Listen(string path)
{
var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = true }, true)
{
BaseAddress = new Uri(_config.BasePath),
Timeout = TimeSpan.FromMinutes(_config.TimeoutInMinute)
};
var uri = PrepareUri(path);

var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
HttpRequestMessage request;
var client = PrepareEventStreamRequest(path, out request);

var response = client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead).Result;
response.EnsureSuccessStatusCode();

return response;

}

public async Task<HttpResponseMessage> ListenAsync(string path)
{
var client = new HttpClient(new HttpClientHandler { AllowAutoRedirect = true }, true)
{
BaseAddress = new Uri(_config.BasePath),
Timeout = TimeSpan.FromMinutes(_config.TimeoutInMinute)
};

var uri = PrepareUri(path);

var request = new HttpRequestMessage(HttpMethod.Get, uri);
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("text/event-stream"));
HttpRequestMessage request;
var client = PrepareEventStreamRequest(path, out request);

var response = await client.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);
response.EnsureSuccessStatusCode();
Expand Down Expand Up @@ -154,6 +143,16 @@ private HttpResponseMessage ProcessRequest(HttpMethod method, string path, objec
}
}

private HttpClient PrepareEventStreamRequest(string path, out HttpRequestMessage request)
{
var client = GetClient(new HttpClientHandler { AllowAutoRedirect = true });
var uri = PrepareUri(path);

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)
{
var uri = PrepareUri(path);
Expand Down

0 comments on commit 9a8842a

Please sign in to comment.