Skip to content

Instantly share code, notes, and snippets.

@kmader
Last active October 22, 2015 20:35
Show Gist options
  • Save kmader/66d4772916c89b5cd40a to your computer and use it in GitHub Desktop.
Save kmader/66d4772916c89b5cd40a to your computer and use it in GitHub Desktop.
Dropbox API for Scala

Overview

Basically a translation of the official Java API (https://www.dropbox.com/developers-v1/core/start/java) a bit scala-fied.

SimpleCreds

You must adapt this file to contain the api and access tokens from your credentials before starting (make generateNewToken equal to true)

SimpleTests

Instead of having a demo program this runs as a very basic scalatest. This means you can run it from the sbt console by just typing >test

name := "dropbox-scala"
version := "1.0-SNAPSHOT"
organization := "fourquant"
scalaVersion := "2.10.4"
resolvers += "Local Maven Repository" at "file:///"+Path.userHome+"/.m2/repository" // for fourquant dependencies
libraryDependencies += "com.dropbox.core" % "dropbox-core-sdk" % "1.8.2"
publishMavenStyle := false
publishTo := {
val nexus = "https://oss.sonatype.org/"
if (version.value.endsWith("SNAPSHOT"))
Some("snapshots" at nexus + "content/repositories/snapshots")
else
Some("releases" at nexus + "service/local/staging/deploy/maven2")
}
pomExtra := (
<url>https://bitbucket.org/4quant</url>
<developers>
<developer>
<id>kmader</id>
<name>Kevin Mader</name>
<url>https://github.com/kmader</url>
</developer>
</developers>)
libraryDependencies += "org.scalatest" %% "scalatest" % "2.2.1" % "test"
javaOptions in test += "-Xmx2G"
package fourquant
/**
* Created by mader on 10/22/15.
*/
object SimpleCreds {
val app_key = "YOUR-KEY"
val app_secret = "YOUR-SECRET"
val app_authcode = "YOUR-CODE"
val app_accesstoken = "YOUR ACCESS TOKEN"
}
package fourquant.dropbox
import java.io.{File, FileInputStream, FileOutputStream, PrintWriter}
import java.util.Locale
import com.dropbox.core._
import fourquant.SimpleCreds
import org.scalatest.{BeforeAndAfterAll, FunSuite, Matchers}
import scala.collection.JavaConversions._
import scala.util.Try
/**
* Created by mader on 10/22/15.
*/
class SimpleTests extends FunSuite with BeforeAndAfterAll with Matchers {
import SimpleCreds._
val appInfo = new DbxAppInfo(app_key, app_secret)
val config = new DbxRequestConfig(
"JavaTutorial/1.0", Locale.getDefault().toString())
val generateNewToken = false
if(generateNewToken) {
test("Create Access Token") {
val webAuth = new DbxWebAuthNoRedirect(config, appInfo)
val authorizeUrl = webAuth.start()
println("1. Go to: " + authorizeUrl)
println("2. Click \"Allow\" (you might have to log in first)")
println("3. Copy the authorization code.")
//val code = new BufferedReader(new InputStreamReader(System.in)).readLine().trim()
val authFinish = webAuth.finish(app_authcode)
println(s"accessToken = ${authFinish.accessToken}")
}
}
val client = new DbxClient(config, app_accesstoken)
test ("Basic Setup") {
println(s"Linked Account is ${client.getAccountInfo().displayName}")
}
test("Upload File") {
val inputFile = File.createTempFile("InputData","txt")
val pwFile = new PrintWriter(inputFile)
pwFile.write("Is Just a Test\nTesting")
pwFile.close()
val inputStream = new FileInputStream(inputFile)
try {
val uploadedFile = client.uploadFile("/magnum-opus.txt",
DbxWriteMode.add(), inputFile.length(), inputStream)
println(s"Uploaded: ${uploadedFile.toString}")
} finally {
inputStream.close()
}
}
test("List Folder") {
val listing: DbxEntry.WithChildren = client.getMetadataWithChildren("/")
println("Files in the root path:")
for (child <- listing.children) {
println(s" ${child.name}\t${child.toString}")
}
}
test("Download Files") {
val outputStream = new FileOutputStream("magnum-opus.txt");
val downloadedFile: Try[DbxEntry.File] = Try {
client.getFile("/magnum-opus.txt", null,
outputStream)
}
outputStream.close()
println(s"Metadata: ${downloadedFile.toString}");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment