Hi π
I am playing with scittle ( ajax.core) and Youtube API.
I cannot understand how to work with the response from the HTTP request.
Please advise.
The function I use to make a call.
(require '[reagent.core :as r]
'[reagent.dom :as rdom]
'[ajax.core :refer [GET json-response-format]]
)
(def API-KEY "SECRET")
(defn download-all-comments [video-id]
(GET
""
;; ""
{
:response-format :json
:keywords? false
:params {:part "snippet,replies"
:maxResults 100
:order "relevance"
:textFormat "plainText"
:videoId video-id
:key API-KEY}
:handler #(let [response %
items (-> response .-items)]
(js/console.log response)
(doseq [item items]
(let [snippet (-> item .-snippet)
topLevelComment (-> snippet .-topLevelComment)
textDisplay (-> topLevelComment .-snippet .-textDisplay)]
(println "Text display" textDisplay))))
:error-handler #(println "Error" %)
}))
The JSON response looks like the following
{
"kind": "youtube#commentThreadListResponse",
"etag": "lWAwf_dsD-6XTpMgp0EQ9SZ3UKU",
"nextPageToken": "token=",
"pageInfo": {
"totalResults": 100,
"resultsPerPage": 100
},
"items": [
{
"kind": "youtube#commentThread",
"etag": "vxhgkEi2Mo4BJGF82RQMu5OBKDo",
"id": "UgxnkvKqeYq5HKtMrHp4AaABAg",
"snippet": {
"channelId": "UCGZXYc32ri4D0gSLPf2pZXQ",
"videoId": "dkklExEndHI",
"topLevelComment": {
"kind": "youtube#comment",
"etag": "_6rWhUOMIj07KMaYYyG4gIwY270",
"id": "UgxnkvKqeYq5HKtMrHp4AaABAg",
"snippet": {
"channelId": "UCGZXYc32ri4D0gSLPf2pZXQ",
"videoId": "dkklExEndHI",
"textDisplay": "sunlonger is blessed by God. when i listen to it always getting shivers. ",
"textOriginal": "sunlonger is blessed by God. when i listen to it always getting shivers. ",
"authorDisplayName": "@InterGlam",
"authorProfileImageUrl": "",
"authorChannelUrl": "",
"authorChannelId": {
"value": "UCrLOmuVrIXV_9KCoPKYE0mg"
},
"canRate": true,
"viewerRating": "none",
"likeCount": 3,
"publishedAt": "2011-03-29T15:38:53Z",
"updatedAt": "2011-03-29T15:38:53Z"
}
},
"canReply": true,
"totalReplyCount": 0,
"isPublic": true
}
}
]}
The list of items is always empty when I try to access it. π€·
console.log shows something like this and, I guess, itβs some internal representation of the json payload (dict)?
Maybe itβs response , body and then items?
ah got it
the response is a Clojure map, not a JS object, so you need to use get to access items
(-> response (get "items"))Thank you, let me try that π
strictly speaking you don't need ajax.core, you could also use the browser fetch API:
(-> (js/fetch "url")
(.then (fn [response])
(.json response)))
will give the response as a Promise of a JSON JS objectIt works! Thank you very much for the get fn.
I will try with js/fetch too. π
if you don't disable :keywords? with ajax.core you can use (-> response :items)
I tried to set up cider as described in the docs but so far I get only nrepl connected without eval working in the editor like in your demo. Itβs probably a separate thread π
there is a thread about it here: https://clojurians.slack.com/archives/C034FQN490E/p1703238977455889
one problem I found is that I had to uninstall clj-refactor
Thank you so much. Iβll check the thread π