Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
digitallyborn committed May 10, 2010
1 parent a4523b7 commit f54c4bd
Show file tree
Hide file tree
Showing 23 changed files with 269 additions and 366 deletions.
9 changes: 8 additions & 1 deletion Twitterizer-Desktop/MainForm.cs
Expand Up @@ -124,7 +124,14 @@ private void AuthorizeAndGetUser()

ulong userId = ulong.Parse(ConfigurationManager.AppSettings["Twitterizer.Desktop.UserId"]);

this.user = TwitterUser.Show(this.oauthTokens, userId);
try
{
this.user = TwitterUser.Show(this.oauthTokens, userId);
}
catch (System.Exception)
{
throw;
}
}

/// <summary>
Expand Down
11 changes: 10 additions & 1 deletion Twitterizer2/Core/CommandPerformer.cs
Expand Up @@ -61,7 +61,16 @@ public static T PerformAction(ICommand<T> command)
};
}

T result = command.ExecuteCommand();
T result = default(T);

try
{
result = command.ExecuteCommand();
}
catch (System.Exception)
{
throw;
}

return result;
}
Expand Down
8 changes: 3 additions & 5 deletions Twitterizer2/Core/RequestStatus.cs
Expand Up @@ -36,10 +36,9 @@ namespace Twitterizer
{
using System;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml.Serialization;
using Twitterizer.Core;

/// <summary>
/// Describes the result status of a request
Expand Down Expand Up @@ -218,11 +217,10 @@ public static bool UpdateRequestStatus(HttpWebResponse webResponse)

if (webResponse.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(TwitterErrorDetails));
LastRequestStatus.ErrorDetails = ds.ReadObject(webResponse.GetResponseStream()) as TwitterErrorDetails;
LastRequestStatus.ErrorDetails = SerializationHelper<TwitterErrorDetails>.Deserialize(webResponse, Serializer.JSONdotNet, null);
}
}
catch (SerializationException)
catch (System.Runtime.Serialization.SerializationException)
{
// Do nothing. This is no-fail code.
}
Expand Down
48 changes: 27 additions & 21 deletions Twitterizer2/Core/SerializationHelper.cs
Expand Up @@ -37,19 +37,19 @@ namespace Twitterizer.Core
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

/// <summary>
/// Supported serialization schemes
/// </summary>
internal enum Serializer
{
/// <summary>
/// Utilizes the WCF DataContract serialization for JSON data.
/// Utilizes the Newtonsoft.Json library for serialization
/// </summary>
DataContractJsonSerializer,
JSONdotNet,

/// <summary>
/// Utilizes the AJAX JavaScript serialization classes for semi-manual JSON parsing.
Expand Down Expand Up @@ -80,27 +80,34 @@ internal static class SerializationHelper<T>
/// <returns>A strongly typed object representing the deserialized data of type <typeparamref name="T"/></returns>
public static T Deserialize(WebResponse webResponse, Serializer serializer, JavascriptConversionDelegate javascriptConversionDeligate)
{
T resultObject = default(T);

// Get the response
using (Stream responseStream = webResponse.GetResponseStream())
try
{
byte[] data = ConversionUtility.ReadStream(responseStream);
T resultObject = default(T);

// Get the response
using (Stream responseStream = webResponse.GetResponseStream())
{
byte[] data = ConversionUtility.ReadStream(responseStream);
#if DEBUG
Debug.WriteLine("----------- RESPONSE -----------");
Debug.WriteLine(Encoding.UTF8.GetString(data));
Debug.WriteLine("----------- END -----------");
Debug.WriteLine("----------- RESPONSE -----------");
Debug.WriteLine(Encoding.UTF8.GetString(data));
Debug.WriteLine("----------- END -----------");
#endif

// Deserialize the results.
resultObject = Deserialize(serializer, javascriptConversionDeligate, data);
// Deserialize the results.
resultObject = Deserialize(serializer, javascriptConversionDeligate, data);

responseStream.Close();
responseStream.Close();

Trace.Write(resultObject, "Twitterizer2");
}
Trace.Write(resultObject, "Twitterizer2");
}

return resultObject;
return resultObject;
}
catch (System.Exception)
{
throw;
}
}

/// <summary>
Expand All @@ -116,15 +123,14 @@ public static T Deserialize(Serializer serializer, JavascriptConversionDelegate

switch (serializer)
{
case Serializer.DataContractJsonSerializer:
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(T));
resultObject = (T)ds.ReadObject(new MemoryStream(data));
break;
case Serializer.JavaScriptSerializer:
JavaScriptSerializer jss = new JavaScriptSerializer();
object result = jss.DeserializeObject(Encoding.UTF8.GetString(data));
resultObject = javascriptConversionDeligate(result);
break;
case Serializer.JSONdotNet:
resultObject = JsonConvert.DeserializeObject<T>(Encoding.UTF8.GetString(data));
break;
}

return resultObject;
Expand Down
10 changes: 7 additions & 3 deletions Twitterizer2/Core/TwitterCommand.cs
Expand Up @@ -217,7 +217,7 @@ public T ExecuteCommand()

// Declare the variable to be returned
T resultObject = default(T);

