Skip to content

Commit

Permalink
Add methods for collection the watch session API auth map
Browse files Browse the repository at this point in the history
Refs #53
  • Loading branch information
dhleong committed Oct 29, 2018
1 parent 1603047 commit f27947b
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 7 deletions.
15 changes: 15 additions & 0 deletions src/cljs/wish/providers.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -214,3 +214,18 @@
(throw (js/Error. (str "No provider instance for " sheet-id
"(" provider-id " / " pro-sheet-id ")"))))))

(defn watch-auth-map
"Given a collection of sheet IDs, generate an appropate
auth-map for use with the wish-server watch session API"
[sheet-ids]
(->> sheet-ids
(map (comp first unpack-id))
(into #{})
(reduce
(fn [m provider-id]
(if-let [auth (some-> provider-id
(provider-key :inst)
(provider/watch-auth))]
(assoc m provider-id auth)
m))
{})))
6 changes: 5 additions & 1 deletion src/cljs/wish/providers/caching.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,11 @@
(swap! dirty?-storage disj file-id))

; return the result as-is
result))))
result)))

(watch-auth [this]
; delegate
(provider/watch-auth base)))

(defn with-caching [base-provider]
(let [cache-id (keyword (str (name (provider/id base-provider))
Expand Down
7 changes: 6 additions & 1 deletion src/cljs/wish/providers/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,12 @@
The original `data` *may* be provided, which you can use to
update `:name`, for example, but it may also be omitted.
`data-str` is in the same string format that should be
returned by `load-raw`."))
returned by `load-raw`.")

(watch-auth
[this]
"Generate the auth data needed to watch changes to files provided
by this provider, or nil if that's not supported by this provider"))

(defn signed-out-err?
"Check if the given error was caused by not being signed into the provider"
Expand Down
16 changes: 12 additions & 4 deletions src/cljs/wish/providers/gdrive.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,15 @@
(.-currentUser)
(.get)))

(defn- auth-response []
(some-> (current-user)
(.getAuthResponse)))

(defn- access-token
"When logged in, get the current user's access token"
[]
(-> (current-user)
(.getAuthResponse)
(.-access_token)))
(some-> (auth-response)
(.-access_token)))

(defn- update-signin-status!
[signed-in?]
Expand Down Expand Up @@ -519,7 +522,12 @@
data-str))

; not ready? don't try
(to-chan [[(js/Error. "No network; unable to save sheet") nil]]))))
(to-chan [[(js/Error. "No network; unable to save sheet") nil]])))

(watch-auth [this]
(when-let [resp (auth-response)]
{:id_token (.-id_token resp)
:access_token (access-token)})))

(defn create-provider []
(->GDriveProvider))
6 changes: 5 additions & 1 deletion src/cljs/wish/providers/wish.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@
(to-chan [[(js/Error. "Not implemented") nil]]))

(save-sheet [this file-id data data-str]
(to-chan [[(js/Error. "Not implemented") nil]])))
(to-chan [[(js/Error. "Not implemented") nil]]))

(watch-auth [this]
; not supported:
nil))

(defn create-provider []
(->WishProvider))

0 comments on commit f27947b

Please sign in to comment.