OSDN Git Service

HttpTwitter.SearchメソッドをTwitterApiクラスに置き換え
authorKimura Youichi <kim.upsilon@bucyou.net>
Thu, 5 May 2016 05:23:16 +0000 (14:23 +0900)
committerKimura Youichi <kim.upsilon@bucyou.net>
Thu, 5 May 2016 17:01:25 +0000 (02:01 +0900)
OpenTween.Tests/Api/TwitterApiTest.cs
OpenTween/Api/TwitterApi.cs
OpenTween/Connection/HttpTwitter.cs
OpenTween/Tween.cs
OpenTween/Twitter.cs

index 5fc3489..87bebcb 100644 (file)
@@ -281,6 +281,38 @@ namespace OpenTween.Api
         }
 
         [Fact]
+        public async Task SearchTweets_Test()
+        {
+            using (var twitterApi = new TwitterApi())
+            {
+                var mock = new Mock<IApiConnection>();
+                mock.Setup(x =>
+                    x.GetAsync<TwitterSearchResult>(
+                        new Uri("search/tweets.json", UriKind.Relative),
+                        new Dictionary<string, string> {
+                            { "q", "from:twitterapi" },
+                            { "result_type", "recent" },
+                            { "include_entities", "true" },
+                            { "include_ext_alt_text", "true" },
+                            { "lang", "en" },
+                            { "count", "200" },
+                            { "max_id", "900" },
+                            { "since_id", "100" },
+                        },
+                        "/search/tweets")
+                )
+                .ReturnsAsync(new TwitterSearchResult());
+
+                twitterApi.apiConnection = mock.Object;
+
+                await twitterApi.SearchTweets("from:twitterapi", "en", count: 200, maxId: 900L, sinceId: 100L)
+                    .ConfigureAwait(false);
+
+                mock.VerifyAll();
+            }
+        }
+
+        [Fact]
         public async Task ListsOwnerships_Test()
         {
             using (var twitterApi = new TwitterApi())
index f78e415..af648bb 100644 (file)
@@ -164,6 +164,29 @@ namespace OpenTween.Api
             return this.apiConnection.PostLazyAsync<TwitterStatus>(endpoint, param);
         }
 
+        public Task<TwitterSearchResult> SearchTweets(string query, string lang = null, int? count = null, long? maxId = null, long? sinceId = null)
+        {
+            var endpoint = new Uri("search/tweets.json", UriKind.Relative);
+            var param = new Dictionary<string, string>
+            {
+                ["q"] = query,
+                ["result_type"] = "recent",
+                ["include_entities"] = "true",
+                ["include_ext_alt_text"] = "true",
+            };
+
+            if (lang != null)
+                param["lang"] = lang;
+            if (count != null)
+                param["count"] = count.ToString();
+            if (maxId != null)
+                param["max_id"] = maxId.ToString();
+            if (sinceId != null)
+                param["since_id"] = sinceId.ToString();
+
+            return this.apiConnection.GetAsync<TwitterSearchResult>(endpoint, param, "/search/tweets");
+        }
+
         public Task<TwitterLists> ListsOwnerships(string screenName, long? cursor = null)
         {
             var endpoint = new Uri("lists/ownerships.json", UriKind.Relative);
index d8b5ee5..95c8207 100644 (file)
@@ -138,28 +138,6 @@ namespace OpenTween
                 this.CreateApiCalllback("/favorites/list"));
         }
 
