OSDN Git Service

Merge pull request #307 from opentween/detect-rate-limits
[opentween/open-tween.git] / OpenTween / Api / GraphQL / ErrorResponse.cs
index 4f778a0..2a8e6d4 100644 (file)
@@ -30,6 +30,7 @@ using System.Threading.Tasks;
 using System.Xml;
 using System.Xml.Linq;
 using System.Xml.XPath;
+using OpenTween.Api.DataModel;
 
 namespace OpenTween.Api.GraphQL
 {
@@ -63,5 +64,25 @@ namespace OpenTween.Api.GraphQL
 
             throw new WebApiException(messageText, responseJson);
         }
+
+        public static void ThrowIfContainsRateLimitMessage(XElement rootElm)
+        {
+            var messageElm = rootElm.XPathSelectElement("//itemContent[itemType[text()='TimelineMessagePrompt']]");
+            if (messageElm == null)
+                return;
+
+            var bodyText = messageElm.XPathSelectElement("content/bodyText")?.Value ?? "";
+            if (bodyText.StartsWith("You have reached the limit"))
+            {
+                var error = new TwitterError
+                {
+                    Errors = new[]
+                    {
+                        new TwitterErrorItem { Code = TwitterErrorCode.RateLimit, Message = "" },
+                    },
+                };
+                throw new TwitterApiException(0, error, "");
+            }
+        }
     }
 }