From a1cd615e7cac8ca11e629dee646bb97aa3e29d40 Mon Sep 17 00:00:00 2001 From: Ziedelth Date: Thu, 28 Sep 2023 16:50:57 +0200 Subject: [PATCH] Add Authorization header to API requests --- src/main/kotlin/fr/jais/scraper/utils/API.kt | 9 +++++++-- src/main/kotlin/fr/jais/scraper/utils/Const.kt | 1 + 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/src/main/kotlin/fr/jais/scraper/utils/API.kt b/src/main/kotlin/fr/jais/scraper/utils/API.kt index bcb2ed8..211cdd9 100644 --- a/src/main/kotlin/fr/jais/scraper/utils/API.kt +++ b/src/main/kotlin/fr/jais/scraper/utils/API.kt @@ -20,10 +20,15 @@ object API { } private fun post(url: String, json: String): HttpResponse { - val request = HttpRequest.newBuilder() + val requestBuilder = HttpRequest.newBuilder() .uri(URI.create(url)) .header("Content-Type", "application/json") - .POST(HttpRequest.BodyPublishers.ofString(json)) + + if (!Const.secureKey.isNullOrBlank()) { + requestBuilder.header("Authorization", Const.secureKey) + } + + val request = requestBuilder.POST(HttpRequest.BodyPublishers.ofString(json)) .build() return Const.httpClient.send(request, HttpResponse.BodyHandlers.ofString()) } diff --git a/src/main/kotlin/fr/jais/scraper/utils/Const.kt b/src/main/kotlin/fr/jais/scraper/utils/Const.kt index 3f62e62..5ffd206 100644 --- a/src/main/kotlin/fr/jais/scraper/utils/Const.kt +++ b/src/main/kotlin/fr/jais/scraper/utils/Const.kt @@ -18,4 +18,5 @@ object Const { val multipleSpaceRegex = "\\s+".toRegex() const val calendarBaseUrl = "https://anime.icotaku.com" val apiUrl = System.getenv("API_URL") ?: "http://localhost:8080/" + val secureKey: String? = System.getenv("SECURE_KEY") }