Working with Hammock and AgFx for Windows phone 7 app
Sorry if the question I am asking makes nonsense as I am really new to this. :(
I have a project which is already using Hammock for oAuth (version 1.0) authentication. And then I saw Shawn Burke's awesome data caching framework AgFx and I really want to be able to use it within my project.
But the problem I am having is, with Hammock I can easily make a request by using
var request = new RestRequest
{
Credentials = _credentials,
Path = "/fav.xml",
Method = WebMethod.Post
};
where I store access token, consumer key, etc. in the开发者_开发问答 _credentials object. And it does its magic for me so I don't need to convert them into the long oauth signature string.
But with AgFx, my understanding is, I need to use WebLoadRequest to request for the data, which looks like this
return new WebLoadRequest(loadContext, new Uri(myUri), "POST", "post-data");
where "post-data" should be the oauth signature string. Then basically I need to throw away what Hammock gives to me and rewrite the code to get the signature.
I just wonder if there's a better way of doing this? Or should I say if there's a libary I can use to get this signature easily?
Any help would be much appreciated!!
Cheers,
Xin
I have figured it out.
Shawn Burke who created this framework pointed me to the right direction.
Bisically what I've done is I create a HammockLoadRequest which inherits from AgFx's LoadRequest, and in the Execute override, replace the HttpWebRequest with Hammock's RestRequest and that's it.
public class HammockLoadRequest : LoadRequest
{
public OAuthCredentials Credentials { get; set; }
public string AuthorityUrl { get; set; }
/// <summary>
/// Create a HammockLoadRequest
/// </summary>
public HammockLoadRequest(LoadContext loadContext, OAuthCredentials credentials, string authorityUrl)
: base(loadContext)
{
Credentials = credentials;
AuthorityUrl = authorityUrl;
}
/// <summary>
/// Performs the actual get for this request.
/// </summary>
/// <param name="result"></param>
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
public override void Execute(Action<LoadRequestResult> result)
{
PriorityQueue.AddNetworkWorkItem(
() =>
{
var client = new RestClient
{
Authority = this.AuthorityUrl,
HasElevatedPermissions = true
};
var restRequest = new RestRequest
{
Credentials = this.Credentials,
Path = "/xxx.json",
};
restRequest.AddParameter("count", "5");
restRequest.AddParameter("include_rts", "1");
//if (sinceId != 0)
// request.AddParameter("since_id", sinceId.ToString());
RestCallback responseHandler = (request, response, userstate) =>
{
if (response.StatusCode != HttpStatusCode.OK)
{
Helpers.ShowMessage(String.Format("Error Getting Status: {0}", response.StatusCode));
return;
}
// convert string to stream
byte[] bytes = Encoding.UTF8.GetBytes(response.Content);
var stream = new MemoryStream(bytes);
stream.Close();
result(new LoadRequestResult(new MemoryStream(bytes)));
return;
};
client.BeginRequest(restRequest, responseHandler);
});
}
}
精彩评论