From c04618409ec66e752803bcbcde8121b3abc937a4 Mon Sep 17 00:00:00 2001 From: saitouena Date: Mon, 10 Feb 2020 14:15:27 +0900 Subject: [PATCH 1/6] add ->base-url and ->app-url (url generator) --- src/kintone_client/url.cljc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/kintone_client/url.cljc b/src/kintone_client/url.cljc index 7dc5d93..dc1881c 100644 --- a/src/kintone_client/url.cljc +++ b/src/kintone_client/url.cljc @@ -154,3 +154,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)))) \ No newline at end of file From dea5062d03d8acdae627e6575292a9a0087c3e3c Mon Sep 17 00:00:00 2001 From: saitouena Date: Mon, 10 Feb 2020 14:21:31 +0900 Subject: [PATCH 2/6] add ->base-url and ->app-url test --- test/kintone_client/url_test.cljc | 40 +++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/test/kintone_client/url_test.cljc b/test/kintone_client/url_test.cljc index d5be1ca..3137f88 100644 --- a/test/kintone_client/url_test.cljc +++ b/test/kintone_client/url_test.cljc @@ -632,3 +632,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))) From 2266cce5d2b877953e768ba1e268a758765c9559 Mon Sep 17 00:00:00 2001 From: saitouena Date: Mon, 10 Feb 2020 14:21:48 +0900 Subject: [PATCH 3/6] add ->base-url ->app-url document --- README.md | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/README.md b/README.md index a39416b..df9d9ef 100644 --- a/README.md +++ b/README.md @@ -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`. From c57f664091b92dde55142babc6dd86109d78cdec Mon Sep 17 00:00:00 2001 From: saitouena Date: Mon, 10 Feb 2020 14:22:04 +0900 Subject: [PATCH 4/6] add TODO of parse-base-url and parse-app-url for consistency It seems better that parse-app-url and parse-base-url return :s? key for consistency --- src/kintone_client/url.cljc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/kintone_client/url.cljc b/src/kintone_client/url.cljc index dc1881c..14525d3 100644 --- a/src/kintone_client/url.cljc +++ b/src/kintone_client/url.cljc @@ -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\"} @@ -113,6 +114,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 From 0655533607765828ed83b279c7fa851fae8b63b8 Mon Sep 17 00:00:00 2001 From: saitouena Date: Mon, 10 Feb 2020 14:48:04 +0900 Subject: [PATCH 5/6] parse-app-url and parse-base-url returns :s? :s? is true with https://hoge.s.cybozu.com --- src/kintone_client/url.cljc | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) diff --git a/src/kintone_client/url.cljc b/src/kintone_client/url.cljc index 14525d3..f25b2e6 100644 --- a/src/kintone_client/url.cljc +++ b/src/kintone_client/url.cljc @@ -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 "|")) @@ -65,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") @@ -124,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") From fe44630e6d3d465e20eb0d065e2736f5d5f81048 Mon Sep 17 00:00:00 2001 From: saitouena Date: Mon, 10 Feb 2020 14:49:32 +0900 Subject: [PATCH 6/6] fix .s test --- test/kintone_client/url_test.cljc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/test/kintone_client/url_test.cljc b/test/kintone_client/url_test.cljc index 3137f88..3ad2f3c 100644 --- a/test/kintone_client/url_test.cljc +++ b/test/kintone_client/url_test.cljc @@ -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" @@ -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"} @@ -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"}