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

Add boundary layer #6

Merged
merged 2 commits into from
Dec 25, 2017
Merged
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
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@
:dependencies [[rkworks/baum "0.4.0"]]}
:provided {:dependencies [[org.clojure/clojure "1.8.0"]]}
:1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]}
:1.9 {:dependencies [[org.clojure/clojure "1.9.0-beta1"]]}})
:1.9 {:dependencies [[org.clojure/clojure "1.9.0"]]}})
61 changes: 48 additions & 13 deletions src/cybozu_http/kintone/api/app.clj
Original file line number Diff line number Diff line change
@@ -1,20 +1,55 @@
(ns cybozu-http.kintone.api.app
(:refer-clojure :exclude [get])
(:require [cybozu-http.kintone.api.bare :refer [defapi]]))
(:require [cybozu-http.kintone.api.internal.app :as internal]))

(defapi get :get "/app.json"
[id :- app-id])
(defprotocol AppAPI
(get
[auth app-id]
[auth app-id opts]))

(defapi get-form :get "/form.json"
[app :- app-id])
(extend-protocol AppAPI
clojure.lang.IPersistentMap
(get
([auth app-id]
(internal/get auth app-id))
([auth app-id opts]
(internal/get auth app-id opts))))

;;; form configurations
;;; doc: https://developer.cybozu.io/hc/ja/articles/204783170
;;; https://developer.cybozu.io/hc/ja/articles/204529724
(defprotocol AppFormAPI
(get-form
[auth app-id]
[auth app-id opts]))

(defapi get-fields :get "/app/form/fields.json"
[app :- app-id]
[lang :- language])
(extend-protocol AppFormAPI
clojure.lang.IPersistentMap
(get-form
([auth app-id]
(internal/get-form auth app-id))
([auth app-id opts]
(internal/get-form auth app-id opts))))

(defapi get-layout :get "/app/form/layout.json"
[app :- app-id])
(defprotocol AppFieldsAPI
(get-fields
[auth app-id]
[auth app-id opts]))

(extend-protocol AppFieldsAPI
clojure.lang.IPersistentMap
(get-fields
([auth app-id]
(internal/get-fields auth app-id))
([auth app-id opts]
(internal/get-fields auth app-id opts))))

(defprotocol AppLayoutAPI
(get-layout
[auth app-id]
[auth app-id opts]))

(extend-protocol AppLayoutAPI
clojure.lang.IPersistentMap
(get-layout
([auth app-id]
(internal/get-layout auth app-id))
([auth app-id opts]
(internal/get-layout auth app-id opts))))
21 changes: 11 additions & 10 deletions src/cybozu_http/kintone/api/apps.clj
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
(ns cybozu-http.kintone.api.apps
(:refer-clojure :exclude [get])
(:require [cybozu-http.kintone.api.bare :refer [defapi]]))
(:require [cybozu-http.kintone.api.internal.apps :as internal]))

(defapi get :get "/apps.json"
[]
[ids :- app-ids
codes :- app-codes
name :- name
spaceIds :- space-ids
limit :- limit
offset :- offset]
[:apps])
(defprotocol AppsAPI
(get [auth] [auth opts]))

(extend-protocol AppsAPI
clojure.lang.IPersistentMap
(get
([auth]
(internal/get auth))
([auth opts]
(internal/get auth opts))))
42 changes: 19 additions & 23 deletions src/cybozu_http/kintone/api/file.clj
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
(ns cybozu-http.kintone.api.file
(:require [cheshire.core :as c]
[clojure.java.io :as io]
[cybozu-http.kintone.api.bare :as bare])
(:import java.nio.file.attribute.FileAttribute
java.nio.file.Files))
(:require [cybozu-http.kintone.api.internal.file :as internal]))

(defn- rename-file [file new-filename]
(let [parent (Files/createTempDirectory "cybozu-http-clj-" (into-array FileAttribute []))
new-file (io/file parent new-filename)]
(io/copy file new-file)
new-file))
(defprotocol FileAPI
(upload [auth file] [auth file opts])
(download [auth file-key] [auth file-key opts]))

