Skip to content

Instantly share code, notes, and snippets.

@mauricio
Created January 31, 2012 19:33
Show Gist options
  • Select an option

  • Save mauricio/1712438 to your computer and use it in GitHub Desktop.

Select an option

Save mauricio/1712438 to your computer and use it in GitHub Desktop.
private Result<T> HandleHttpResult<T> (BasicFuture<Result<T>> future, Action<Result<T>> action, Func<HttpResult> httpResultGenerator)
{
Result<T> result = null;
try {
HttpResult httpResult = httpResultGenerator ();
result = this.parser.ParseResponse<T> ((HttpStatusCode)httpResult.Code, httpResult.ContentType, httpResult.Content, typeof(T));
} catch (WebException e) {
Console.WriteLine (e);
if (e.Response != null && e.Response.GetResponseStream () != null) {
string data = IOUtils.ReadAll (e.Response.GetResponseStream ());
Console.WriteLine (data);
}
result = new Result<T> (default(T), 500, e.Message, e);
} catch (Exception e) {
Console.WriteLine (e);
result = new Result<T> (default(T), 500, e.Message, e);
}
future.SetResult (result);
if (action != null) {
action (result);
}
return result;
}
public Future<Result<Batch>> UploadFile (string path, long folderId = -1, Action<Result<Batch>> action = null)
{
IDictionary<string,string> parameters = new Dictionary<string,string> ();
if (folderId > 0) {
parameters ["folder_id"] = folderId.ToString ();
}
return this.HandleHttpResult<Batch> ( action, delegate {
return HttpHelper.HttpUploadFile (
this.Credential,
String.Format ("{0}/{1}", this.client.BaseUrl, UPLOAD),
path,
"uploaded_data",
ContentTypeRegistry.ContentTypeOf (path),
parameters);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment