Skip to content

Commit

Permalink
Merge pull request #60 from mayorJAY/feat-user-agent-header
Browse files Browse the repository at this point in the history
Add User Agent Header
  • Loading branch information
Cliftonz authored Jan 2, 2024
2 parents 1c906c1 + 504e94b commit e96ae8c
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 9 deletions.
23 changes: 23 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.jetbrains.kotlin.konan.properties.Properties

plugins {
`java-library`
`maven-publish`
Expand Down Expand Up @@ -133,3 +135,24 @@ nexusStaging {
username = System.getenv("MAVEN_USERNAME")
password = System.getenv("MAVEN_PASSWORD")
}

val propsDir = "$buildDir/props"
sourceSets {
main {
kotlin {
output.dir(propsDir)
}
}
}

tasks.register("generateVersionProperty") {
val propertiesFile = file("$propsDir/version.properties")
propertiesFile.parentFile.mkdirs()
val properties = Properties()
properties.setProperty("version", "$version")
propertiesFile.writer().use { properties.store(it, null) }
}

tasks.named("processResources") {
dependsOn("generateVersionProperty")
}
36 changes: 27 additions & 9 deletions src/main/kotlin/helpers/RetrofitHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,44 @@ import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.Properties

class RetrofitHelper(
private val config: NovuConfig,
private val loggerLevel: HttpLoggingInterceptor.Level = HttpLoggingInterceptor.Level.BASIC
) {

private lateinit var retrofit: Retrofit

fun getInstance(): Retrofit {
val httpClient = OkHttpClient.Builder()
if (this::retrofit.isInitialized) {
return retrofit
}

httpClient
.addInterceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("Authorization", "ApiKey ${config.apiKey}")
.build()
chain.proceed(request)
}
val httpClient = OkHttpClient.Builder()
httpClient.addInterceptor { chain ->
val request = chain.request().newBuilder()
.addHeader("Authorization", "ApiKey ${config.apiKey}")
.addHeader("User-Agent", "novu/Kotlin@${retrieveProjectVersion()}")
.build()
chain.proceed(request)
}
.addInterceptor(HttpLoggingInterceptor().setLevel(loggerLevel))
val gson = GsonBuilder().setLenient().create()
return Retrofit.Builder().client(httpClient.build()).baseUrl(config.backendUrl)
retrofit = Retrofit.Builder()
.client(httpClient.build())
.baseUrl(config.backendUrl)
.addConverterFactory(GsonConverterFactory.create(gson))
.build()
return retrofit
}

private fun retrieveProjectVersion(): String {
val properties = Properties()
val inputStream = this.javaClass.getResourceAsStream("/version.properties") ?: return ""
inputStream.use {
properties.load(it)
return properties.getProperty("version") ?: ""
}
}
}
2 changes: 2 additions & 0 deletions src/test/kotlin/BlueprintsApiTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class BlueprintsApiTest {

assert(request.path == "/blueprints/group-by-category")
assert(request.method == "GET")
assert(request.headers["Authorization"] != null)
assert(request.headers["User-Agent"] != null)
assert(result == responseBody)
}

Expand Down

0 comments on commit e96ae8c

Please sign in to comment.