-        public HttpStatusCode Search(string words, string lang, int? count, long? maxId, long? sinceId, ref string content)
-        {
-            Dictionary<string, string> param = new Dictionary<string, string>();
-            if (!string.IsNullOrEmpty(words)) param.Add("q", words);
-            if (!string.IsNullOrEmpty(lang)) param.Add("lang", lang);
-            if (count != null) param.Add("count", count.ToString());
-            if (maxId != null) param.Add("max_id", maxId.ToString());
-            if (sinceId != null) param.Add("since_id", sinceId.ToString());
-
-            if (param.Count == 0) return HttpStatusCode.BadRequest;
-
-            param.Add("result_type", "recent");
-            param.Add("include_entities", "true");
-            param.Add("include_ext_alt_text", "true");
-            return httpCon.GetContent(GetMethod,
-                this.CreateTwitterUri("/1.1/search/tweets.json"),
-                param,
-                ref content,
-                this.CreateRatelimitHeadersDict(),
-                this.CreateApiCalllback("/search/tweets"));
-        }
-
         public HttpStatusCode SavedSearches(ref string content)
         {
             return httpCon.GetContent(GetMethod,
index 1f315be..5947d70 100644 (file)
@@ -2559,7 +2559,7 @@ namespace OpenTween
 
             p.Report("Search refreshing...");
 
-            await Task.Run(() =>
+            await Task.Run(async () =>
             {
                 WebApiException lastException = null;
 
@@ -2570,10 +2570,12 @@ namespace OpenTween
                         if (string.IsNullOrEmpty(tab.SearchWords))
                             continue;
 
-                        this.tw.GetSearch(read, tab, false);
+                        await this.tw.GetSearch(read, tab, false)
+                            .ConfigureAwait(false);
 
                         if (loadMore)
-                            this.tw.GetSearch(read, tab, true);
+                            await this.tw.GetSearch(read, tab, true)
+                                .ConfigureAwait(false);
                     }
                     catch (WebApiException ex)
                     {
index faaf374..84b0be3 100644 (file)
@@ -1116,24 +1116,8 @@ namespace OpenTween
             return minimumId;
         }
 
-        private long? CreatePostsFromSearchJson(string content, TabClass tab, bool read, int count, bool more)
+        private long? CreatePostsFromSearchJson(TwitterSearchResult items, TabClass tab, bool read, int count, bool more)
         {
-            TwitterSearchResult items;
-            try
-            {
-                items = TwitterSearchResult.ParseJson(content);
-            }
-            catch (SerializationException ex)
-            {
-                MyCommon.TraceOut(ex.Message + Environment.NewLine + content);
-                throw new WebApiException("Json Parse Error(DataContractJsonSerializer)", content, ex);
-            }
-            catch (Exception ex)
-            {
-                MyCommon.TraceOut(ex, MethodBase.GetCurrentMethod().Name + " " + content);
-                throw new WebApiException("Invalid Json!", content, ex);
-            }
-
             long? minimumId = null;
 
             foreach (var result in items.Statuses)
@@ -1346,13 +1330,10 @@ namespace OpenTween
                 throw new WebApiException(lastException.Message, lastException);
         }
 
-        public void GetSearch(bool read,
-                            TabClass tab,
-                            bool more)
+        public async Task GetSearch(bool read, TabClass tab, bool more)
         {
-            HttpStatusCode res;
-            var content = "";
             var count = GetApiResultCount(MyCommon.WORKERTYPE.PublicSearch, more, false);
+
             long? maxId = null;
             long? sinceId = null;
             if (more)
@@ -1364,33 +1345,13 @@ namespace OpenTween
                 sinceId = tab.SinceId;
             }
 
-            try
-            {
-                // TODO:一時的に40>100件に 件数変更UI作成の必要あり
-                res = twCon.Search(tab.SearchWords, tab.SearchLang, count, maxId, sinceId, ref content);
-            }
-            catch(Exception ex)
-            {
-                throw new WebApiException("Err:" + ex.Message, ex);
-            }
-            switch (res)
-            {
-                case HttpStatusCode.BadRequest:
-                    throw new WebApiException("Invalid query", content);
-                case HttpStatusCode.NotFound:
-                    throw new WebApiException("Invalid query", content);
-                case HttpStatusCode.PaymentRequired: //API Documentには420と書いてあるが、該当コードがないので402にしてある
-                    throw new WebApiException("Search API Limit?", content);
-                case HttpStatusCode.OK:
-                    break;
-                default:
-                    throw new WebApiException("Err:" + res.ToString() + "(" + MethodBase.GetCurrentMethod().Name + ")", content);
-            }
+            var searchResult = await this.Api.SearchTweets(tab.SearchWords, tab.SearchLang, count, maxId, sinceId)
+                .ConfigureAwait(false);
 
             if (!TabInformations.GetInstance().ContainsTab(tab))
                 return;
 
-            var minimumId =  this.CreatePostsFromSearchJson(content, tab, read, count, more);
+            var minimumId = this.CreatePostsFromSearchJson(searchResult, tab, read, count, more);
 
             if (minimumId != null)
                 tab.OldestId = minimumId.Value;