Skip to content

Instantly share code, notes, and snippets.

@ryoakg
Last active July 29, 2020 17:35
Show Gist options
  • Save ryoakg/30b46081ec4d1e8d1be4879204ed9c12 to your computer and use it in GitHub Desktop.
Save ryoakg/30b46081ec4d1e8d1be4879204ed9c12 to your computer and use it in GitHub Desktop.
(set-env! :dependencies '[;; https://mvnrepository.com/artifact/com.dropbox.core/dropbox-core-sdk
[com.dropbox.core/dropbox-core-sdk "3.0.5"]])
(require '[clojure.reflect :refer [reflect]])
(->> com.dropbox.core.v2.files.DbxUserFilesRequests
reflect
:members
(filter #(and (:return-type %)
(re-find #"Builder\z" (str (:name %)))
((:flags %) :public)))
(sort-by :name)
(map (juxt :name :parameter-types)))
#_([alphaGetMetadataBuilder [java.lang.String]]
[alphaUploadBuilder [java.lang.String]]
[copyBatchBuilder [java.util.List]]
[copyBuilder [java.lang.String java.lang.String]]
[copyV2Builder [java.lang.String java.lang.String]]
[downloadBuilder [java.lang.String]]
[getMetadataBuilder [java.lang.String]]
[getPreviewBuilder [java.lang.String]]
[getThumbnailBuilder [java.lang.String]]
[listFolderBuilder [java.lang.String]]
[listFolderGetLatestCursorBuilder [java.lang.String]]
[listRevisionsBuilder [java.lang.String]]
[moveBatchBuilder [java.util.List]]
[moveBuilder [java.lang.String java.lang.String]]
[moveV2Builder [java.lang.String java.lang.String]]
[searchBuilder [java.lang.String java.lang.String]]
[uploadBuilder [java.lang.String]])
;;; see: https://github.com/dropbox/dropbox-sdk-java
(set-env! :dependencies '[;; https://mvnrepository.com/artifact/com.dropbox.core/dropbox-core-sdk
[com.dropbox.core/dropbox-core-sdk "3.0.5"]])
;;; https://github.com/dropbox/dropbox-sdk-java/blob/master/src/main/java/com/dropbox/core/v2/DbxPathV2.java
(import (com.dropbox.core.v2 DbxClientV2 DbxPathV2))
;;; 文字列の操作しかしていない
;;;
;;; Dropbox では / でディレクトリを区切っているよ!
;;; という事をコードで表現したものと考えればいい
;;;
;;; なので、Pathクラス/オブジェクト があるわけでもなく
;;; 対象が Dropbox上に存在している必用もない
(DbxPathV2/getParent "/tmp")
;; => ""
(DbxPathV2/getParent "/tmp/a/b")
;; => "/tmp/a"
(DbxPathV2/getName "/tmp/a/b")
;; => "b"
(seq (DbxPathV2/split "/tmp/a/b"))
;; => ("tmp" "a" "b")
(DbxPathV2/findError "/tmp")
;; => nil
(DbxPathV2/findError "tmp")
;; => "expecting first character to be \"/\""
(DbxPathV2/findError "/tmp/")
;; => "must not end with \"/\""
(->> ["/tmp" "tmp" "/tmp/"]
(map #(DbxPathV2/isValid %)))
;; => (true false false)
;;; see: https://github.com/dropbox/dropbox-sdk-java
(set-env! :dependencies '[;; https://mvnrepository.com/artifact/com.dropbox.core/dropbox-core-sdk
[com.dropbox.core/dropbox-core-sdk "3.0.5"]])
(require '[clojure.java.io :as io]
'[clojure.set :as set]
'[clojure.java.browse :refer [browse-url]]
'[clojure.reflect :refer [reflect]])
(import (com.dropbox.core DbxAppInfo
DbxRequestConfig
DbxWebAuthNoRedirect
DbxAuthFinish
DbxException)
(com.dropbox.core.v2 DbxClientV2 DbxPathV2)
(com.dropbox.core.v2.users FullAccount)
(com.dropbox.core.v2.files FileMetadata Metadata
ListFolderResult
DbxUserFilesRequests
CommitInfo WriteMode
UploadErrorException
UploadBuilder
UploadSessionCursor
UploadSessionFinishErrorException
UploadSessionLookupErrorException
UploadSessionOffsetError
DownloadErrorException
DownloadBuilder)
(java.util Locale))
;;; 1. Create App on the console
;;; https://www.dropbox.com/developers/apps/create
(def app-key (atom nil))
(def app-secret (atom nil))
(def client (atom nil))
;;; 2. set key & secret
;;; app list: https://www.dropbox.com/developers/apps/
(do
(print "app-key: ") (reset! app-key (read-line))
(print "app-secret: ") (reset! app-secret (read-line))
nil)
;;; You can see the app information anytime
(browse-url (str "https://www.dropbox.com/developers/apps/info/" @app-key))
(defn authenticate-on-the-web
"returns access token"
[^DbxRequestConfig key secret config url-opener authorization-code-prompt]
(let [^DbxWebAuthNoRedirect web-auth (DbxWebAuthNoRedirect. config
(DbxAppInfo. key secret))
^String auth-url (.start web-auth)]
(url-opener auth-url)
(let [^DbxAuthFinish auth-finish (.finish web-auth (authorization-code-prompt))]
(.getAccessToken auth-finish))))
;;; get a Dropbox handler
(let [^DbxRequestConfig config (-> (DbxRequestConfig/newBuilder "Foo/1.0")
(.withUserLocale (str (java.util.Locale/getDefault)))
.build)
^String access-token (authenticate-on-the-web @app-key @app-secret config
browse-url
#(do
(print "Authorization code: ")
(read-line)))]
(reset! client (DbxClientV2. config access-token)))
(-> @client
.users
.getCurrentAccount
bean
(update :name bean))
;;; Uploading files
;;; see: https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/upload-file/src/main/java/com/dropbox/core/examples/upload_file/Main.java
;;; alt: .chunkedUploadFile()
(let [dropbox-path "/hosts.txt"]
(with-open [is (io/input-stream "/etc/hosts")]
(let [^DbxUserFilesRequests req (.files @client)
^UploadBuilder bld (-> req
(.uploadBuilder dropbox-path)
(.withMode WriteMode/ADD)
(.withClientModified (java.util.Date.)))
^FileMetadata meta (-> bld (.uploadAndFinish is))]
(bean meta))))
;;; Listing files
;;; see: https://github.com/dropbox/dropbox-sdk-java/blob/master/examples/tutorial/src/main/java/com/dropbox/core/examples/tutorial/Main.java
(let [folder "" ;empty-string means the root
c @client]
((fn re [^ListFolderResult res]
(lazy-seq (concat (-> res
.getEntries
(->> (map bean)))
(when (.getHasMore res)
(re (-> c
.files
(.listFolderContinue (.getCursor res))))))))
(-> c
.files
(.listFolder folder))))
;;; Downloading files
(let [dropbox-path "/hosts.txt"]
(let [^java.io.OutputStream os (java.io.ByteArrayOutputStream.)
^DbxUserFilesRequests req (.files @client)
^DownloadBuilder bld (-> req
(.downloadBuilder dropbox-path))
^FileMetadata meta (-> bld (.download os))]
{:meta (bean meta)
:content (.toString os "UTF-8")}))
;;; Operations
(->> com.dropbox.core.v2.files.DbxUserFilesRequests
reflect
:members
(filter #(and (re-find #"Builder\z" (str (:name %)))
(set/intersection #{:public} (:flags %))))
(map (juxt :name :parameter-types)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment