From 11d7ac714c519a177f9bc4808f7594c60ddfeb49 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 22 Jul 2019 02:34:55 +0300 Subject: [PATCH] Added initial dropbox tests - get folder contents and download file --- .../ui/worldscreen/optionstable/DropBox.kt | 70 +++++++++++++++++++ .../optionstable/WorldScreenOptionsTable.kt | 12 +++- 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 core/src/com/unciv/ui/worldscreen/optionstable/DropBox.kt diff --git a/core/src/com/unciv/ui/worldscreen/optionstable/DropBox.kt b/core/src/com/unciv/ui/worldscreen/optionstable/DropBox.kt new file mode 100644 index 0000000000..1ad4266878 --- /dev/null +++ b/core/src/com/unciv/ui/worldscreen/optionstable/DropBox.kt @@ -0,0 +1,70 @@ +package com.unciv.ui.worldscreen.optionstable + +import com.unciv.logic.GameSaver +import java.io.BufferedReader +import java.io.DataOutputStream +import java.io.InputStreamReader +import java.net.HttpURLConnection +import java.net.URL +import java.nio.charset.StandardCharsets + +class DropBox(){ + + /** + * @param dropboxApiArg If true, then the data will be sent via a Dropbox-Api-Arg header and not via the post body + */ + fun dropboxApi(url:String, data:String="",dropboxApiArg:Boolean=false):String{ + + with(URL(url).openConnection() as HttpURLConnection) { + requestMethod = "POST" // optional default is GET + + setRequestProperty("Authorization","Bearer LTdBbopPUQ0AAAAAAAACxh4_Qd1eVMM7IBK3ULV3BgxzWZDMfhmgFbuUNF_rXQWb") + if(!dropboxApiArg) setRequestProperty("Content-Type","application/json") + setRequestProperty("Dropbox-API-Arg", data) + doOutput = true + + try { + if(!dropboxApiArg) { + val postData: ByteArray = data.toByteArray(StandardCharsets.UTF_8) + val outputStream = DataOutputStream(outputStream) + outputStream.write(postData) + outputStream.flush() + } + + val reader = BufferedReader(InputStreamReader(inputStream)) + val output = reader.readText() + + + println("\nSent 'GET' request to URL : $url; Response Code : $responseCode") + println(output) + return output + } + catch (ex:Exception){ + println(ex.message) + return "Error!" + } + } + } + + fun getFolderList(folder:String):FolderList{ + val response = dropboxApi("https://api.dropboxapi.com/2/files/list_folder", + "{\"path\":\"$folder\"}") + return GameSaver().json().fromJson(FolderList::class.java,response) + } + + fun getFileInfo(fileName:String):String{ + val response = dropboxApi("https://content.dropboxapi.com/2/files/download", + "{\"path\":\"$fileName\"}",true) + return response + } + + class FolderList{ + var entries = ArrayList() + } + + class FolderListEntry{ + var name="" + var path_display="" + } + +} \ No newline at end of file diff --git a/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenOptionsTable.kt b/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenOptionsTable.kt index eb90e38931..aa88ae25cc 100644 --- a/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenOptionsTable.kt +++ b/core/src/com/unciv/ui/worldscreen/optionstable/WorldScreenOptionsTable.kt @@ -96,6 +96,15 @@ class WorldScreenOptionsTable(screen:WorldScreen) : PopupTable(screen){ scrollPane.setScrollingDisabled(true,false) add(scrollPane).maxHeight(screen.stage.height*0.6f).row() +/* + addButton("Dropbox Testing"){ +// val folderList = DropBox().getFolderList("/Maps") +// for(folder in folderList.entries) +// print(folder.name) + DropBox().getFileInfo("/Maps/China starts on tundra") + } + */ + addButton("Close"){ remove() } pack() // Needed to show the background. @@ -251,4 +260,5 @@ class WorldScreenOptionsTable(screen:WorldScreen) : PopupTable(screen){ } super.draw(batch, parentAlpha) } -} \ No newline at end of file +} +