try
{
// This must be set for all twitter request.
Expand Down Expand Up @@ -247,8 +247,8 @@ public T ExecuteCommand()
System.Net.ServicePointManager.Expect100Continue = true;

resultObject = SerializationHelper<T>.Deserialize(
webResponse,
this.SelectedSerializer,
webResponse,
this.SelectedSerializer,
this.ConvertJavaScriptSerializedObject);

this.AddResultToCache(cacheKeyBuilder, cache, resultObject);
Expand All @@ -272,6 +272,10 @@ public T ExecuteCommand()

return default(T);
}
catch (System.Exception)
{
throw;
}

// Pass the current oauth tokens into the new object, so method calls from there will keep the authentication.
resultObject.Tokens = this.Tokens;
Expand Down
1 change: 0 additions & 1 deletion Twitterizer2/Core/TwitterObject.cs
Expand Up @@ -39,7 +39,6 @@ namespace Twitterizer.Core
/// <summary>
/// The base object class
/// </summary>
[DataContract]
[System.Serializable]
public class TwitterObject : ITwitterObject
{
Expand Down
39 changes: 34 additions & 5 deletions Twitterizer2/Exceptions/TwitterErrorDetails.cs
Expand Up @@ -34,31 +34,60 @@

namespace Twitterizer
{
using System.Runtime.Serialization;
using System.Xml.Serialization;
using Newtonsoft.Json;
using Twitterizer.Core;

/// <summary>
/// Twitter Error Details class
/// </summary>
/// <remarks>Often, twitter returns error details in the body of response. This class represents the data structure of the error for deserialization.</remarks>
[DataContract]
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
[XmlRoot("hash")]
public class TwitterErrorDetails
public class TwitterErrorDetails : ITwitterObject
{
/// <summary>
/// Gets or sets the request path.
/// </summary>
/// <value>The request path.</value>
[DataMember(Name = "request")]
[JsonProperty(PropertyName = "request")]
[XmlElement("request")]
public string RequestPath { get; set; }

/// <summary>
/// Gets or sets the error message.
/// </summary>
/// <value>The error message.</value>
[DataMember(Name = "error")]
[JsonProperty(PropertyName = "error")]
[XmlElement("error")]
public string ErrorMessage { get; set; }

#region ITwitterObject Members

public RateLimiting RateLimiting
{
get
{
throw new System.NotImplementedException();
}
set
{
throw new System.NotImplementedException();
}
}

public OAuthTokens Tokens
{
get
{
throw new System.NotImplementedException();
}
set
{
throw new System.NotImplementedException();
}
}

#endregion
}
}
6 changes: 2 additions & 4 deletions Twitterizer2/Exceptions/TwitterizerException.cs
Expand Up @@ -36,11 +36,10 @@ namespace Twitterizer
using System.Globalization;
using System.IO;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Security.Permissions;
using System.Text;
using Twitterizer.Core;
using System.Runtime.Serialization;

/// <summary>
/// The Twitterizer Exception
Expand Down Expand Up @@ -91,9 +90,8 @@ public TwitterizerException(string message)

if (response.ContentType.StartsWith("application/json", StringComparison.OrdinalIgnoreCase))
{
DataContractJsonSerializer ds = new DataContractJsonSerializer(typeof(TwitterErrorDetails));
webException.Response.GetResponseStream().Seek(0, SeekOrigin.Begin);
this.ErrorDetails = ds.ReadObject(webException.Response.GetResponseStream()) as TwitterErrorDetails;
this.ErrorDetails = SerializationHelper<TwitterErrorDetails>.Deserialize(response, Serializer.JSONdotNet, null);
}
else if (response.ContentType.StartsWith("text/xml", StringComparison.OrdinalIgnoreCase))
{
Expand Down
11 changes: 6 additions & 5 deletions Twitterizer2/Methods/Account/TwitterRateLimitStatus.cs
Expand Up @@ -35,13 +35,14 @@ namespace Twitterizer
{
using System;
using System.Globalization;
using System.Runtime.Serialization;
using Newtonsoft.Json;
using Twitterizer.Core;

/// <summary>
/// The Twitter Rate Limit Status class
/// </summary>
[DataContract, Serializable]
[Serializable]
[JsonObject(MemberSerialization=MemberSerialization.OptIn)]
public class TwitterRateLimitStatus : TwitterObject
{
#region Constructors
Expand Down Expand Up @@ -69,21 +70,21 @@ protected TwitterRateLimitStatus(OAuthTokens tokens)
/// Gets or sets the remaining hits.
/// </summary>
/// <value>The remaining hits.</value>
[DataMember(Name = "remaining_hits")]
[JsonProperty(PropertyName = "remaining_hits")]
public int RemainingHits { get; set; }

/// <summary>
/// Gets or sets the hourly limit.
/// </summary>
/// <value>The hourly limit.</value>
[DataMember(Name = "hourly_limit")]
[JsonProperty(PropertyName = "hourly_limit")]
public int HourlyLimit { get; set; }

/// <summary>
/// Gets or sets the UTC string value of the time rate limiting will reset.
/// </summary>
/// <value>The reset time string.</value>
[DataMember(Name = "reset_time")]
[JsonProperty(PropertyName = "reset_time")]
public string ResetTimeString { get; set; }
#endregion

Expand Down

0 comments on commit f54c4bd

Please sign in to comment.