You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
TheCitadelofRicks/src/main/kotlin/org/mosad/thecitadelofricks/controller/CachetAPIController.kt

75 lines
2.7 KiB

/**
* TheCitadelofRicks
*
* Copyright 2019 <seil0@mosad.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*/
package org.mosad.thecitadelofricks.controller
import org.mosad.thecitadelofricks.controller.StatusController.Companion.getTotalRequests
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import java.io.BufferedReader
import java.io.IOException
import java.io.InputStreamReader
import java.net.HttpURLConnection
import java.net.URL
class CachetAPIController {
companion object {
private val logger: Logger = LoggerFactory.getLogger(CachetAPIController::class.java)
private var oldTotalRequests = 0
fun postTotalRequests() {
try {
val url = URL("${StartupController.getCachetBaseURL()}/api/v1/metrics/1/points")
val jsonInputString = "{\"value\": ${getTotalRequests() -oldTotalRequests}, \"timestamp\": \"${(System.currentTimeMillis() / 1000)}\"}"
oldTotalRequests = getTotalRequests()
val con = url.openConnection() as HttpURLConnection
con.requestMethod = "POST"
con.setRequestProperty("Content-Type", "application/json; utf-8")
con.setRequestProperty("Accept", "application/json")
con.setRequestProperty("X-Cachet-Token", StartupController.getCachetAPIKey())
con.doOutput = true
val os = con.outputStream
val input = jsonInputString.toByteArray(charset("utf-8"))
os.write(input, 0, input.size)
val br = BufferedReader(InputStreamReader(con.inputStream, "utf-8"))
val response = StringBuilder()
var responseLine: String?
while (br.readLine().also { responseLine = it } != null) {
response.append(responseLine!!.trim { it <= ' ' })
}
logger.info(response.toString())
} catch (e1: IOException) {
e1.printStackTrace()
}
}
}
}

Du besuchst diese Seite mit einem veralteten IPv4-Internetzugang. Möglicherweise treten in Zukunft Probleme mit der Erreichbarkeit und Performance auf. Bitte frage deinen Internetanbieter oder Netzwerkadministrator nach IPv6-Unterstützung.
You are visiting this site with an outdated IPv4 internet access. You may experience problems with accessibility and performance in the future. Please ask your ISP or network administrator for IPv6 support.
Weitere Infos | More Information
Klicke zum schließen | Click to close