OSDN Git Service

HttpTwitter.UpdateStatusメソッドをTwitterApiクラスに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Fri, 29 Apr 2016 05:22:53 +0000 (14:22 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 29 Apr 2016 12:04:59 +0000 (21:04 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/HttpTwitter.cs
OpenTween/Connection/Imgur.cs
OpenTween/Connection/Mobypicture.cs
OpenTween/Connection/TwipplePhoto.cs
OpenTween/Connection/TwitterPhoto.cs
OpenTween/Connection/imgly.cs
OpenTween/Connection/yfrog.cs
OpenTween/Tween.cs
OpenTween/Twitter.cs

index 8cfcc66..96acede 100644 (file)
@@ -112,6 +112,35 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task StatusesUpdate_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.PostLazyAsync<TwitterStatus>(
+                        new Uri("statuses/update.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "status", "hogehoge" },
+                            { "include_entities", "true" },
+                            { "include_ext_alt_text", "true" },
+                            { "in_reply_to_status_id", "100" },
+                            { "media_ids", "10,20" },
+                        })
+                )
+                .ReturnsAsync(LazyJson.Create(new TwitterStatus()));
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.StatusesUpdate("hogehoge", replyToId: 100L, mediaIds: new[] { 10L, 20L })
+                    .IgnoreResponse()
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task StatusesDestroy_Test()
         {
             using (var twitterApi = new TwitterApi())
index c0515bc..b46f65f 100644 (file)
@@ -60,6 +60,24 @@ namespace OpenTween.Api
             return this.apiConnection.GetAsync<TwitterStatus>(endpoint, param, "/statuses/show/:id");
         }
 
+        public Task<LazyJson<TwitterStatus>> StatusesUpdate(string status, long? replyToId, IReadOnlyList<long> mediaIds)
+        {
+            var endpoint = new Uri("statuses/update.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["status"] = status,
+                ["include_entities"] = "true",
+                ["include_ext_alt_text"] = "true",
+            };
+
+            if (replyToId != null)
+                param["in_reply_to_status_id"] = replyToId.ToString();
+            if (mediaIds != null && mediaIds.Count > 0)
+                param.Add("media_ids", string.Join(",", mediaIds));
+
+            return this.apiConnection.PostLazyAsync<TwitterStatus>(endpoint, param);
+        }
+
         public Task<LazyJson<TwitterStatus>> StatusesDestroy(long statusId)
         {
             var endpoint = new Uri("statuses/destroy.json", UriKind.Relative);
index c48564e..2590e7f 100644 (file)
@@ -152,26 +152,6 @@ namespace OpenTween
             this.Initialize("", "", "", 0);
         }
 
-        public HttpStatusCode UpdateStatus(string status, long? replyToId, List<long> mediaIds, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("status", status);
-            if (replyToId != null) param.Add("in_reply_to_status_id", replyToId.ToString());
-            param.Add("include_entities", "true");
-            param.Add("include_ext_alt_text", "true");
-            //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
-
-            if (mediaIds != null && mediaIds.Count > 0)
-                param.Add("media_ids", string.Join(",", mediaIds));
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/statuses/update.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
         public HttpStatusCode UpdateStatusWithMedia(string status, long? replyToId, IMediaItem item, ref string content)
         {
             //画像投稿用エンドポイント
index a0ad5c1..455bbdc 100644 (file)
@@ -133,7 +133,7 @@ namespace OpenTween.Connection
 
             var textWithImageUrl = text + " " + imageUrl.Trim();
 
-            await Task.Run(() => this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId))
+            await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
                 .ConfigureAwait(false);
         }
 
index 6ca7c6c..37762f5 100644 (file)
@@ -147,7 +147,7 @@ namespace OpenTween.Connection
 
             var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
 
-            await Task.Run(() => this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId))
+            await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
                 .ConfigureAwait(false);
         }
 
index 85ab5bf..c52d4e0 100644 (file)
@@ -127,7 +127,7 @@ namespace OpenTween.Connection
 
             var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
 
-            await Task.Run(() => this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId))
+            await this.twitter.PostStatus(textWithImageUrl, inReplyToStatusId)
                 .ConfigureAwait(false);
         }
 
index 113112b..363beb2 100644 (file)
@@ -93,7 +93,7 @@ namespace OpenTween.Connection
                     throw new ArgumentException("Err:Media not found.");
             }
 
-            await Task.Run(() => this.tw.PostStatusWithMultipleMedia(text, inReplyToStatusId, mediaItems))
+            await this.tw.PostStatusWithMultipleMedia(text, inReplyToStatusId, mediaItems)
                 .ConfigureAwait(false);
         }
 
index 86b99a2..e564a07 100644 (file)
@@ -110,7 +110,7 @@ namespace OpenTween.Connection
 
             var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
 
