Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
6
Help us understand the problem. What is going on with this article?

More than 5 years have passed since last update.

@kysnm

Play 2.0 / Scala の WS で同期リクエスト

play.api.libs.concurrent.Promise を使うと良いみたい。

参考:
http://stackoverflow.com/questions/9260225/play2-mini-and-akka2-for-http-gateway

Qiita の API だとこんな感じになる。

package models

import play.api.libs.ws.WS.WSRequestHolder
import play.api.libs.ws.WS.url
import play.api.libs.concurrent.Promise
import play.api.libs.ws.Response

object Qiita {
  def getTags = {
    val wsRequestHolder: WSRequestHolder = url("https://qiita.com/api/v1/tags")
    val promiseResponse: Promise[Response] = wsRequestHolder.get()
    val response = promiseResponse.await.get

    println("HTTP status code: " + response.status)
    println("HTTP body: " + response.body)
  }

}

console から実行するとこんな感じ

$ play console
[info] Loading project definition from /private/tmp/todolist/project[info] Set current project to todolist (in build file:/private/tmp/todolist/)[info] Compiling 1 Scala source to /private/tmp/todolist/target/scala-2.9.1/classes...[info] Starting scala interpreter...[info] 
Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_35).
Type in expressions to have them evaluated.
Type :help for more information.

scala> new play.core.StaticApplication(new java.io.File("."))
[info] play - database [default] connected at jdbc:postgresql://localhost:5432/todolist
[info] play - Application started (Prod)
res0: play.core.StaticApplication = play.core.StaticApplication@4b9e2380

scala> models.Qiita.getTags
HTTP status code: 200
HTTP body: [{"name":"Qiita","url_name":"Qiita","icon_url":"http://qiita.com/system/tags/icons/000/000/001/thumb/favicon.png?1320171109","follower_count":2416,"item_count":189},{"name":"Java","url_name":"Java","icon_url":"http://qiita.com/system/tags/icons/000/000/002/thumb/Java-Runtime-Environment-V6.0.260.png?1316130938","follower_count":2382,"item_count":183},{"name":"Ruby","url_name":"Ruby","icon_url":"http://qiita.com/system/tags/icons/000/000/003/thumb/ruby.jpeg?1316130908","follower_count":3222,"item_count":920},{"name":"Python","url_name":"Python","icon_url":"http://qiita.com/system/tags/icons/000/000/004/thumb/search.gif?1316130880","follower_count":1840,"item_count":238},{"name":"Perl","url_name":"Perl","icon_url":"http://qiita.com/system/tags/icons/000/000/005/thumb/imgres.jpeg?1316130753","follower_count":1085,"item_count":98},{"name":"PHP","url_name":"PHP","icon_url":"http://qiita.com/system/tags/icons/000/000/006/thumb/PHP.png?1338128905","follower_count":3043,"item_count":492},{"name":"Emacs","url_name":"Emacs","icon_url":"http://qiita.com/system/tags/icons/000/000/007/thumb/emacs.jpeg?1316130821","follower_count":1376,"item_count":195},{"name":"Vim","url_name":"Vim","icon_url":"http://qiita.com/system/tags/icons/000/000/008/thumb/vim-editor_logo.png?1316130850","follower_count":2664,"item_count":219},{"name":"ShellScript","url_name":"ShellScript","icon_url":"http://qiita.com/system/tags/icons/000/000/009/thumb/shellscript.png?1316130734","follower_count":1526,"item_count":127},{"name":"C","url_name":"C","icon_url":"http://qiita.com/system/tags/icons/000/000/010/thumb/C.png?1318001428","follower_count":1316,"item_count":64},{"name":"C++","url_name":"C%2B%2B","icon_url":"http://qiita.com/system/tags/icons/000/000/011/thumb/203px-Gccegg.png?1328941397","follower_count":1355,"item_count":101},{"name":"Objective-C","url_name":"Objective-C","icon_url":"http://qiita.com/system/tags/icons/000/000/012/thumb/objc.png?1316167122","follower_count":2360,"item_count":320},{"name":"HTML","url_name":"HTML","icon_url":"http://qiita.com/system/tags/icons/000/000/013/thumb/110126-HTML5_Logo.png?1316130376","follower_count":4126,"item_count":100},{"name":"CSS","url_name":"CSS","icon_url":"http://qiita.com/system/tags/icons/000/000/014/thumb/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Nuvola_mimetypes_source_css.png?1316130407","follower_count":3654,"item_count":140},{"name":"JavaScript","url_name":"JavaScript","icon_url":"http://qiita.com/system/tags/icons/000/000/015/thumb/images.jpeg?1316130454","follower_count":5002,"item_count":668},{"name":"CoffeeScript","url_name":"CoffeeScript","icon_url":"http://qiita.com/system/tags/icons/000/000/016/thumb/coffeescript_logo.png?1316130584","follower_count":1642,"item_count":74},{"name":"Haskell","url_name":"Haskell","icon_url":"http://qiita.com/system/tags/icons/000/000/017/thumb/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Haskell-Logo.png?1316130328","follower_count":764,"item_count":137},{"name":"Scheme","url_name":"Scheme","icon_url":"http://qiita.com/system/tags/icons/000/000/018/thumb/%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB:Lambda_lc.png?1316130303","follower_count":493,"item_count":46},{"name":"iPhone","url_name":"iPhone","icon_url":"http://qiita.com/system/tags/icons/000/000/019/thumb/k1014925770.jpeg?1316130246","follower_count":3252,"item_count":176},{"name":"Android","url_name":"Android","icon_url":"http://qiita.com/system/tags/icons/000/000/020/thumb/android_logo.gif?1316130200","follower_count":2573,"item_count":222}]

scala> 

6
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
6
Help us understand the problem. What is going on with this article?