PageRenderTime 60ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/Twitpic/TwitpicClient.cs

https://bitbucket.org/ugaya40/instant-image-uploader
C# | 96 lines | 78 code | 15 blank | 3 comment | 0 complexity | 31c7a22e40188cac208c5f4103d68478 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Codeplex.OAuth;
  6. using System.Net;
  7. using System.Reactive.Linq;
  8. using System.IO;
  9. namespace ImageUploader.Extensions.Twitpic
  10. {
  11. public class TwitpicClient : OAuthBase
  12. {
  13. const string ApiKey = "a6092a4ce2a252852f9d808849a4d344"; // set your apikey
  14. readonly AccessToken accessToken;
  15. public TwitpicClient(string consumerKey, string consumerSecret, AccessToken accessToken)
  16. : base(consumerKey, consumerSecret)
  17. {
  18. this.accessToken = accessToken;
  19. }
  20. private WebRequest CreateRequest(string url)
  21. {
  22. const string ServiceProvider = "https://api.twitter.com/1/account/verify_credentials.json";
  23. const string Realm = "http://api.twitter.com/";
  24. var req = WebRequest.Create(url);
  25. // generate oauth signature and parameters
  26. var parameters = ConstructBasicParameters(ServiceProvider, MethodType.Get, accessToken);
  27. // make auth header string
  28. var authHeader = BuildAuthorizationHeader(new[] { new Parameter("Realm", Realm) }.Concat(parameters));
  29. // set authenticate headers
  30. req.Headers["X-Verify-Credentials-Authorization"] = authHeader;
  31. req.Headers["X-Auth-Service-Provider"] = ServiceProvider;
  32. return req;
  33. }
  34. public IObservable<string> UploadPicture(string filename, string message, byte[] file)
  35. {
  36. var req = CreateRequest("http://api.twitpic.com/2/upload.xml"); // choose xml or json
  37. req.Method = "POST";
  38. var boundaryKey = Guid.NewGuid().ToString();
  39. var boundary = "--" + boundaryKey;
  40. req.ContentType = "multipart/form-data; boundary=" + boundaryKey;
  41. return Observable.Defer(() =>
  42. Observable.FromAsyncPattern<Stream>(req.BeginGetRequestStream, req.EndGetRequestStream)())
  43. .Do(stream =>
  44. {
  45. using (stream)
  46. using (var sw = new StreamWriter(stream, new UTF8Encoding(false)))
  47. {
  48. sw.WriteLine(boundary);
  49. sw.WriteLine("Content-Disposition: form-data; name=\"key\"");
  50. sw.WriteLine();
  51. sw.WriteLine(ApiKey);
  52. sw.WriteLine(boundary);
  53. sw.WriteLine("Content-Disposition: form-data; name=\"message\"");
  54. sw.WriteLine();
  55. sw.WriteLine(message);
  56. sw.WriteLine(boundary);
  57. sw.WriteLine("Content-Disposition: form-data; name=\"media\"; filename=\"" + filename + "\"");
  58. sw.WriteLine("Content-Type: application/octet-stream");
  59. sw.WriteLine("Content-Transfer-Encoding: binary");
  60. sw.WriteLine();
  61. sw.Flush();
  62. stream.Write(file, 0, file.Length);
  63. stream.Flush();
  64. sw.WriteLine();
  65. sw.WriteLine("--" + boundaryKey + "--");
  66. sw.Flush();
  67. }
  68. })
  69. .SelectMany(_ => Observable.FromAsyncPattern<WebResponse>(req.BeginGetResponse, req.EndGetResponse)())
  70. .Select(res =>
  71. {
  72. using (res)
  73. using (var stream = res.GetResponseStream())
  74. using (var sr = new StreamReader(stream, Encoding.UTF8))
  75. {
  76. return sr.ReadToEnd();
  77. }
  78. });
  79. }
  80. }
  81. }