-            await Task.Run(() => this.tw.PostStatus(textWithImageUrl, inReplyToStatusId))
+            await this.tw.PostStatus(textWithImageUrl, inReplyToStatusId)
                 .ConfigureAwait(false);
         }
 
index af98e4c..2864b94 100644 (file)
@@ -110,7 +110,7 @@ namespace OpenTween.Connection
 
             var textWithImageUrl = text + " " + imageUrlElm.Value.Trim();
 
-            await Task.Run(() => this.tw.PostStatus(textWithImageUrl, inReplyToStatusId))
+            await this.tw.PostStatus(textWithImageUrl, inReplyToStatusId)
                 .ConfigureAwait(false);
         }
 
index a503b62..219cf2b 100644 (file)
@@ -115,8 +115,8 @@ namespace OpenTween
         private SettingCommon _cfgCommon;
 
         //twitter解析部
-        private Twitter tw = new Twitter();
         private TwitterApi twitterApi = new TwitterApi();
+        private Twitter tw;
 
         //Growl呼び出し部
         private GrowlHelper gh = new GrowlHelper(Application.ProductName);
@@ -777,6 +777,7 @@ namespace OpenTween
                 this._cfgCommon.AutoShortUrlFirst = MyCommon.UrlConverter.Uxnu;
 
             HttpTwitter.TwitterUrl = this._cfgCommon.TwitterUrl;
+            this.tw = new Twitter(this.twitterApi);
 
             //認証関連
             if (string.IsNullOrEmpty(this._cfgCommon.Token)) this._cfgCommon.UserName = "";
@@ -3097,7 +3098,8 @@ namespace OpenTween
                 {
                     if (status.mediaItems == null || status.mediaItems.Length == 0)
                     {
-                        this.tw.PostStatus(status.status, status.inReplyToId);
+                        await this.tw.PostStatus(status.status, status.inReplyToId)
+                            .ConfigureAwait(false);
                     }
                     else
                     {
index d937376..bd8c3e7 100644 (file)
@@ -167,11 +167,17 @@ namespace OpenTween
         //private FavoriteQueue favQueue;
 
         private HttpTwitter twCon = new HttpTwitter();
+        private TwitterApi Api { get; }
 
         //private List<PostClass> _deletemessages = new List<PostClass>();
 
-        public Twitter()
+        public Twitter() : this(new TwitterApi())
         {
+        }
+
+        public Twitter(TwitterApi api)
+        {
+            this.Api = api;
             this.Configuration = TwitterConfiguration.DefaultConfiguration();
         }
 
@@ -462,7 +468,7 @@ namespace OpenTween
             return false;
         }
 
-        public void PostStatus(string postStr, long? reply_to, List<long> mediaIds = null)
+        public async Task PostStatus(string postStr, long? reply_to, IReadOnlyList<long> mediaIds = null)
         {
             this.CheckAccountState();
 
@@ -473,38 +479,11 @@ namespace OpenTween
                 return;
             }
 
-            HttpStatusCode res;
-            var content = "";
-            try
-            {
-                res = twCon.UpdateStatus(postStr, reply_to, mediaIds, ref content);
-            }
-            catch(Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message, ex);
-            }
-
-            // 投稿に成功していても404が返ることがあるらしい: https://dev.twitter.com/discussions/1213
-            if (res == HttpStatusCode.NotFound)
-                return;
-
-            this.CheckStatusCode(res, content);
+            var response = await this.Api.StatusesUpdate(postStr, reply_to, mediaIds)
+                .ConfigureAwait(false);
 
-            TwitterStatus status;
-            try
-            {
-                status = TwitterStatus.ParseJson(content);
-            }
-            catch(SerializationException ex)
-            {
-                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
-                throw new WebApiException("Err:Json Parse Error(DataContractJsonSerializer)", content, ex);
-            }
-            catch(Exception ex)
-            {
-                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
-                throw new WebApiException("Err:Invalid Json!", content, ex);
-            }
+            var status = await response.LoadJsonAsync()
+                .ConfigureAwait(false);
 
             this.UpdateUserStats(status.User);
 
@@ -559,7 +538,7 @@ namespace OpenTween
             }
         }
 
-        public void PostStatusWithMultipleMedia(string postStr, long? reply_to, IMediaItem[] mediaItems)
+        public async Task PostStatusWithMultipleMedia(string postStr, long? reply_to, IMediaItem[] mediaItems)
         {
             this.CheckAccountState();
 
@@ -580,7 +559,8 @@ namespace OpenTween
             if (mediaIds.Count == 0)
                 throw new WebApiException("Err:Invalid Files!");
 
-            PostStatus(postStr, reply_to, mediaIds);
+            await this.PostStatus(postStr, reply_to, mediaIds)
+                .ConfigureAwait(false);
         }
 
         public long UploadMedia(IMediaItem item)