Permalink
Browse files

address issue #157 - record to TextWriter

  • Loading branch information...
1 parent 3f86fbe commit 2f1f56bcf13152b134d2e77b988fa8e567cc9cd5 @bitpusher bitpusher committed May 20, 2012
View
41 src/CIAPI.IntegrationTests/Rpc/RecordingFixture.cs
@@ -19,13 +19,52 @@ namespace CIAPI.IntegrationTests.Rpc
public class RecordingFixture : RpcFixtureBase
{
[Test]
+ public void HowToUseRecorderWithStream()
+ {
+ var rpcClient = new Client(Settings.RpcUri, Settings.StreamingUri, AppKey);
+
+ // start recording requests
+ var sb = new StringBuilder();
+ rpcClient.StartRecording(new StringWriter(sb));
+
+ rpcClient.LogIn(Settings.RpcUserName, Settings.RpcPassword);
+
+
+
+
+
+ // get some headlines
+ var headlines = rpcClient.News.ListNewsHeadlinesWithSource("dj", "UK", 100);
+
+ // get a story id from one of the headlines
+ var storyId = headlines.Headlines[0].StoryId;
+
+ // get the body of the story
+ // the api team has yet again broken an established and published interface with yet another
+ // half baked change. if you are going to require a key to retrieve detail you need to provide
+ // the key on the master. again, the client is required to maintain corellation information (i.e. market type)
+ // not impressed.
+ var storyDetail = rpcClient.News.GetNewsDetail("dj", storyId.ToString());
+
+ Assert.IsNotNullOrEmpty(storyDetail.NewsDetail.Story, "story was empty?");
+
+
+ rpcClient.LogOut();
+ rpcClient.StopRecording();
+
+ var output = sb.ToString();
+ // NOTE: data written to stream is not parsable JSON. it is composed of discrete json fragments separated by "+=_______=+"
+ Assert.IsTrue(output.Contains("\"Target\": \"https://ciapi.cityindex.com/tradingapi/session\""));
+ }
+
+ [Test]
public void HowToUseRecorder()
{
var rpcClient = new Client(Settings.RpcUri, Settings.StreamingUri, AppKey);
// start recording requests
- rpcClient.StartRecording();
+ rpcClient.StartRecording(null);
rpcClient.LogIn(Settings.RpcUserName, Settings.RpcPassword);
View
8 src/CIAPI.Serializer/Serializer.cs
@@ -11,13 +11,13 @@ namespace CIAPI.Serialization
{
public class Serializer : Salient.ReliableHttpClient.Serialization.IJsonSerializer
{
-
+
public string SerializeObject(object value)
{
//Json.NET 4.5 changed the default dateformatting to ISO 8601 - http://james.newtonking.com/archive/2012/03/20/json-net-4-5-release-1-iso-dates-async-metro-build.aspx
//Change it back to the previous default of MS style dates i.e - /Date(19372927629377)/
- return JsonConvert.SerializeObject(value, new JsonSerializerSettings{ DateFormatHandling = DateFormatHandling.MicrosoftDateFormat });
+ return JsonConvert.SerializeObject(value, new JsonSerializerSettings { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat, Formatting = Formatting.Indented });
}
public T DeserializeObject<T>(string json)
@@ -29,9 +29,9 @@ public T DeserializeObject<T>(string json)
}
- catch
+ catch
{
-
+
//swallow for now
}
View
2 src/CIAPI/Rpc/ApiClient.cs
@@ -278,7 +278,7 @@ private void DisposeMetricsTimer()
public void StartMetrics()
{
DisposeMetricsTimer();
- StartRecording();
+ StartRecording(null);
_metricsTimer = new Timer(ignored => PostMetrics(), null, 1000, 10000);
}
public void StopMetrics()
View
6 src/ReliableHttpClient/Salient.ReliableHttpClient/ClientBase.cs
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
+using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
@@ -341,10 +342,13 @@ public void Dispose()
#region Recording
- public void StartRecording()
+
+ public void StartRecording(TextWriter writer)
{
+ Controller.Recorder.Writer = writer;
Controller.Recorder.Paused = false;
}
+
public void StopRecording()
{
View
21 src/ReliableHttpClient/Salient.ReliableHttpClient/Recorder.cs
@@ -1,19 +1,26 @@
using System.Collections.Generic;
+using System.IO;
using System.Threading;
+using Salient.ReliableHttpClient.Serialization;
namespace Salient.ReliableHttpClient
{
public class Recorder
{
+ public const string Separator = "+=_____________________________________________________________________________=+";
+ public TextWriter Writer { get; set; }
+ public IJsonSerializer Serializer { get; set; }
public bool Paused { get; set; }
private List<RequestInfoBase> Requests { get; set; }
- public Recorder()
+ public Recorder(IJsonSerializer serializer)
{
+ Serializer = serializer;
Requests = new List<RequestInfoBase>();
}
+
/// <summary>
/// allows async processingcomplete handlers a chance to add to the recorder.
/// </summary>
@@ -29,8 +36,10 @@ public void Clear()
{
Requests.Clear();
}
-
+
}
+
+
public List<RequestInfoBase> GetRequests()
{
Wait(500);
@@ -40,7 +49,7 @@ public List<RequestInfoBase> GetRequests()
Requests.ForEach(r => result.Add(r.Copy()));
return result;
}
-
+
}
public void AddRequest(RequestInfoBase info)
{
@@ -50,6 +59,12 @@ public void AddRequest(RequestInfoBase info)
}
lock (Requests)
{
+ if (Writer != null)
+ {
+ var json = Serializer.SerializeObject(info);
+ Writer.WriteLine(Separator);
+ Writer.WriteLine(json);
+ }
Requests.Add(info.Copy());
}
View
9 src/ReliableHttpClient/Salient.ReliableHttpClient/RequestController.cs
@@ -50,17 +50,12 @@ public class RequestController : IDisposable
{
_requestFactory = requestFactory;
}
+
public RequestController(IJsonSerializer serializer)
- : this()
{
_serializer = serializer;
-
- }
-
- private RequestController()
- {
- Recorder = new Recorder { Paused = true };
+ Recorder = new Recorder(_serializer) { Paused = true };
Id = Guid.NewGuid();
Log.Debug("creating RequestController: " + Id);
_requestFactory = new RequestFactory();
View
2 src/ReliableHttpClient/Salient.ReliableHttpClient/RequestInfoBase.cs
@@ -164,7 +164,7 @@ private string Sanitize(object value)
{
value = "NULL";
}
- string result = Regex.Replace(value.ToString(), "\"Password\":\"password\"", "\"Password\":\"XXXXXX\"");
+ string result = Regex.Replace(value.ToString(), "\"Password\":\\s?\"password\"", "\"Password\": \"XXXXXX\"");
return result;
}
public override string ToString()

0 comments on commit 2f1f56b

Please sign in to comment.