mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-29 22:29:15 +07:00
Added initial dropbox tests - get folder contents and download file
This commit is contained in:
70
core/src/com/unciv/ui/worldscreen/optionstable/DropBox.kt
Normal file
70
core/src/com/unciv/ui/worldscreen/optionstable/DropBox.kt
Normal file
@ -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<FolderListEntry>()
|
||||
}
|
||||
|
||||
class FolderListEntry{
|
||||
var name=""
|
||||
var path_display=""
|
||||
}
|
||||
|
||||
}
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user