OSDN Git Service

ついっぷるフォトの投稿処理をOAuthEchoHandlerに移行
authorKimura Youichi <kim.upsilon@bucyou.net>
Tue, 3 May 2016 12:53:15 +0000 (21:53 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Wed, 4 May 2016 08:40:15 +0000 (17:40 +0900)
OpenTween/Connection/TwipplePhoto.cs
OpenTween/Twitter.cs

index c52d4e0..2afa7a5 100644 (file)
@@ -25,11 +25,13 @@ using System.Collections.Generic;
 using System.IO;
 using System.Linq;
 using System.Net;
+using System.Net.Http;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using System.Xml;
 using System.Xml.Linq;
 using System.Xml.XPath;
+using OpenTween.Api;
 using OpenTween.Api.DataModel;
 
 namespace OpenTween.Connection
@@ -61,7 +63,7 @@ namespace OpenTween.Connection
             this.twitter = twitter;
             this.twitterConfig = twitterConfig;
 
-            this.twippleApi = new TwippleApi(twitter.AccessToken, twitter.AccessTokenSecret);
+            this.twippleApi = new TwippleApi(twitter.Api);
         }
 
         #endregion
@@ -141,17 +143,21 @@ namespace OpenTween.Connection
             this.twitterConfig = config;
         }
 
-        public class TwippleApi : HttpConnectionOAuthEcho
+        public class TwippleApi
         {
+            private readonly HttpClient http;
+
             private static readonly Uri UploadEndpoint = new Uri("http://p.twipple.jp/api/upload2");
 
-            public TwippleApi(string twitterAccessToken, string twitterAccessTokenSecret)
-                : base(new Uri("http://api.twitter.com/"), new Uri("https://api.twitter.com/1.1/account/verify_credentials.json"))
+            private static readonly Uri OAuthRealm = new Uri("http://api.twitter.com/");
+            private static readonly Uri AuthServiceProvider = new Uri("https://api.twitter.com/1.1/account/verify_credentials.json");
+
+            public TwippleApi(TwitterApi twitterApi)
             {
-                this.Initialize(ApplicationSettings.TwitterConsumerKey, ApplicationSettings.TwitterConsumerSecret,
-                    twitterAccessToken, twitterAccessTokenSecret, "", "");
+                var handler = twitterApi.CreateOAuthEchoHandler(AuthServiceProvider, OAuthRealm);
 
-                this.InstanceTimeout = 60000;
+                this.http = Networking.CreateHttpClient(handler);
+                this.http.Timeout = TimeSpan.FromMinutes(1);
             }
 
             /// <summary>
@@ -163,25 +169,30 @@ namespace OpenTween.Connection
             {
                 // 参照: http://p.twipple.jp/wiki/API_Upload2/ja
 
-                var param = new Dictionary<string, string>
+                using (var request = new HttpRequestMessage(HttpMethod.Post, UploadEndpoint))
+                using (var multipart = new MultipartFormDataContent())
                 {
-                    ["upload_from"] = Application.ProductName,
-                };
-                var paramFiles = new List<KeyValuePair<string, IMediaItem>>
-                {
-                    new KeyValuePair<string, IMediaItem>("media", item),
-                };
-                var response = "";
-
-                var uploadTask = Task.Run(() => this.GetContent(HttpConnection.PostMethod,
-                    UploadEndpoint, param, paramFiles, ref response, null, null));
-
-                var ret = await uploadTask.ConfigureAwait(false);
-
-                if (ret != HttpStatusCode.OK)
-                    throw new WebApiException("Err:" + ret, response);
-
-                return XDocument.Parse(response);
+                    request.Content = multipart;
+
+                    using (var uploadFromContent = new StringContent(Application.ProductName))
+                    using (var mediaStream = item.OpenRead())
+                    using (var mediaContent = new StreamContent(mediaStream))
+                    {
+                        multipart.Add(uploadFromContent, "upload_from");
+                        multipart.Add(mediaContent, "media", item.Name);
+
+                        using (var response = await this.http.SendAsync(request).ConfigureAwait(false))
+                        {
+                            var responseText = await response.Content.ReadAsStringAsync()
+                                .ConfigureAwait(false);
+
+                            if (!response.IsSuccessStatusCode)
+                                throw new WebApiException(response.StatusCode.ToString(), responseText);
+
+                            return XDocument.Parse(responseText);
+                        }
+                    }
+                }
             }
         }
     }
index 205b727..a4b31e8 100644 (file)
@@ -144,6 +144,7 @@ namespace OpenTween
         /// </summary>
         public static readonly Regex DMSendTextRegex = new Regex(@"^DM? +(?<id>[a-zA-Z0-9_]+) +(?<body>.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
 
+        public TwitterApi Api { get; }
         public TwitterConfiguration Configuration { get; private set; }
 
         delegate void GetIconImageDelegate(PostClass post);
@@ -168,7 +169,6 @@ namespace OpenTween
         //private FavoriteQueue favQueue;
 
         private HttpTwitter twCon = new HttpTwitter();
-        private TwitterApi Api { get; }
 
         //private List<PostClass> _deletemessages = new List<PostClass>();