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`. diff --git a/src/kintone_client/url.cljc b/src/kintone_client/url.cljc index 7dc5d93..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 "|")) @@ -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\"} @@ -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") @@ -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 @@ -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") @@ -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)))) \ No newline at end of file diff --git a/test/kintone_client/url_test.cljc b/test/kintone_client/url_test.cljc index d5be1ca..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"} @@ -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)))