Skip to content

Instantly share code, notes, and snippets.

@devqmr
Last active September 2, 2023 16:02
Show Gist options
  • Save devqmr/1569b8af48eadb40a674324077fe5da9 to your computer and use it in GitHub Desktop.
Save devqmr/1569b8af48eadb40a674324077fe5da9 to your computer and use it in GitHub Desktop.
Create Github Gist direct from mobile app
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