import dispatch._ import com.google.appengine.api.urlfetch._ import java.net.URL object DispatchHelper { /** * convert dispatch.Request -> String */ implicit def r2s(r: Request) = { r.host.get + r.req.getRequestLine.getUri } /** * convert dispatch.Request -> com.google.appengine.api.urlfetch.HTTPRequest */ implicit def dispatch2gae(r: Request): HTTPRequest = { val isPost = r.req.getMethod == "POST" val g = new HTTPRequest(new URL(r), if (isPost) HTTPMethod.POST else HTTPMethod.GET, FetchOptions.Builder.withDeadline(10)) r.req.getAllHeaders.foreach(h => {g.addHeader(h); println(h)}) r.req match { case p: org.apache.http.client.methods.HttpPost => val payload = new java.io.ByteArrayOutputStream p.getEntity.writeTo(payload) g.setPayload(payload.toByteArray) println(p.getEntity.getContentType) case _ => } g } }
こんな感じでimplicit defを宣言してimport DispatchHelper._すれば、dispatchでリクエストを作って、URLFetchService.fetch()の引数にそのまま渡せばOK。
dispatchの解説はこちら
0 件のコメント:
コメントを投稿