Last active
September 2, 2023 16:02
-
-
Save devqmr/1569b8af48eadb40a674324077fe5da9 to your computer and use it in GitHub Desktop.
Create Github Gist direct from mobile app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun sendImageToGIST(fileName:String, content: String) { | |
val contentJSON = JSONObject() | |
contentJSON.put("content", content) | |
val fileJSON = JSONObject() | |
fileJSON.put("${fileName}_${System.currentTimeMillis()}.json", contentJSON) | |
val dataJsonObject = JSONObject() | |
dataJsonObject.put("description", "Created via API 3 FROM Android App") | |
dataJsonObject.put("public", true) | |
dataJsonObject.put("files", fileJSON) | |
TelegramBot.createNewGistByOkHttp(dataJsonObject.toString()) | |
} | |
private fun createNewGistByOkHttp(data: String) { | |
val client = OkHttpClient.Builder() | |
.connectTimeout(20, TimeUnit.SECONDS) // connect timeout | |
.writeTimeout(20, TimeUnit.SECONDS) // socket timeout | |
.readTimeout(20, TimeUnit.SECONDS) // socket timeout | |
.build() | |
val JSON = MediaType.get("application/json; charset=utf-8") | |
val body = RequestBody.create(JSON, data) | |
val request: Request = Request.Builder() | |
.url("https://api.github.com/gists") | |
.header("Authorization","token #your_token#") | |
.post(body) | |
.build() | |
val call: Call = client.newCall(request) | |
call.enqueue(object : Callback { | |
@Throws(IOException::class) | |
override fun onResponse(call: Call?, response: Response?) { | |
Timber.d("onResponse: [call > $call]") | |
Timber.d("onResponse: [response > $response]") | |
} | |
override fun onFailure(call: Call?, e: IOException?) { | |
e?.printStackTrace() | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment