]> gitweb.factorcode.org Git - factor.git/commitdiff
mediawiki.api: Refresh invalid tokens, refactor
authorGiftpflanze <gifti@tools.wmflabs.org>
Wed, 17 Aug 2022 04:42:02 +0000 (04:42 +0000)
committerGiftpflanze <gifti@tools.wmflabs.org>
Mon, 9 Jan 2023 18:51:26 +0000 (18:51 +0000)
extra/mediawiki/api/api.factor

index 8ad8e3d92a43956c4293625bacd9b6bcf5541d9f..788aa7d504c6b7a7866bd0e64a00a40e5b691997 100644 (file)
@@ -1,9 +1,10 @@
-! Copyright (C) 2021 Giftpflanze.
+! Copyright (C) 2021, 2022 Giftpflanze.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: arrays accessors assocs assocs.extras calendar
-combinators continuations formatting http http.client io
-json.reader kernel locals make math math.parser namespaces
-oauth1 prettyprint sequences strings system threads ;
+combinators combinators.short-circuit continuations formatting
+http http.client io json.reader kernel locals make math
+math.parser namespaces oauth1 prettyprint sequences strings
+system threads ;
 IN: mediawiki.api
 
 TUPLE: oauth-login consumer-token consumer-secret access-token
@@ -72,7 +73,7 @@ t botflag set-global
         { "meta" "tokens" }
         { "type" "login" }
     } cookie-post*
-    "query" "tokens" "logintoken" [ of ] tri@ ;
+    { "query" "tokens" "logintoken" } deep-of ;
 
 : login ( -- cookies )
     [
@@ -104,18 +105,26 @@ t botflag set-global
 : nonce-already-used? ( assoc -- ? )
     "error" of
     [ "code" of "mwoauth-invalid-authorization" = ]
-    [ "info" of "Nonce already used" subseq-index ] bi
+    [ "info" of "Nonce already used" subseq-of? ] bi
     and ;
 
 : readonly? ( assoc -- ? )
-    "error" "code" [ of ] bi@ "readonly" = dup
+    { "error" "code" } deep-of "readonly" = dup
     [ 5 minutes sleep ] when ;
 
+DEFER: get-csrf-token
+: badtoken? ( assoc -- ? )
+    { "error" "code" } deep-of "badtoken" = dup
+    [ get-csrf-token drop ] when ;
+
 : failed? ( response assoc -- response assoc ? )
-    2dup 2dup code-200? not
-    rot retry-after? or
-    over nonce-already-used? or
-    swap readonly? or ;
+    2dup {
+        [ code-200? not ]
+        [ drop retry-after? ]
+        [ nip nonce-already-used? ]
+        [ nip readonly? ]
+        [ nip badtoken? ]
+    } 2|| ;
 
 : dispatch-call ( params -- response data )
     {
@@ -172,7 +181,7 @@ quot2: ( ... -- ... ) -- seq )
     [
         "query" of "pages" "revisions" [ of first ] bi@
         [ "timestamp" of basetimestamp set-global ]
-        [ "slots" "main" "content" [ of ] tri@ ] bi
+        [ { "slots" "main" "content" } deep-of ] bi
     ] bi ;
 
 <PRIVATE