OSDN Git Service

HttpTwitter.SendDirectMessageメソッドをTwitterApiクラスに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Fri, 29 Apr 2016 05:59:44 +0000 (14:59 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Fri, 29 Apr 2016 12:05:08 +0000 (21:05 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/HttpTwitter.cs
OpenTween/SendErrorReportForm.cs
OpenTween/Twitter.cs

index 96acede..254b257 100644 (file)
@@ -164,6 +164,32 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task DirectMessagesNew_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.PostLazyAsync<TwitterDirectMessage>(
+                        new Uri("direct_messages/new.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "text", "hogehoge" },
+                            { "screen_name", "opentween" },
+                        })
+                )
+                .ReturnsAsync(LazyJson.Create(new TwitterDirectMessage()));
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.DirectMessagesNew("hogehoge", "opentween")
+                    .IgnoreResponse()
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task DirectMessagesDestroy_Test()
         {
             using (var twitterApi = new TwitterApi())
index b46f65f..237236b 100644 (file)
@@ -89,6 +89,18 @@ namespace OpenTween.Api
             return this.apiConnection.PostLazyAsync<TwitterStatus>(endpoint, param);
         }
 
+        public Task<LazyJson<TwitterDirectMessage>> DirectMessagesNew(string status, string sendTo)
+        {
+            var endpoint = new Uri("direct_messages/new.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["text"] = status,
+                ["screen_name"] = sendTo,
+            };
+
+            return this.apiConnection.PostLazyAsync<TwitterDirectMessage>(endpoint, param);
+        }
+
         public Task<LazyJson<TwitterDirectMessage>> DirectMessagesDestroy(long statusId)
         {
             var endpoint = new Uri("direct_messages/destroy.json", UriKind.Relative);
index 09c8fcc..6255daa 100644 (file)
@@ -167,21 +167,6 @@ namespace OpenTween
                 null);
         }
 
-        public HttpStatusCode SendDirectMessage(string status, string sendto, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            param.Add("text", status);
-            param.Add("screen_name", sendto);
-            //if (AppendSettingDialog.Instance.ShortenTco && AppendSettingDialog.Instance.UrlConvertAuto) param.Add("wrap_links", "true")
-
-            return httpCon.GetContent(PostMethod,
-                this.CreateTwitterUri("/1.1/direct_messages/new.json"),
-                param,
-                ref content,
-                null,
-                null);
-        }
-
         public HttpStatusCode RetweetStatus(long id, ref string content)
         {
             Dictionary<string, string> param = new Dictionary<string, string>();
index 5b9bb52..cbea3e8 100644 (file)
@@ -165,7 +165,7 @@ namespace OpenTween
 
         public async Task SendByDmAsync()
         {
-            await Task.Run(() => this.tw.SendDirectMessage(this.EncodedReportForDM));
+            await this.tw.SendDirectMessage(this.EncodedReportForDM);
         }
 
         private void UpdateEncodedReport()
index 6fd1a53..69ea00f 100644 (file)
@@ -475,7 +475,8 @@ namespace OpenTween
             if (mediaIds == null &&
                 Twitter.DMSendTextRegex.IsMatch(postStr))
             {
-                SendDirectMessage(postStr);
+                await this.SendDirectMessage(postStr)
+                    .ConfigureAwait(false);
                 return;
             }
 
@@ -499,7 +500,8 @@ namespace OpenTween
 
             if (Twitter.DMSendTextRegex.IsMatch(postStr))
             {
-                SendDirectMessage(postStr);
+                await this.SendDirectMessage(postStr)
+                    .ConfigureAwait(false);
                 return;
             }
 
@@ -554,43 +556,20 @@ namespace OpenTween
             return status.MediaId;
         }
 
-        public void SendDirectMessage(string postStr)
+        public async Task SendDirectMessage(string postStr)
         {
             this.CheckAccountState();
             this.CheckAccessLevel(TwitterApiAccessLevel.ReadWriteAndDirectMessage);
 
             var mc = Twitter.DMSendTextRegex.Match(postStr);
 
-            HttpStatusCode res;
-            var content = "";
-            try
-            {
-                res = twCon.SendDirectMessage(mc.Groups["body"].Value, mc.Groups["id"].Value, ref content);
-            }
-            catch(Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message, ex);
-            }
-
-            this.CheckStatusCode(res, content);
+            var response = await this.Api.DirectMessagesNew(mc.Groups["body"].Value, mc.Groups["id"].Value)
+                .ConfigureAwait(false);
 
-            TwitterDirectMessage status;
-            try
-            {
-                status = TwitterDirectMessage.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 dm = await response.LoadJsonAsync()
+                .ConfigureAwait(false);
 
-            this.UpdateUserStats(status.Sender);
+            this.UpdateUserStats(dm.Sender);
         }
 
         public void PostRetweet(long id, bool read)