Skip to content

Commit b825dd2

Browse files
committed
add lobster http client
1 parent 8b94aff commit b825dd2

9 files changed

Lines changed: 76 additions & 40 deletions

File tree

src/main/lobjur/state.cljs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
(defonce curr-view (atom (init-stories :hottest)))
77

88
;; To test a comment-heavy story, open a story, then evaluate this line
9-
(swap! curr-view assoc :short_id "jclvos")
9+
;; (swap! curr-view assoc-in [:story :short_id] "jclvos")

src/main/lobjur/utils/common.cljs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
(ns lobjur.utils.common
2-
(:require
3-
["gjs.gi.GLib" :as GLib]))
2+
)
43

54
(defn parse-json [t] (-> t (js/JSON.parse) (js->clj :keywordize-keys true)))
6-
(def base-url-lobster (GLib/Uri.parse "https://lobste.rs/" GLib/UriFlags.NONE))
75

src/main/lobjur/utils/http.cljs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,21 @@
88
(js* "~{}._promisify(~{}.Session.prototype, 'send_and_read_async', 'send_and_read_finish')", Gio, Soup)
99

1010
(def session (new Soup/Session #js {:user-agent "lobjur"}))
11-
(defn get [url]
11+
(defn get [url & {:as options}]
1212
(-> (.send_and_read_async
1313
session
14-
(Soup/Message.new "GET", url)
14+
(if (:params options)
15+
(Soup/Message.new_from_encoded_form
16+
"GET",
17+
url,
18+
(Soup/form_encode_hash (clj->js
19+
(into {}
20+
(map
21+
(fn [[k v]] [(name k) (str v)])
22+
(:params options))))))
23+
(Soup/Message.new "GET", url))
1524
0
1625
nil)
1726
(.then (comp
1827
ByteArray/toString
19-
ByteArray/fromGBytes))
20-
(.catch println)))
28+
ByteArray/fromGBytes))))

src/main/lobjur/widgets/comments.cljs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
[lobjur.state :refer [curr-view]]
55
[rollui.core :as rollui]
66
[lobjur.utils.http :as http]
7-
[lobjur.utils.common :refer [parse-json base-url-lobster]]
7+
[lobjur.utils.common :refer [parse-json]]
8+
[lobster.core :as lobster]
89
[lobjur.widgets.shared :refer [upvote-btn back-to-home-btn]]
910
["gjs.gi.GLib" :as GLib]
1011
["gjs.gi.Gio" :as Gio]
@@ -67,14 +68,13 @@
6768
(.connect "bind" list-bind))]
6869
(Gtk/ListView.new selection-model factory)))
6970