(defn upload* [auth file & {:keys [filename] :as opts}]
(let [file (cond-> file (seq filename) (rename-file filename))
params {:multipart [{:name "file" :content file}]}]
(bare/api-call auth :post "/file.json" params opts)))
(extend-protocol FileAPI
clojure.lang.IPersistentMap
(upload
([auth file]
(internal/upload auth file))

(defn upload [auth file & {:keys [filename]}]
(-> (upload* auth file :filename filename)
:body
(c/parse-string true)
:fileKey))
([auth file opts]
(->> (reduce into [] opts)
(apply internal/upload auth file))))

(defn download* [auth file-key & {:keys [as-byte-array?] :as opts}]
(let [params (cond-> (bare/build-params :get {:fileKey file-key})
as-byte-array? (assoc :as :byte-array))]
(bare/api-call auth :get "/file.json" params opts)))
(download
([auth file-key]
(internal/download auth file-key))

([auth file-key opts]
(->> (reduce into [] opts)
(apply internal/download auth file-key)))))
25 changes: 20 additions & 5 deletions src/cybozu_http/kintone/api/guests.clj
Original file line number Diff line number Diff line change
@@ -1,8 +1,23 @@
(ns cybozu-http.kintone.api.guests
(:require [cybozu-http.kintone.api.bare :refer [defapi]]))
(:require [cybozu-http.kintone.api.internal.guests :as internal]))

(defapi post :post "/guests.json"
[guests :- guests])
(defprotocol GuestsAPI
(post
[auth guests]
[auth guests opts])
(delete
[auth guests]
[auth guests opts]))

(defapi delete :delete "/guests.json"
[guests :- guests])
(extend-protocol GuestsAPI
clojure.lang.IPersistentMap
(post
([auth guests]
(internal/post auth guests))
([auth guests opts]
(internal/post auth guests opts)))
(delete
([auth guests]
(internal/delete auth guests))
([auth guests opts]
(internal/delete auth guests opts))))
20 changes: 20 additions & 0 deletions src/cybozu_http/kintone/api/internal/app.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(ns cybozu-http.kintone.api.internal.app
(:refer-clojure :exclude [get])
(:require [cybozu-http.kintone.api.internal.bare :refer [defapi]]))

(defapi get :get "/app.json"
[id :- app-id])

(defapi get-form :get "/form.json"
[app :- app-id])

;;; form configurations
;;; doc: https://developer.cybozu.io/hc/ja/articles/204783170
;;; https://developer.cybozu.io/hc/ja/articles/204529724

(defapi get-fields :get "/app/form/fields.json"
[app :- app-id]
[lang :- language])

(defapi get-layout :get "/app/form/layout.json"
[app :- app-id])
13 changes: 13 additions & 0 deletions src/cybozu_http/kintone/api/internal/apps.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
(ns cybozu-http.kintone.api.internal.apps
(:refer-clojure :exclude [get])
(:require [cybozu-http.kintone.api.internal.bare :refer [defapi]]))

(defapi get :get "/apps.json"
[]
[ids :- app-ids
codes :- app-codes
name :- name
spaceIds :- space-ids
limit :- limit
offset :- offset]
[:apps])
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(ns cybozu-http.kintone.api.bare
(ns cybozu-http.kintone.api.internal.bare
(:require [cheshire.core :as c]
[clj-http.client :as cli]
[slingshot.slingshot :refer [try+]])
Expand Down
31 changes: 31 additions & 0 deletions src/cybozu_http/kintone/api/internal/file.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
(ns cybozu-http.kintone.api.internal.file
(:require [cheshire.core :as c]
[clojure.java.io :as io]
[cybozu-http.kintone.api.internal.bare :as bare])
(:import java.nio.file.attribute.FileAttribute
java.nio.file.Files))

(defn- rename-file [file new-filename]
(let [parent (Files/createTempDirectory "cybozu-http-clj-" (into-array FileAttribute []))
new-file (io/file parent new-filename)]
(io/copy file new-file)
new-file))

(defn upload* [auth file & {:keys [filename] :as opts}]
(let [file (cond-> file (seq filename) (rename-file filename))
params {:multipart [{:name "file" :content file}]}]
(bare/api-call auth :post "/file.json" params opts)))

(defn upload [auth file & {:keys [filename]}]
(-> (upload* auth file :filename filename)
:body
(c/parse-string true)
:fileKey))

(defn download* [auth file-key & {:keys [as-byte-array?] :as opts}]
(let [params (cond-> (bare/build-params :get {:fileKey file-key})
as-byte-array? (assoc :as :byte-array))]
(bare/api-call auth :get "/file.json" params opts)))

(defn download [auth file-key & opts]
(apply download* auth file-key opts))
8 changes: 8 additions & 0 deletions src/cybozu_http/kintone/api/internal/guests.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(ns cybozu-http.kintone.api.internal.guests
(:require [cybozu-http.kintone.api.internal.bare :refer [defapi]]))

(defapi post :post "/guests.json"
[guests :- guests])

(defapi delete :delete "/guests.json"
[guests :- guests])
Loading