Skip to content

Commit

Permalink
feat: add user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
finalchild committed Sep 6, 2024
1 parent 6c24f51 commit 1e979f8
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.portone.openapi

import com.squareup.kotlinpoet.FileSpec
import com.squareup.kotlinpoet.KModifier
import com.squareup.kotlinpoet.PropertySpec
import org.gradle.api.DefaultTask
import org.gradle.api.file.DirectoryProperty
import org.gradle.api.provider.Property
import org.gradle.api.tasks.Input
import org.gradle.api.tasks.OutputDirectory
import org.gradle.api.tasks.TaskAction

abstract class GenerateVersionCodeTask : DefaultTask() {
@get:Input
abstract val version: Property<String>

@get:OutputDirectory
abstract val outputDirectory: DirectoryProperty

@TaskAction
fun generateVersionCode() {
val outputDirectory = this.outputDirectory.get().asFile.toPath()

FileSpec.builder("io.portone.sdk.server", "Version")
.addProperty(PropertySpec.builder("SDK_VERSION", String::class, KModifier.CONST, KModifier.INTERNAL).initializer("%S", version.get()).build())
.build().writeTo(outputDirectory)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ class SchemaGenerator(private val document: JsonObject, private val includePrefi
.endControlFlow()
funBuilder.addStatement("contentType(ContentType.Application.Json)")
funBuilder.addStatement("accept(ContentType.Application.Json)")
funBuilder.addStatement("""userAgent("portone-server-sdk-jvm/${'$'}{SDK_VERSION}")""")

if (operation.method.uppercase() in listOf("POST", "PUT", "PATCH")) {
if (operation.bodyParams.isNotEmpty()) {
Expand Down Expand Up @@ -752,7 +753,7 @@ class SchemaGenerator(private val document: JsonObject, private val includePrefi
FileSpec.builder(clientClass)
.addImport("io.ktor.client.request", "get", "post", "delete", "patch", "headers", "accept", "setBody")
.addImport("io.ktor.client.call", "body")
.addImport("io.ktor.http", "appendPathSegments", "HttpHeaders", "ContentType", "contentType")
.addImport("io.ktor.http", "appendPathSegments", "HttpHeaders", "ContentType", "contentType", "userAgent")
.addImport("kotlinx.serialization", "encodeToString")
.addImport("kotlinx.serialization.json", "Json")
.addImport("kotlinx.coroutines", "GlobalScope")
Expand Down
26 changes: 21 additions & 5 deletions common/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import com.vanniktech.maven.publish.SonatypeHost
import io.portone.openapi.GenerateSchemaCodeTask
import io.portone.openapi.GenerateVersionCodeTask
import io.portone.openapi.PatchCodeTask
import io.portone.openapi.SavePatchTask
import org.jetbrains.dokka.gradle.DokkaTask
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.dsl.jvm.JvmTargetValidationMode
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile
import java.io.ByteArrayOutputStream
import java.net.URI
import java.nio.charset.StandardCharsets

plugins {
`java-library`
Expand All @@ -23,12 +26,23 @@ plugins {
group = "io.portone"
description = "JVM library for integrating PortOne payment infrastructure."

val githubRef = System.getenv("GITHUB_REF")
version =
if (githubRef != null && githubRef.startsWith("refs/tags/v")) {
githubRef.substring("refs/tags/v".length)
} else {
"0.0.1-SNAPSHOT"
run {
val output = ByteArrayOutputStream()
exec {
workingDir = rootProject.projectDir
executable = "git"
args = listOf("describe", "--dirty", "--tags", "--match", "v*", "--first-parent")
standardOutput = output
}
output.toString(StandardCharsets.UTF_8).trimEnd('\n', '\r').substring(1)
}

val generateVersionCode =
tasks.register<GenerateVersionCodeTask>("generateVersionCode") {
version = project.version.toString()
outputDirectory = layout.buildDirectory.dir("generated/sources/versionCode/kotlin/main")
outputs.upToDateWhen { false }
}

val generateSchemaCode =
Expand All @@ -42,6 +56,7 @@ val patchCode =
originDirectory = generateSchemaCode.flatMap { it.outputDirectory }
patchesDirectory = file("patches")
outputDirectory.set(layout.buildDirectory.dir("generated/sources/patchedCode/kotlin/main"))
outputs.upToDateWhen { false }
}

val savePatch =
Expand All @@ -54,6 +69,7 @@ val savePatch =
sourceSets {
main {
kotlin {
srcDir(generateVersionCode)
srcDir(patchCode)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.ktor.http.ContentType
import io.ktor.http.HttpHeaders
import io.ktor.http.appendPathSegments
import io.ktor.http.contentType
import io.ktor.http.userAgent
import io.portone.sdk.server.schemas.AlreadyPaidError
import io.portone.sdk.server.schemas.AlreadyPaidException
import io.portone.sdk.server.schemas.AlreadyPaidOrWaitingError
Expand Down Expand Up @@ -241,6 +242,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -317,6 +319,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(SendIdentityVerificationBody(storeId = storeId,
channelKey = channelKey, customer = customer, customData = customData, bypass = bypass,
operator = operator, method = method)))
Expand Down Expand Up @@ -391,6 +394,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(ConfirmIdentityVerificationBody(storeId = storeId,
otp = otp)))
}
Expand Down Expand Up @@ -461,6 +465,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody("{}")
}
if (httpResponse.status.value !in 200..299) {
Expand Down Expand Up @@ -527,6 +532,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(PreRegisterPaymentBody(storeId = storeId,
totalAmount = totalAmount, taxFreeAmount = taxFreeAmount, currency = currency)))
}
Expand Down Expand Up @@ -585,6 +591,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -649,6 +656,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -725,6 +733,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -800,6 +809,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(IssueBillingKeyBody(storeId = storeId, method =
method, channelKey = channelKey, channelGroupId = channelGroupId, customer = customer,
customData = customData, bypass = bypass, noticeUrls = noticeUrls)))
Expand Down Expand Up @@ -874,6 +884,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -933,6 +944,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -992,6 +1004,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -1058,6 +1071,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -1120,6 +1134,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -1187,6 +1202,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -1255,6 +1271,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -1332,6 +1349,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(CreatePaymentScheduleBody(payment = payment,
timeToPay = timeToPay)))
}
Expand Down Expand Up @@ -1431,6 +1449,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(CancelPaymentBody(storeId = storeId, amount =
amount, taxFreeAmount = taxFreeAmount, vatAmount = vatAmount, reason = reason, requester =
requester, currentCancellableAmount = currentCancellableAmount, refundAccount =
Expand Down Expand Up @@ -1570,6 +1589,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(BillingKeyPaymentInput(storeId = storeId,
billingKey = billingKey, channelKey = channelKey, orderName = orderName, customer =
customer, customData = customData, amount = amount, currency = currency, installmentMonth
Expand Down Expand Up @@ -1713,6 +1733,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(InstantPaymentInput(storeId = storeId, channelKey
= channelKey, channelGroupId = channelGroupId, method = method, orderName = orderName,
isCulturalExpense = isCulturalExpense, isEscrow = isEscrow, customer = customer,
Expand Down Expand Up @@ -1823,6 +1844,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(IssueCashReceiptBody(storeId = storeId, paymentId
= paymentId, channelKey = channelKey, type = type, orderName = orderName, currency =
currency, amount = amount, productType = productType, customer = customer, paidAt =
Expand Down Expand Up @@ -1901,6 +1923,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody("{}")
}
if (httpResponse.status.value !in 200..299) {
Expand Down Expand Up @@ -1968,6 +1991,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody("{}")
}
if (httpResponse.status.value !in 200..299) {
Expand Down Expand Up @@ -2045,6 +2069,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(RegisterEscrowLogisticsBody(storeId = storeId,
sender = sender, receiver = receiver, logistics = logistics, sendEmail = sendEmail,
products = products)))
Expand Down Expand Up @@ -2129,6 +2154,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(ModifyEscrowLogisticsBody(storeId = storeId,
sender = sender, receiver = receiver, logistics = logistics, sendEmail = sendEmail,
products = products)))
Expand Down Expand Up @@ -2204,6 +2230,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(ConfirmEscrowBody(storeId = storeId, fromStore =
fromStore)))
}
Expand Down Expand Up @@ -2270,6 +2297,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(ResendWebhookBody(storeId = storeId, webhookId =
webhookId)))
}
Expand Down Expand Up @@ -2333,6 +2361,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
}
if (httpResponse.status.value !in 200..299) {
val httpBody = httpResponse.body<String>()
Expand Down Expand Up @@ -2395,6 +2424,7 @@ public class PortOneApi(
}
contentType(ContentType.Application.Json)
accept(ContentType.Application.Json)
userAgent("portone-server-sdk-jvm/${SDK_VERSION}")
setBody(this@PortOneApi.json.encodeToString(RegisterStoreReceiptBody(items = items, storeId =
storeId)))
}
Expand Down
3 changes: 3 additions & 0 deletions common/src/main/java/module-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
module io.portone.sdk.server {
requires kotlinx.coroutines.core;
requires kotlinx.serialization.json;
requires io.ktor.client.core;
requires io.ktor.client.okhttp;
requires io.ktor.http;

exports io.portone.sdk.server;
exports io.portone.sdk.server.schemas;
Expand Down

0 comments on commit 1e979f8

Please sign in to comment.