ClojureScript: parsing a Transit response - clojure

I started learning ClojureScript this week and I stuck parsing a Transit response, I have this function:
(defn handler [response]
(let [comment (:comment response)
created_at (:created_at response)
last_name (:last_name response)
_ (.log js/console (str ">>> COMMENT >>>>> " comment))
comments_div (.getElementById js/document "comments")]
(.append comments_div comment)
(.log js/console (str "Handler response: " response))))
And the console shows:
So, "response" looks fine but I can't get the content from the "response" map (I think is a map) using:
comment (:comment response) or comment (get response :comment)
The headers say that the response is an "application/transit+json" kind. I tried:
(ns blog.core
(:require [cognitect.transit :as t]))
(def r (t/reader :json))
let [parsed (t/read r response).... <--- inside the let block
but no luck so far. Need I to parse the var "response"?

Since it is not working like a map, it probably is a string. Try checking the type of response. with
(println (type response))
If it is a string then :
(ns example
(:require [clojure.data.json :as json]))
(console.log ((json/read-str response) :comment))

This works fine:
(ns blog.core
(:require [domina :as dom]
[ajax.core :refer [GET POST DELETE]]
[cognitect.transit :as t]
[bide.core :as r]))
(def r (t/reader :json))
(defn handler [response]
(let [parsed (t/read r response)
_ (.log js/console (str ">>> PARSED >>>>> " (type parsed) ">>>>" parsed))
comment (get parsed "comment")
.... rest of the code...

Related

Can't extract multipart-params from POST request in compojure

When I try to extract the multipart-params from a POST request like this:
(defroutes upload-routes
(POST "/upload" {params :params} (println params))
I got {}.
Then I tried like this:
(defroutes upload-routes
(POST "/upload" {multipart-params :multipart-params} (println multipart-params))
I still got {}.
I guess there are something wrong about my middleware.
So I tried to change the handler, here are the handlers I had tried:
(ns cloudserver.handler
(:require [compojure.core :refer [defroutes routes]]
[compojure.route :as route]
[compojure.handler :as handler]
[cloudserver.routes.home :refer [home-routes]]
[noir.util.middleware :as noir-middleware]
[cloudserver.routes.auth :refer [auth-routes]]
[cloudserver.routes.upload :refer [upload-routes]]
[cloudserver.routes.search :refer [search-routes]]
[cloudserver.routes.download :refer [download-routes]]
[ring.middleware.defaults :refer [api-defaults wrap-defaults site-defaults]]
[ring.middleware.multipart-params :refer [wrap-multipart-params]]
[ring.middleware.params :refer [wrap-params]]
[noir.session :as session]
[ring.middleware.session.memory :refer [memory-store]]))
(def app
(->
(routes auth-routes
home-routes
upload-routes
search-routes
download-routes
app-routes)
session/wrap-noir-session
(wrap-defaults(assoc-in site-defaults [:security :anti-forgery] false)
wrap-multipart-params
wrap-params))
(def app
(->
(routes auth-routes
home-routes
upload-routes
search-routes
download-routes
app-routes)
session/wrap-noir-session
(wrap-defaults(assoc-in site-defaults [:security :anti-forgery] false)
wrap-multipart-params))
(def app
(->
(routes auth-routes
home-routes
upload-routes
search-routes
download-routes
app-routes)
session/wrap-noir-session
(wrap-defaults (-> site-defaults
(assoc-in [:security :anti-forgery] false)
(assoc-in [:params :multipart] true)
(assoc-in [:params :nested] true)))
handler/site))
(def app
(->
(routes auth-routes
home-routes
upload-routes
search-routes
download-routes
app-routes)
wrap-multipart-params
session/wrap-noir-session
(wrap-defaults(assoc-in site-defaults [:security :anti-forgery] false)))
(def app
(noir-middleware/app-handler
[auth-routes
home-routes
upload-routes
search-routes
download-routes
app-routes]
:ring-defaults (assoc site-defaults :security nil)))
But the only result I got is {}
My client code is:
public int upload (String filename, String[] tags, String time, String fingerprint) throws IOException {
String url = host + "/upload";
CloseableHttpClient httpClient = HttpClients.custom().setDefaultCookieStore(cookieStore).build();
HttpPost httpPost = new HttpPost(url);
MultipartEntityBuilder mulentity = MultipartEntityBuilder.create();
mulentity.addBinaryBody("photo", new File(filename));
for (int i = 0; i < tags.length; i ++) {
mulentity.addTextBody("tag" + i, tags[i]);
}
mulentity.addTextBody("fingerprint", fingerprint);
mulentity.addTextBody("time", time);
mulentity.addTextBody("filename", filename.substring(filename.lastIndexOf(File.separatorChar) + 1, filename.length()));
HttpEntity entity = mulentity.build();
httpPost.setEntity(entity);
httpPost.setHeader("Content-Type", "multipart/form-data;boundary=" + BOUNDARY);
int status = 3;
try {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String response = httpClient.execute(httpPost, responseHandler);
status = Integer.parseInt(response);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} finally {
httpClient.close();
}
return status;
}
I am really a green hand in clojure web programming. Thanks a lot!
Problem sovled. It's because the boundary in the request is wrong.
I too am "Clojure Green", but in my multipart Ring + Compojure API app, I just destructure the request on the names of the parameters that come in on the multipart request. The one that is actually a temp file gets output by the printf below has a string representation like "#object[java.io.File 0x4blahblahblah". The middleware took care of lifting it out of the request map and making the parameters available by just their names, I think.
(defroutes upload-routes
(POST "/upload" [photo tag fingerprint time filename :as request]
(printf
"photo %s, tag %s, fingerprint %s, time %s, filename %s"
photo tag fingerprint time filename)))
The :as request isn't really needed if you're not going to do anything with the whole request map. If all you are doing is printf...maybe include it in the printf and you'll get to see a little how the sausage gets made in Ring. Its Maps all the way down.

How do I mock a PUT request using ring.mock.request

How do I make this test pass:
(ns imp-rest.parser-test-rest
(:require [clojure.test :refer :all])
(:require [ring.mock.request :as mock] )
(:require [imp-rest.web :as w]))
(deftest test-parser-rest
(testing "put settings"
(w/app
(mock/request :put "/settings/coordinateName" "FOO" ))
(let [response (w/app (mock/request :get "/settings"))]
(println response )
(is (= (get (:body response) :coordinateName) "FOO")))))
it fails with:
FAIL in (test-parser-rest) (parser_test_rest.clj:30)
put settings
expected: (= (get (:body response) :coordinateName) "FOO")
actual: (not (= nil "FOO"))
Here's my handler:
(ns imp-rest.web
(:use compojure.core)
(:use ring.middleware.json-params)
(:require [clj-json.core :as json])
(:require [ring.util.response :as response])
(:require [compojure.route :as route])
(:require [imp-rest.settings :as s]))
(defn json-response [data & [status]]
{:status (or status 200)
:headers {"Content-Type" "application/json"}
:body (json/generate-string data)})
(defroutes handler
(GET "/settings" []
(json-response (s/get-settings)))
(GET "/settings/:id" [id]
(json-response (s/get-setting id)))
(PUT "/settings" [id value]
(json-response (s/put-setting id value)))
(route/not-found "Page not found") )
(def app
(-> handler
wrap-json-params))
which exposes this map (of settings):
(ns imp-rest.settings)
(def settings
(atom
{:coordinateName nil
:burnin nil
:nslices nil
:mrsd nil
}))
(defn get-settings []
#settings)
(defn get-setting [id]
(#settings (keyword id)))
(defn put-setting [id value]
(swap! settings assoc (keyword id) value)
value)
and the entry point:
(ns imp-rest.core
(:use ring.adapter.jetty)
(:require [imp-rest.web :as web]))
(defn -main
"Entry point"
[& args]
(do
(run-jetty #'web/app {:port 8080})
);END;do
);END: main
Now when I 'lein run' I can make a (working) request like this:
curl -X PUT -H "Content-Type: application/json" \
-d '{"id" : "coordinateName", "value" : "FOO"}' \
http://localhost:8080/settings
which is what I try to mock with the test. Any help appreciated.
If you want to have :id in your PUT /settings/:id route accepting body in format {"value": "..."}, you need to change your routes definition:
(defroutes handler
(GET "/settings" []
(json-response (s/get-settings)))
(GET "/settings/:id" [id]
(json-response (s/get-setting id)))
(PUT "/settings/:id" [id value]
(json-response (s/put-setting id value)))
(route/not-found "Page not found"))
And change how you call your PUT endpoint in the test:
(w/app
(-> (mock/request
:put
"/settings/coordinateName"
(json/generate-string {:value "FOO"}))
(mock/content-type "application/json")))
What was changed?
:id in your PUT URL route definition (/settings -> /settings/:id)
Your PUT request didn't send a correct request and content type.
If you want to have a PUT /settings route expecting {"id": "...", "value": "..."} request body, then you need to change how you create a mock request:
(w/app
(-> (mock/request
:put
"/settings"
(json/generate-string {:id "coordinateName" :value "FOO"}))
(mock/content-type "application/json"))
Your curl request specifies the parameters as JSON in the body of the PUT request, but your mock request tries to use URL parameters.
There are two options to resolve this:
compojure can automatically translate parameters, but only when the relevant middleware is present -- you have wrap-json-params added to your handler, but you're missing wrap-params. The answer from Piotrek Bzdyl amounts to making these params explicit in the compojure routes.
Alternatively, you can add the ID/value pair as JSON in the body of the mock request using request.mock.body.

with-redefs doesn't redefine my function

I have a test:
(ns gui-proxy.handler-test
(:require [clojure.test :refer :all]
[ring.mock.request :as mock]
[gui-proxy.handler :as handler]))
(deftest test-app
(testing "not-found route"
(with-redefs-fn [handler/log-request (fn [type url] (str ""))]
(let [response (handler/app (mock/request :get "/invalid"))]
(is (= (:status response) 404))))))
and the code that are under test:
(ns gui-proxy.handler
(:require [compojure.core :refer :all]
[compojure.route :as route]
[ring.middleware.defaults :refer [wrap-defaults site-defaults]]
[clj-http.client :as client]
[gui-proxy.db :as db]))
(defn log-request [type url]
(db/insert-request-info type url))
(defn log-error []
(log-request ":fail" "fail"))
"gui-proxy - File not found")
(defroutes app-routes
(route/not-found (log-error)))
So, basically i'd like to stop the call to the database-namespace, but i end upp in a fat database exeception stacktrace...
What is wrong?
with-redefs-fn takes a map of bindings, not a vector. Note that the examples at clojuredocs use the #' reader macro to refer to the Var, so summing up you could try
(deftest test-app
(testing "not-found route"
(with-redefs-fn {#'handler/log-request (fn [type url] (str ""))}
(let [response (handler/app (mock/request :get "/invalid"))]
(is (= (:status response) 404))))))
Take a look at with-redefs it should match your usage.
http://clojuredocs.org/clojure.core/with-redefs

How to set Content-Type header on Ring-Compojure application

I'm trying to get started with Clojure and Clojurescript by implementing a simple web app. Things are going pretty good so far and reading from different tutorials I've come up with the code below:
core.clj:
(ns myapp.core
(:require [compojure.core :as compojure]
[compojure.handler :as handler]
[compojure.route :as route]
[myapp.controller :as controller]))
(compojure/defroutes app-routes
(compojure/GET "/" [] controller/index)
(route/resources "/public")
(route/not-found "Not Found"))
(def app
(handler/site app-routes))
controller.clj:
(ns myapp.controller
(:use ring.util.response)
(:require [myapp.models :as model]
[myapp.templates :as template]))
(defn index
"Index page handler"
[req]
(->> (template/home-page (model/get-things)) response))
templates.clj:
(ns myapp.templates
(:use net.cgrand.enlive-html)
(:require [myapp.models :as model]))
(deftemplate home-page "index.html" [things]
[:li] (clone-for [thing things] (do->
(set-attr 'data-id (:id thing))
(content (:name thing)))))
The problem is I can't display non-ascii characters on the page and I don't know how to set HTTP headers on a page.
I see solutions like this but I simply can't figure out where place them in my code:
(defn app [request]
{:status 200
:headers {"Content-Type" "text/plain"}
:body "Hello World"})
P.S: Any suggestions about style and/or code organization are welcome.
Use ring.util.response:
(require '[ring.util.response :as r])
Then on your index function:
(defn index
"Index page handler"
[req]
(-> (r/response (->> (template/home-page (model/get-things)) response))
(r/header "Content-Type" "text/html; charset=utf-8")))
You can chain other actions on the response such as set-cookie and whatnot:
(defn index
"Index page handler"
[req]
(-> (r/response (->> (template/home-page (model/get-things)) response))
(r/header "Content-Type" "text/html; charset=utf-8")
(r/set-cookie "your-cookie-name"
"" {:max-age 1
:path "/"})))

Clojure: Template with Enlive

I copied a very basic sample from https://github.com/cgrand/enlive, but it doesn't compile:
(ns web.handler
(:require
[compojure.core :refer :all]
[compojure.handler]
[compojure.route :as route]
[net.cgrand.enlive-html :as html]))
;; Compiler throws in the next line, see the message below.
(html/deftemplate main-template "templates/index.html"
[]
[:head :title] (html/content "Enlive starter kit"))
(defroutes app-routes
(GET "/" [] "Hello")
(GET "/ping/:what" [what] (str "<h1>Ping '" what "'</h1>"))
(route/resources "/")
(route/not-found "Not Found"))
(def app
(compojure.handler/site app-routes))
Error I get:
java.lang.NullPointerException, compiling:(handler.clj:9:1)
I run with command:
lein ring server-headless
How to make it work?
EDIT
My investigation so far: error throw from enlive-html.clj:54:
(defn tagsoup-parser
"Loads and parse an HTML resource and closes the stream."
[stream]
(filter map?
(with-open [^java.io.Closeable stream stream]
(xml/parse (org.xml.sax.InputSource. stream) startparse-tagsoup)))) ; #54
Probably org.xml.sax is not referenced? How can I do this with lein?
That error normally happens when the template file is not found. With templates/index.html, it is looking in the resources/templates directory or in the src/templates directory.