Skip to content

Commit

Permalink
implement edit backoff #47
Browse files Browse the repository at this point in the history
  • Loading branch information
intracer committed Oct 26, 2017
1 parent 73a5c4c commit f39b718
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
6 changes: 4 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ lazy val commonSettings = Seq(
"Typesafe Repo" at "http://repo.typesafe.com/typesafe/releases/",
"Scalaz Bintray Repo" at "http://dl.bintray.com/scalaz/releases",
Resolver.jcenterRepo,
Resolver.bintrayRepo("rick-beton", "maven")
Resolver.bintrayRepo("rick-beton", "maven"),
Resolver.bintrayRepo("softprops", "maven")
),
scalacOptions ++= Seq("-Ybackend:GenBCode"),

Expand Down Expand Up @@ -61,7 +62,8 @@ lazy val `scalawiki-core` =
"ch.qos.logback" % "logback-classic" % "1.1.3",
"org.sweble.wikitext" % "swc-engine" % "2.0.0" exclude("org.jsoup", "jsoup"),
"commons-codec" % "commons-codec" % "1.10",
"org.jsoup" % "jsoup" % "1.8.3"
"org.jsoup" % "jsoup" % "1.8.3",
"me.lessis" %% "retry" % "0.2.0"
)
}).dependsOn(`http-extensions`)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@ package org.scalawiki.query
import java.nio.file.{Files, Paths}

import org.scalawiki.MwBot
import org.scalawiki.dto.{Namespace, Page}
import org.scalawiki.dto.cmd._
import org.scalawiki.dto.cmd.edit._
import org.scalawiki.dto.cmd.query._
import org.scalawiki.dto.cmd.query.list._
import org.scalawiki.dto.cmd.query.prop._
import org.scalawiki.dto.cmd.query.prop.rvprop.RvProp
import org.scalawiki.dto.{Namespace, Page}
import org.scalawiki.json.MwReads._
import retry.Success

import scala.concurrent.Future

Expand Down Expand Up @@ -124,10 +125,16 @@ class PageQueryImplDsl(query: Either[Set[Long], Set[String]],

bot.log.info(s"${bot.host} edit page: $page, summary: $summary")

if (multi)
bot.postMultiPart(editResponseReads, params)
else
bot.post(editResponseReads, params)
def performEdit(): Future[String] = {
if (multi)
bot.postMultiPart(editResponseReads, params)
else
bot.post(editResponseReads, params)
}

import scala.concurrent.ExecutionContext.Implicits.global
implicit def stringSuccess: Success[String] = Success(_ == "Success")
retry.Backoff()(odelay.Timer.default)(() => performEdit())
}

override def upload(filename: String,
Expand Down
20 changes: 18 additions & 2 deletions scalawiki-core/src/test/scala/org/scalawiki/EditSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,31 @@ class EditSpec extends Specification with MockBotSpec {

val tokenResponse = """{"batchcomplete":"","query":{"tokens":{"csrftoken":"cafebabe+\\"}}}"""
val successResponse = """{"edit":{"result":"Success","pageid":1776370,"title":"pageTitle","contentmodel":"wikitext"}}"""
val errorResponse = """{"edit":{"result":"Error","pageid":1776370,"title":"pageTitle","contentmodel":"wikitext"}}"""

"bot" should {
"edit successfully" in {
val siteInfo = TestUtils.resourceAsString("/org/scalawiki/ukwiki_siteinfo.json")
val bot: MwBot = getBot(
HttpStub(Map("action" -> "query", "meta" -> "siteinfo", "format" -> "json"), siteInfo),
HttpStub(Map("action" -> "query", "meta" -> "tokens", "format" -> "json"), tokenResponse),
HttpStub(Map("assert" -> "bot", "format" -> "json", "text" -> "pageText", "token"-> "cafebabe+\\", "bot" -> "x",
"title" -> "pageTitle", "action" -> "edit", "summary" -> "editsummary"), successResponse)
HttpStub(Map("assert" -> "bot", "format" -> "json", "text" -> "pageText", "token" -> "cafebabe+\\", "bot" -> "x",
"title" -> "pageTitle", "action" -> "edit", "summary" -> "editsummary"), successResponse)
)

val result = bot.page("pageTitle").edit("pageText", Some("editsummary")).await
result === "Success"
}

"edit retry error once" in {
val siteInfo = TestUtils.resourceAsString("/org/scalawiki/ukwiki_siteinfo.json")
val bot: MwBot = getBot(
HttpStub(Map("action" -> "query", "meta" -> "siteinfo", "format" -> "json"), siteInfo),
HttpStub(Map("action" -> "query", "meta" -> "tokens", "format" -> "json"), tokenResponse),
HttpStub(Map("assert" -> "bot", "format" -> "json", "text" -> "pageText", "token"-> "cafebabe+\\", "bot" -> "x",
"title" -> "pageTitle", "action" -> "edit", "summary" -> "editsummary"), errorResponse),
HttpStub(Map("assert" -> "bot", "format" -> "json", "text" -> "pageText", "token"-> "cafebabe+\\", "bot" -> "x",
"title" -> "pageTitle", "action" -> "edit", "summary" -> "editsummary"), successResponse)
)

val result = bot.page("pageTitle").edit("pageText", Some("editsummary")).await
Expand Down

0 comments on commit f39b718

Please sign in to comment.