|
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}"); |
|
} |
|
} |