I am trying to implement an amazon aws binding with Clojure, so that I can search for a book by sending an isbn and get an xml report of it. I am then trying to retrieve the salesrank and book title from the report. Here is what I am trying to use: https://github.com/FreeAgent/clj-amazon
Instead of using REPL, I made a core2.clj inside the src folder and added this code into it:
(ns clj_amazon.core2
(:use clj-amazon.core)
(:use clj-amazon.product-advertising)
(:gen-class))
(defn -main [& args]
(def ACCESS-KEY "my access code")
(def SECRET-KEY "my secret key" )
(def ASSOCIATE-ID "my id")
(def gibson-opus-search (with-signer (ACCESS-KEY, SECRET-KEY) (item-search :search-index "Books", :keywords "Neuromancer", :associate-tag ASSOCIATE-ID, :condition "New")))
(gibson-opus-search)
)
If I remove
(gibson-opus-search)
and run "lein run" in the command line, I get no errors. I have the correct access key/secret key, and it seems that the code is working fine. But I also get no report printed. I'm new to Clojure, so I'm not understanding what I should do next. I tried to call the function with
(gibson-opus-search)
, but then i get this error: wrong number of args(0) passed to persistentarraymap. How can I fix this?
You are defining gibson-opus-search with def which is for for assigning values, not defining functions. You need to define it with defn then call it with the right number of arguments.
Related
I have have the following project structure:
--fourth (my project folder)
---core
----core.clj
----mycore.clj
core looks like this:
(ns core.core)
(require '[core.mycore :refer [myhello2]] :verbose)
(println "hi")
mycore looks like this:
(ns core.mycore)
(defn myhello2 []
(println "hi")
)
If i evaluate core.clj, i get the following error: Could not locate mycore__init.class, mycore.clj or mycore.cljc on classpath.
If i do the same and i just switch the "core" directory to "src", it works. What can i do, to make it work?
I tried to put
{:depths ["core"]} in deps.edn, but nothing changed.
But my deps.edn is empty: {}
Per https://clojure.org/guides/tools_build, you want to use :path, not :depths (which I've never heard of / don't know to be valid at all) to specify the locations in your source tree from which to search for items.
{
:paths ["."]
}
...means that core.mycore will be looked for in ./core/mycore.clj instead of ./src/core/mycore.clj (as is the case with the default :paths ["src"]).
So I use re-graph version 0.1.11 and I try fetching data from the endpoint. After fetching data, I checked network tab in the browser and I found the expected data after that it should activate my callback function, but it doesn't seem to work (but sometimes it works and after refreshing the page a few times it doesn't work again). Here is the code.
;; how I init re-graph
(rf/dispatch [::re-graph/init
::conf/graphql-client-name
{:ws-url url
:http-url url
:ws-reconnect-timeout 500
:resume-subscriptions? true}])
(re-frame.core/reg-event-fx
::fetch-expected-data
(fn [cofx event]
(let [app-db (:db cofx)
some-params (-> event second (cljs.core/js->clj :keywordize-keys true))
token (-> app-db (lens/get-in (auth-db/lens-token :group-level-x)))]
(re-frame.core/dispatch
[:re-graph.core/query
::conf/graphql-client-name
"query findExpectedData($query: FetchExpectedDataInput!, $token: String!) {
findExpectedData(query: $query, token: $token){
value1
value2
...
}
}"
{:query some-params
:token token}
;; this is where the problem occurs
;; even though i found the data in the network tab, but
;; this callback doesn't seem to work (sometimes it works sometimes it doens't)
[::fetched-data-completed]]))))
(re-frame.core/reg-event-fx
::fetched-data-completed
(fn [cofx [_ {:keys [data errors] :as payload}]]
(let [app-db (:db cofx)
error-message (-> errors :errors first :message)]
(if (or (nil? errors) (empty? errors))
(do (bla bla when success))
(pr error-message)))))
I'm stuck with this problem for a few months. maybe because I fetch a lot of data at the same time? or could be something else anyone knows?. By the way the actual code I use defmacro, but it works the same way as the above code.
So I managed to find the answer to my own question. It seems like app-db has not been initialized properly so I fixed that problem and everything works fine. hope it helps someone who struggle with this problem.
I'm newbee in Clojure world. My pet project is writing of kafka consumer / producer. There a lot of topic in www ,but I've faced with misunderstanding.
There is a code
(ns producer
(:require [clojure.tools.logging :as log])
(:import (java.util Properties)
(org.apache.kafka.common.serialization StringSerializer)
(org.apache.kafka.clients.producer KafkaProducer ProducerRecord))
(:gen-class))
(defn create-kafka-producer [server]
(let [producer-props {
"value.serializer" StringSerializer
"key.serializer" StringSerializer
"bootstrap.servers" server }]
(KafkaProducer. producer-props)))
(defn send-single-message [producer topic-name record]
(.send producer (ProducerRecord. topic-name (str "Value: " (.value record)))))
(defn -main []
(def svr "localhost:8084")
(def producer (create-kafka-producer svr))
(send-single-message producer "Test msg"))
I want just pass some msg into kafka through send-single-message function. But as you see in code example is using (.value record) and when I'm trying to pass the "Test msg" string it crashed and show the followed error
Exception in thread "main" java.lang.IllegalArgumentException: No matching field found: value for class java.lang.String
I understand that is because I've pass string object, that has no .value
So, how is it possible to solve that issue? Thanks in advance
p.s. I've tried to pass other structure, but no results or different errors
As you noted, the error is because you're attempting to get the value field of the passed object, but you're passing in a String which doesn't have a value field.
Checking that ProducerRecord constructor signature, it takes a generic argument of type V (which is just an Object as far as Clojure is concerned). I'd just pass the String directly and omit the value field access.
I'm learning ClojureScript, I have two functions that just change the content in the "root-app" div:
(ns blog.core)
(defn mount-components []
(let [content (js/document.getElementById "root-app")]
(while (.hasChildNodes content)
(.removeChild content (.-lastChild content)))
(.appendChild content (js/document.createTextNode "Wilkommen zu mein
ekelhaft blog!!"))))
(defn init! []
(def current_url js/window.location.href)
(if (clojure.string/includes? current_url "about")
(.log js/console (str "Whatever URL ->>>" js/window.location.href))
(mount-components)))
All works fine in http://localhost:3000/about because the "root-app" div exists in that page, but in http://localhost:3000/blog, I get the error message:
Because there is no such div in that page. All this is weird because it looks like ClojureScript in fact finds that:
(if (clojure.string/includes? current_url "about")
is actually false en the console.log is not printed.
My question is: why the function mount-components runs and sends the error message even when the conditional if is false? The weird thing is that the console.log:
(.log js/console (str "Whatever URL ->>>" js/window.location.href))
doesn't run but mount-components function does. I guess I'm not understanding the "sequence" in the way that ClojureScript works.
I'm not sure, but by your description, I think the logic you are thinking of and the logic you are actually testing are not the same. Your if statement looks for the word 'about' in the URL. If it is there, then it prints the console log i.e. it will be there for http://localhost:300/about. If it is NOT there, it will run the mount-components function, which looks for the div ID which you say is not included on the page, so you get the error. the mount-components is an ELSE statement and is therefore executed when the test is false.
The if form works like (if cond true-branch false-branch), so your (mount-component) is executed because it's in the false branch. Check out when, which only has a true branch.
I am using Clojure and Monger
It works fine, and I group functions by the collection they relate to.
Therefore, every file begins like this :
(ns img-cli.model.mycollectionname
(:require [monger.core :as mg]
[monger.collection :as mc]
[edn-config.core :refer [env]])
(:import [com.mongodb MongoOptions ServerAddress DB WriteConcern]
[org.bson.types ObjectId]))
(def config (get-in env [:mongo]))
;; using MongoOptions allows fine-tuning connection parameters,
;; like automatic reconnection (highly recommended for production
;; environment)
(def ^MongoOptions opts (mg/mongo-options { :threads-allowed-to-block-for-connection-multiplier 300}))
(def ^ServerAddress sa (mg/server-address (:url config) (:port config)))
(def conn (mg/connect sa opts))
(def db (mg/get-db conn (:db config)))
(def collection-name "asset")
;; I have to write one like this every time
(defn find-one-as-map
"fetch asset by Id"
[^String id]
(mc/find-one-as-map db collection-name {:_id (ObjectId. id)}))
Code duplication has of course several disadvantages in itself.
Also I'm not sure if connections are properly pooled afterwards ?
How can I avoid doing this ?
I sense I could pass an additional "db" parameter to each function, but then where would it come from ?
If I create the db connection in the "entry" file of my program, then how could it be passed to every function from there ?
For instance let's says I have Compojure routes in different files :
;; in the main handler file
(def db ...) ;; if I move the previous db configuration
;; in here, it could be the only place where this is set
;; importing Compojure routes from different files
(defroutes routes-from-file1
routes-from-file2...)
Let's say that some functions called from some of the routes in "file2" need access to the db, how can I pass this variable to them ?
I also have a lot of repetitive code afterwards, for instance to get data by Id for every collection...
I feel this could be simplified, but I'm not sure how.
Just refer to it by its namespace
(ns foo
(:require [handler :as h]))
(println h/db)