我的网络客户端(用cljs
编写)连接到需要进行一些第三方API调用的后端(用clj
编写)。它必须在服务器上完成,然后结果应以特定方式转换并发送回客户端。
这是我的其中一个网址的处理程序
(defn get-orders [req]
(let [{:keys [sig uri]} (api-signature :get-orders)]
(client/get uri
{:async? true}
(fn [response] {:body "something"})
(fn [exception] {:body "error"}))))
而不是返回{:body "something"}
,它返回以下错误:
No implementation of method: :render of protocol: #'compojure.response/Renderable found for class: org.apache.http.impl.nio.client.FutureWrapper
我做错了什么?
答案 0 :(得分:4)
当您指定{:async? true}
时,clj-http.client/get
将在您收到的错误消息中返回FutureWrapper
(defn handler [request]
(response {:result (client/get "http://example.com")}))
。
因此,如果您不需要异步,请不要使用它。这是一个同步环处理程序的示例,它调用第三方URL并返回返回的响应。
(defn handler [request respond raise]
(client/get "http://example.com"
{:async? true}
(fn [response] (respond {:body "something"}))
(fn [exception] (raise {:body "error"}))))
如果您确实需要异步,请使用环路处理程序的异步版本。
:async?
不要忘记配置webserver适配器以使用异步处理程序。例如,对于Jetty,将true
标志设置为(jetty/run-jetty app {:port 4000 :async? true :join? false})
,如此
(defn handler [request]
(let [result1 (promise)
result2 (promise)]
(client/get "http://example.com/"
{:async? true}
(fn [response] (deliver result1 {:success true :body "something"}))
(fn [exception] (deliver result1 {:success false :body "error"})))
(client/get "http://example.com/"
{:async? true}
(fn [response] (deliver result2 {:success true :body "something"}))
(fn [exception] (deliver result2 {:success false :body "error"})))
(cond
(and (:success @result1)
(:success @result2))
(response {:result1 (:body @result1)
:result2 (:body @result2)})
(not (:success @result1))
(throw (ex-info "fail1" (:body @result1)))
(not (:success @result2))
(throw (ex-info "fail2" (:body @result2))))))
如果您要同时拨打多个第三方网址并返回网络客户端,请使用a future帮助
@override
onBackPressed(){
clearBackTrace(intent);
}