70-
(defn comments-view [{:keys [title url score tags short_id comment_count]}]
71+
(defn comments-view [{:keys [title url score tags short_id]}]
7172
[Adw/Clamp
7273
:child
7374
[Gtk/Box
7475
:orientation Gtk/Orientation.VERTICAL
7576
:spacing 8
7677
:margin-top 8
77-
:margin-bottom 8
7878
:margin-start 8
7979
:margin-end 8
8080
:.append
@@ -92,7 +92,7 @@
9292
[Gtk/Box
9393
:spacing 8
9494
:.append
95-
(let [host (.get_host (.parse_relative base-url-lobster url GLib/UriFlags.NONE))]
95+
(let [host (.get_host (.parse_relative lobster/base-url url GLib/UriFlags.NONE))]
9696
[Gtk/LinkButton
9797
:css_classes #js ["small" "button" "flat" "caption"]
9898
:halign Gtk/Align.START
@@ -106,13 +106,13 @@
106106
:valign Gtk/Align.CENTER
107107
:css_classes #js ["small" "button" "flat" "tag" "caption"]])]
108108
:.append
109-
(-> (http/get (str "https://lobste.rs/s/" short_id ".json"))
110-
(.then parse-json)
109+
(-> (lobster/story short_id)
111110
(.then
112111
(fn [comments]
113112
(if (> (count (:comments comments)) 0)
114113
[Gtk/ScrolledWindow
115114
:propagate-natural-height true
115+
:vexpand true
116116
:child
117117
(comments-list-view (:comments comments))]
118118
[Adw/StatusPage

src/main/lobjur/widgets/shared.cljs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
(ns lobjur.widgets.shared
22
(:require
33
[lobjur.state :refer [curr-view]]
4-
[lobjur.utils.common :refer [base-url-lobster]]
54
["gjs.gi.GLib" :as GLib]
65
["gjs.gi.Gtk" :as Gtk]))
76

src/main/lobjur/widgets/stories_list_view.cljs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,15 @@
33
[lobjur.widgets.shared :refer [upvote-btn back-to-home-btn]]
44
[lobjur.state :as state :refer [curr-view]]
55
[lobjur.utils.http :as http]
6-
[lobjur.utils.common :refer [parse-json base-url-lobster]]
6+
[lobjur.utils.common :refer [parse-json]]
7+
[lobster.core :as lobster]
78
[rollui.core :as rollui :refer-macros [defc]]
89
["gjs.gi.Adw" :as Adw]
910
["gjs.gi.GLib" :as GLib]
1011
["gjs.gi.Gtk" :as Gtk]))
1112

12-
(def stories-urls
13-
{:active "https://lobste.rs/active.json"
14-
:hottest "https://lobste.rs/hottest.json"
15-
:recents "https://lobste.rs/recents.json"})
16-
1713
(defn tagged-stories-url [tag page]
1814
(str "https://lobste.rs/t/" tag ".json" "?page=" page))
19-
(defn compute-stories-url [kw page]
20-
(str (kw stories-urls) "?page=" page))
21-
2215

2316
;;taken from https://stackoverflow.com/a/69122877/11189772
2417
(js* "function timeAgo(input) {
@@ -68,7 +61,7 @@ global.timeAgo = timeAgo")
6861
:spacing 8
6962
:selection-mode Gtk/SelectionMode.NONE
7063
:.append
71-
(let [host (.get_host (.parse_relative base-url-lobster url GLib/UriFlags.NONE))]
64+
(let [host (.get_host (.parse_relative lobster/base-url url GLib/UriFlags.NONE))]
7265
[Gtk/LinkButton
7366
:css_classes #js ["small" "button" "flat" "caption"]
7467
:halign Gtk/Align.START
@@ -145,14 +138,12 @@ global.timeAgo = timeAgo")
145138
:.add_css_class (list "small" "flat")
146139
:$clicked #(reset! curr-view {:name :search})])]])
147140
148-
(defn stories-widget-provider [url]
149-
(-> (http/get url)
150-
(.then parse-json)
151-
(.then
152-
(partial map story-item-widget))
141+
(defn stories-widget-provider [prom]
142+
(-> prom
143+
(.then (partial map story-item-widget))
153144
(.catch println)))
154145
155-
(defn stories-list-view [top-bar url]
146+
(defn stories-list-view [top-bar prom]
156147
[Gtk/ScrolledWindow
157148
:propagate_natural_height true
158149
:child
@@ -172,7 +163,7 @@ global.timeAgo = timeAgo")
172163
:.append
173164
[Gtk/ListBox
174165
:css_classes #js ["boxed-list"]
175-
:.append (stories-widget-provider url)]
166+
:.append (stories-widget-provider prom)]
176167
:.append
177168
[Gtk/Box
178169
:hexpand true

src/main/lobjur/widgets/user.cljs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
["gjs.gi.Gtk" :as Gtk]
55
["gjs.gi.Pango" :as Pango]
66
[lobjur.utils.common :refer [parse-json]]
7-
[lobjur.utils.http :as http]))
7+
[lobster.core :as lobster]))
88

99
(defn user-view [username]
1010
[Adw/Clamp
@@ -16,8 +16,7 @@
1616
[Gtk/ScrolledWindow
1717
:propagate-natural-height true
1818
:child
19-
(-> (http/get (str "https://lobste.rs/u/" username ".json"))
20-
(.then parse-json)
19+
(-> (lobster/user username)
2120
(.then
2221
(fn [user]
2322
(let [grid (Gtk/Grid. #js {:row_spacing 8

src/main/lobjur/widgets/window.cljs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
(ns lobjur.widgets.window
22
(:require
33
[lobjur.widgets.comments :refer [comments-view]]
4-
[lobjur.widgets.stories-list-view :refer [top-bar stories-list-view compute-stories-url tagged-stories-url]]
4+
[lobjur.widgets.stories-list-view :refer [top-bar stories-list-view tagged-stories-url]]
55
[lobjur.widgets.user :refer [user-view]]
6+
[lobster.core :as lobster]
67
[rollui.core :as rollui :refer [derived-atom] :refer-macros [defc]]
78
[lobjur.state :refer [curr-view]]
89
["gjs.gi.Gtk" :as Gtk]
@@ -15,7 +16,10 @@
1516
(fn [v]
1617
(println v)
1718
(case (:name v)
18-
:stories (stories-list-view top-bar (compute-stories-url (:stories-kw v) (:page v)))
19+
:stories (stories-list-view top-bar
20+
(case (:stories-kw v)
21+
:hottest (lobster/hottest (select-keys v [:page]))
22+
:active (lobster/active (select-keys v [:page]))))
1923
:comments (comments-view (:story v))
2024
:user (user-view (:username v))
2125
:tag (stories-list-view
@@ -26,9 +30,8 @@
2630
:label (str "Tagged with: " (:tag v))
2731
:xalign 0.0
2832
:.add_css_class "title-2"])
29-
(tagged-stories-url (:tag v)
30-
(:page v)))
31-
(stories-list-view top-bar (compute-stories-url :hottest 0))))))
33+
(lobster/tagged (:tag v) (select-keys v [:page])))
34+
(stories-list-view top-bar (lobster/hottest))))))
3235

3336
(declare header-bar)
3437
(defc header-bar []

src/main/lobster/core.cljs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
(ns lobster.core
2+
(:require
3+
[lobjur.utils.http :as http]
4+
[lobjur.utils.common :refer [parse-json]]
5+
["gjs.gi.GLib" :as GLib]))
6+
7+
(def base-url (GLib/Uri.parse "https://lobste.rs/" GLib/UriFlags.NONE))
8+
9+
(defn- rel [& urls]
10+
(.to_string (reduce
11+
(fn [base url]
12+
(.parse_relative base url GLib/UriFlags.NONE))
13+
base-url urls)))
14+
15+
(defn hottest [& {:as params}]
16+
(.then
17+
(http/get (rel "hottest.json") {:params params})
18+
parse-json))
19+
20+
(defn active [& {:as params}]
21+
(.then
22+
(http/get (rel "active.json") {:params params})
23+
parse-json))
24+
25+
(defn story [id & {:as params}]
26+
(.then
27+
(http/get (rel "s/" (str id ".json")) {:params params})
28+
parse-json))
29+
30+
(defn tagged [tag & {:as params}]
31+
(.then
32+
(http/get (rel "t/" (str tag ".json")) {:params params})
33+
parse-json))
34+
35+
(defn user [username & {:as params}]
36+
(.then
37+
(http/get (rel "u/" (str username ".json")) {:params params})
38+
parse-json))

0 commit comments

Comments
 (0)