GROWIのAPIの実行用のScalaのプログラム例のサンプル。URLとAPI Tokenはダミーの値を設定している。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package info.pandanote.scalajhttptest | |
// See https://pandanote.info/?p=6186 for details. | |
import java.net.{ URLEncoder, URLDecoder } | |
import scalaj.http._ | |
import play.api.libs.json._; | |
case class CustomResponse(statusCode: Int, status: String, body: String) | |
class ScalajHttpClientSample { | |
val readTimeoutMillis = 5000 | |
var connectTimeoutMillis = 5000 | |
def Get(url: String, headers: Map[String, String]): CustomResponse = httpRequest("GET", url, headers) | |
def Post(url: String, headers: Map[String, String], requestBody: String): CustomResponse = httpRequest("POST", url, headers, requestBody) | |
def Put(url: String, headers: Map[String, String], requestBody: String): CustomResponse = httpRequest("PUT", url, headers, requestBody) | |
def Delete(url: String, headers: Map[String, String]): CustomResponse = httpRequest("DELETE", url, headers) | |
def httpRequest(webMethod: String, url: String, headers: Map[String, String], requestBody: String = ""): CustomResponse = { | |
var request = Http(url) | |
.headers(headers) | |
.timeout(connTimeoutMs = connectTimeoutMillis, readTimeoutMs = readTimeoutMillis) | |
if (requestBody != "") { | |
request = request.postData(requestBody) | |
} | |
request = request.method(webMethod) | |
val response: HttpResponse[String] = request.execute() | |
return extractResponse(response) | |
} | |
def extractResponse(res: HttpResponse[String]): CustomResponse = { | |
val statusCode = res.code | |
val httpStatus = statusCode match { | |
case 200 => "OK" | |
case 201 => "CREATED" | |
case 204 => "NO_CONTENT" | |
case 401 => "UNAUTHORIZED" | |
case 400 => "BAD_REQUEST" | |
case 404 => "NOT_FOUND" | |
case 403 => "FORBIDDEN" | |
case 500 => "INTERNAL_SERVER_ERROR" | |
case _ => "OTHER_STATUS_CODE" | |
} | |
val body = res.body | |
return CustomResponse(statusCode, httpStatus, body) | |
} | |
} | |
object Main { | |
def siteurl = "https://wiki.example.org" // <-- Change here. | |
def main(args: Array[String]) = { | |
val client: ScalajHttpClientSample = new ScalajHttpClientSample | |
val token = "Token" // <-- Change here. | |
val r = client.Get(siteurl+"/_api/pages.get?access_token="+URLEncoder.encode(token,"UTF-8")+"&path=/"+URLEncoder.encode("テストページ","UTF-8"), Map.empty[String,String]) | |
val rr = r.body | |
val r3: JsValue = Json.parse(rr) | |
val page_id = r3 \ "page" \ "_id" | |
println(page_id) | |
val revision_id = r3 \ "page" \ "revision" \ "_id" | |
println(revision_id) | |
val body = r3 \ "page" \ "revision" \ "body" | |
println(body) | |
val param: Map[String,String] = Map("access_token" -> token, "body" -> (body.as[String]+"\n$y=y_0\\,\\displaystyle\\frac{e^{t-t_0}}{2}$\n"), "page_id" -> page_id.as[String], "revision_id" -> revision_id.as[String]) | |
val message = Json.toJson(param) | |
val p = client.Post(siteurl+"/_api/pages.update",Map("Content-Type" -> "application/json"),Json.stringify(message)) | |
println(p) | |
println(message) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment