Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse-app(base)-url returns .s url or not #18

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ You can do insert, update, delete at once with `bulk-request`.
;; => {:domain "cybozu.com", :subdomain "foo", :guest-space-id "11", :app-id "1"}
(valid-app-url? "https://hoge.cybozu.com")
;; => true
(->base-url {:subdomain "foo" :domain "cybozu.com"})
;; => "https://foo.cybozu.com"
(->base-url {:subdomain "foo" :domain "cybozu.com" :s? true}) ;; you can specify :s? if needed
;; => "https://foo.s.cybozu.com"
(->app-url {:subdomain "foo" :domain "cybozu.com" :guest-space-id "11" :app-id "1"})
;; => "https://foo.cybozu.com/k/guest/11/1"
(->app-url {:subdomain "foo" :domain "cybozu.com"}) ;; returns nil if given info is not enough or generated url is invalid
;; => nil
```

For more information, See [API documents](https://cljdoc.org/d/toyokumo/kintone-client/CURRENT), `test/`, and `dev/test.clj`.
Expand Down
49 changes: 36 additions & 13 deletions src/kintone_client/url.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"cybozu-dev.cn"])

(def ^:private re-base-url*
(str "^https://([a-zA-Z0-9][a-zA-Z0-9\\-]{1,30}[a-zA-Z0-9])(?:\\.s)?\\."
(str "^https://([a-zA-Z0-9][a-zA-Z0-9\\-]{1,30}[a-zA-Z0-9])(\\.s)?\\."
"("
(->> (map #(str/replace % "." "\\.") domain-list)
(str/join "|"))
Expand All @@ -56,6 +56,7 @@
(extract-base-url "https://foo.s.cybozu.com/k/guest/11/1")
(extract-base-url "https://hoge.hoge.com/k/11"))

;; TODO: add :s? key to returned map
(defn parse-base-url
"
(parse-base-url \"https://hoge.cybozu.com\")\n=> {:domain \"cybozu.com\", :subdomain \"hoge\"}
Expand All @@ -64,9 +65,10 @@
(parse-base-url \"https://hoge.hoge.com/k/11\")\n=> nil
"
[url]
(when-let [[_ subdomain domain] (re-find re-base-url url)]
{:domain domain
:subdomain subdomain}))
(when-let [[_ subdomain s domain] (re-find re-base-url url)]
(cond-> {:domain domain
:subdomain subdomain}
s (assoc :s? true))))

(comment
(parse-base-url "https://hoge.cybozu.com")
Expand Down Expand Up @@ -113,6 +115,7 @@
(extract-app-url "https://foo.s.cybozu.com/k/guest/11/1")
(extract-app-url "https://hoge.hoge.com/k/11"))

;; TODO: add :s? key to returned map
(defn parse-app-url
"
(parse-app-url \"https://hoge.cybozu.com\")\n=> nil\n
Expand All @@ -122,15 +125,17 @@
"
[url]
(or
(when-let [[_ subdomain domain app-id] (re-find re-app-url url)]
{:domain domain
:subdomain subdomain
:app-id app-id})
(when-let [[_ subdomain domain guest-space-id app-id] (re-find re-guest-app-url url)]
{:domain domain
:subdomain subdomain
:guest-space-id guest-space-id
:app-id app-id})))
(when-let [[_ subdomain s domain app-id] (re-find re-app-url url)]
(cond-> {:domain domain
:subdomain subdomain
:app-id app-id}
s (assoc :s? true)))
(when-let [[_ subdomain s domain guest-space-id app-id] (re-find re-guest-app-url url)]
(cond-> {:domain domain
:subdomain subdomain
:guest-space-id guest-space-id
:app-id app-id}
s (assoc :s? true)))))

(comment
(parse-app-url "https://hoge.cybozu.com")
Expand All @@ -154,3 +159,21 @@
(valid-app-url? "https://hoge.cybozu.com/k/12")
(valid-app-url? "https://foo.s.cybozu.com/k/guest/11/1")
(valid-app-url? "https://hoge.hoge.com/k/11"))

(defn ->base-url
"generates kintone base url. returns nil if input data is not enough or generated app url is invalid."
[{:keys [domain subdomain s?]}]
(when (and domain subdomain)
(let [base-url (str "https://" subdomain "." (when s? "s.") domain)]
(when (valid-base-url? base-url)
base-url))))

(defn ->app-url
"generates kintone app url. returns nil if input data is not enough or generated app url is invalid."
[{:keys [domain subdomain guest-space-id app-id s?]}]
(when (and domain subdomain app-id)
(let [app-url (if guest-space-id
(str "https://" subdomain "." (when s? "s.") domain "/k/guest/" guest-space-id "/" app-id)
(str "https://" subdomain "." (when s? "s.") domain "/k/" app-id))]
(when (valid-app-url? app-url)
app-url))))
47 changes: 44 additions & 3 deletions test/kintone_client/url_test.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@

"https://foo.s.cybozu.com/k/1"
{:domain "cybozu.com"
:subdomain "foo"}
:subdomain "foo"
:s? true}

"https://foo-bar.cybozu.com/k/1"
{:domain "cybozu.com"
Expand Down Expand Up @@ -414,7 +415,7 @@
nil

"https://foo.s.cybozu.com/k/1"
{:subdomain "foo" :domain "cybozu.com" :app-id "1"}
{:subdomain "foo" :domain "cybozu.com" :app-id "1" :s? true}

"https://foo-bar.cybozu.com/k/1"
{:subdomain "foo-bar" :domain "cybozu.com" :app-id "1"}
Expand Down Expand Up @@ -464,7 +465,7 @@
{:subdomain "foo" :domain "cybozu.com" :guest-space-id "11" :app-id "99999999"}

"https://foo.s.cybozu.com/k/guest/11/1"
{:subdomain "foo" :domain "cybozu.com" :guest-space-id "11" :app-id "1"}
{:subdomain "foo" :domain "cybozu.com" :guest-space-id "11" :app-id "1" :s? true}

"https://foo-bar.cybozu.com/k/guest/11/1"
{:subdomain "foo-bar" :domain "cybozu.com" :guest-space-id "11" :app-id "1"}
Expand Down Expand Up @@ -632,3 +633,43 @@

"https://foo.cybozu.com/k/guest/11/"
false)))

(deftest ->base-url-test
(are [m base-url] (= (sut/->base-url m) base-url)
{:subdomain "foo" :domain "cybozu.com"}
"https://foo.cybozu.com"
{:subdomain "foo" :domain "cybozu.com" :s? true}
"https://foo.s.cybozu.com"
;; invalid
{:subdomain "foo"}
nil
;; invalid
{:subdomain "foo_bar" :domain "cybozu.com"}
nil))

(deftest ->app-url-test
(testing "default space app"
(are [m app-url] (= (sut/->app-url m) app-url)
{:subdomain "foo" :domain "cybozu.com" :app-id "1"}
"https://foo.cybozu.com/k/1"
;; s?
{:subdomain "foo" :domain "cybozu.com" :app-id "1" :s? true}
"https://foo.s.cybozu.com/k/1"
;; invalid
{:subdomain "foo" :domain "cybozu.com"}
nil
;; invalid
{:subdomain "foo_bar" :domain "cybozu.com" :app-id "1"}
nil))
(testing "guest space app"
(are [m app-url] (= (sut/->app-url m) app-url)
{:subdomain "foo" :domain "cybozu.com" :guest-space-id "11" :app-id "1"}
"https://foo.cybozu.com/k/guest/11/1"
{:subdomain "foo" :domain "cybozu.com" :guest-space-id "11" :app-id "1" :s? true}
"https://foo.s.cybozu.com/k/guest/11/1"
;; invalid
{:subdomain "foo" :domain "cybozu.com" :guest-space-id "11"}
nil
;; invalid
{:subdomain "foo_bar" :domain "cybozu.com" :guest-space-id "11" :app-id "1"}
nil)))