diff --git a/build.gradle.kts b/build.gradle.kts index 33efeef..e871767 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -5,7 +5,7 @@ plugins { } group = "dev.voroby" -version = "1.0.0" +version = "1.0.1-SNAPSHOT" repositories { mavenCentral() diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/HttpTelegramGateway.kt b/src/main/kotlin/dev/voroby/telegram/gateway/HttpTelegramGateway.kt index fb05b15..d08085d 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/HttpTelegramGateway.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/HttpTelegramGateway.kt @@ -1,7 +1,9 @@ package dev.voroby.telegram.gateway import dev.voroby.telegram.gateway.common.infrastructure.HttpProtocol +import dev.voroby.telegram.gateway.common.service.Http import dev.voroby.telegram.gateway.common.service.HttpService +import dev.voroby.telegram.gateway.common.service.Service import dev.voroby.telegram.gateway.service.checkSendAbility.CheckSendAbility import dev.voroby.telegram.gateway.service.checkSendAbility.CheckSendAbilityService import dev.voroby.telegram.gateway.service.checkVerificationStatus.CheckVerificationStatus @@ -15,7 +17,7 @@ class HttpTelegramGateway( private val gatewayHost: String? ): TelegramGateway { - private val httpService by lazy { HttpService(protocol) } + private val httpService: Service> by lazy { HttpService(protocol) } private val checkSendAbilityService by lazy { CheckSendAbilityService(httpService) } @@ -53,4 +55,4 @@ class HttpTelegramGateway( override fun close() { protocol.close() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/TelegramGateway.kt b/src/main/kotlin/dev/voroby/telegram/gateway/TelegramGateway.kt index 132785e..ca30791 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/TelegramGateway.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/TelegramGateway.kt @@ -27,4 +27,4 @@ interface TelegramGateway : AutoCloseable { is HttpProtocol -> HttpTelegramGateway(protocol, accessToken, gatewayHost) } } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/DeliveryStatus.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/DeliveryStatus.kt index 95848cd..6e4535b 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/DeliveryStatus.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/DeliveryStatus.kt @@ -7,5 +7,9 @@ data class DeliveryStatus( @JsonProperty("updated_at") val updatedAt: Int ) { - enum class Status { sent, read, revoked } -} \ No newline at end of file + enum class Status { + @JsonProperty("sent") Sent, + @JsonProperty("read") Read, + @JsonProperty("revoked") Revoked + } +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/RequestStatus.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/RequestStatus.kt index 542f576..d14fc2a 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/RequestStatus.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/RequestStatus.kt @@ -10,4 +10,4 @@ data class RequestStatus( @JsonProperty("delivery_status") val deliveryStatus: DeliveryStatus? = null, @JsonProperty("verification_status") val verificationStatus: VerificationStatus? = null, val payload: String? = null -) \ No newline at end of file +) diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/Response.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/Response.kt index 3d54552..81f3363 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/Response.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/Response.kt @@ -5,4 +5,4 @@ sealed interface Response { data class Success(val result: RequestStatus) : Response data class Error(val error: String) : Response -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/VerificationStatus.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/VerificationStatus.kt index 29e046a..cfb8a71 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/VerificationStatus.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/domain/VerificationStatus.kt @@ -8,5 +8,10 @@ data class VerificationStatus( @JsonProperty("code_entered") val codeEntered: String? = null, ) { - enum class Status { code_valid, code_invalid, code_max_attempts_exceeded, expired } + enum class Status { + @JsonProperty("code_valid") CodeValid, + @JsonProperty("code_invalid") CodeInvalid, + @JsonProperty("code_max_attempts_exceeded") CodeMaxAttemptsExceeded, + @JsonProperty("expired") Expired + } } diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/HttpProtocol.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/HttpProtocol.kt index dfd722e..ed9a2b8 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/HttpProtocol.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/HttpProtocol.kt @@ -36,4 +36,4 @@ class HttpProtocol( const val TLS_PROTOCOL = "TLSv1.2" } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/Protocol.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/Protocol.kt index 666e094..f58cf8c 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/Protocol.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/Protocol.kt @@ -13,4 +13,4 @@ sealed interface Protocol : AutoCloseable { httpClientBuilder: HttpClient.Builder? = null ): Protocol = HttpProtocol(httpClientBuilder) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/ProtocolResponse.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/ProtocolResponse.kt index 8614eb2..cd56c19 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/ProtocolResponse.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/ProtocolResponse.kt @@ -6,4 +6,4 @@ data class ProtocolResponse( val ok: Boolean, val result: RequestStatus? = null, val error: String = "" -) \ No newline at end of file +) diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/TrustAllManager.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/TrustAllManager.kt index d7cd591..78173a6 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/TrustAllManager.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/infrastructure/TrustAllManager.kt @@ -16,4 +16,4 @@ class TrustAllManager: X509TrustManager { override fun getAcceptedIssuers(): Array { return emptyArray() } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/service/Http.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/service/Http.kt new file mode 100644 index 0000000..a44ea8f --- /dev/null +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/service/Http.kt @@ -0,0 +1,11 @@ +package dev.voroby.telegram.gateway.common.service + +object Http { + + data class Request( + val body: Payload, + val serviceName: String, + val accessToken: String, + val host: String? + ) +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/service/HttpService.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/service/HttpService.kt index 59f7358..5ec7a44 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/service/HttpService.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/service/HttpService.kt @@ -5,27 +5,24 @@ import arrow.core.Either.Companion.catch import dev.voroby.telegram.gateway.common.domain.Response import dev.voroby.telegram.gateway.common.domain.Response.Error import dev.voroby.telegram.gateway.common.domain.Response.Success -import dev.voroby.telegram.gateway.common.infrastructure.HttpProtocol -import dev.voroby.telegram.gateway.util.Http.Companion.DEFAULT_GATEWAY_HOST +import dev.voroby.telegram.gateway.common.infrastructure.Protocol +import dev.voroby.telegram.gateway.util.Http.DEFAULT_GATEWAY_HOST import dev.voroby.telegram.gateway.util.ObjectMapper.toJson import java.net.URI import java.net.http.HttpRequest import java.net.http.HttpRequest.BodyPublishers.ofString -class HttpService(private val protocol: HttpProtocol) { +class HttpService(private val protocol: Protocol) : Service> { - suspend fun execute( - body: Payload, - serviceName: String, - accessToken: String, - host: String? - ): Either = catch { - val request = httpRequest(body, serviceName, accessToken, host) - val baseResponse = protocol(request) - if (baseResponse.ok) - baseResponse.result?.let { Success(it) } ?: Error(NO_RESULT_MESSAGE) - else - Error(baseResponse.error) + override suspend fun invoke(request: Http.Request<*>): Either = with(request) { + catch { + val protocolRequest = httpRequest(body, serviceName, accessToken, host) + val baseResponse = protocol(protocolRequest) + if (baseResponse.ok) + baseResponse.result?.let { Success(it) } ?: Error(NO_RESULT_MESSAGE) + else + Error(baseResponse.error) + } } private fun httpRequest( @@ -43,4 +40,4 @@ class HttpService(private val protocol: HttpProtocol) { const val NO_RESULT_MESSAGE = "There is no result from the server" } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/common/service/Service.kt b/src/main/kotlin/dev/voroby/telegram/gateway/common/service/Service.kt index aa96ae5..bab6bcd 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/common/service/Service.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/common/service/Service.kt @@ -6,4 +6,4 @@ import dev.voroby.telegram.gateway.common.domain.Response interface Service { suspend operator fun invoke(request: T): Either -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/service/ServiceName.kt b/src/main/kotlin/dev/voroby/telegram/gateway/service/ServiceName.kt index db151dd..adf3c8c 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/service/ServiceName.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/service/ServiceName.kt @@ -5,4 +5,4 @@ object ServiceName { const val SEND_VERIFICATION_MESSAGE = "sendVerificationMessage" const val CHECK_SEND_ABILITY = "checkSendAbility" const val CHECK_VERIFICATION_STATUS = "checkVerificationStatus" -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/service/checkSendAbility/CheckSendAbility.kt b/src/main/kotlin/dev/voroby/telegram/gateway/service/checkSendAbility/CheckSendAbility.kt index 728631e..d02d966 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/service/checkSendAbility/CheckSendAbility.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/service/checkSendAbility/CheckSendAbility.kt @@ -11,4 +11,4 @@ object CheckSendAbility { val gatewayHost: String? = null, val payload: Request ) -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/service/checkSendAbility/CheckSendAbilityService.kt b/src/main/kotlin/dev/voroby/telegram/gateway/service/checkSendAbility/CheckSendAbilityService.kt index 6a220a0..78d95c3 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/service/checkSendAbility/CheckSendAbilityService.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/service/checkSendAbility/CheckSendAbilityService.kt @@ -1,18 +1,21 @@ package dev.voroby.telegram.gateway.service.checkSendAbility -import dev.voroby.telegram.gateway.common.service.HttpService +import dev.voroby.telegram.gateway.common.service.Http import dev.voroby.telegram.gateway.common.service.Service import dev.voroby.telegram.gateway.service.ServiceName.CHECK_SEND_ABILITY import dev.voroby.telegram.gateway.service.checkSendAbility.CheckSendAbility.ExtendedRequest -class CheckSendAbilityService(private val httpService: HttpService) : Service { +class CheckSendAbilityService( + private val httpService: Service> +) : Service { override suspend fun invoke(request: ExtendedRequest) = with(request) { - httpService.execute( + val httpRequest = Http.Request( body = payload, serviceName = CHECK_SEND_ABILITY, accessToken = accessToken, host = gatewayHost ) + httpService.invoke(httpRequest) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/service/checkVerificationStatus/CheckVerificationStatus.kt b/src/main/kotlin/dev/voroby/telegram/gateway/service/checkVerificationStatus/CheckVerificationStatus.kt index 165e421..4cad06c 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/service/checkVerificationStatus/CheckVerificationStatus.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/service/checkVerificationStatus/CheckVerificationStatus.kt @@ -17,4 +17,4 @@ object CheckVerificationStatus { val gatewayHost: String? = null, val payload: Request ) -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/service/checkVerificationStatus/CheckVerificationStatusService.kt b/src/main/kotlin/dev/voroby/telegram/gateway/service/checkVerificationStatus/CheckVerificationStatusService.kt index 2b4f0dd..ce5df69 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/service/checkVerificationStatus/CheckVerificationStatusService.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/service/checkVerificationStatus/CheckVerificationStatusService.kt @@ -1,18 +1,21 @@ package dev.voroby.telegram.gateway.service.checkVerificationStatus -import dev.voroby.telegram.gateway.common.service.HttpService +import dev.voroby.telegram.gateway.common.service.Http import dev.voroby.telegram.gateway.common.service.Service import dev.voroby.telegram.gateway.service.ServiceName.CHECK_VERIFICATION_STATUS import dev.voroby.telegram.gateway.service.checkVerificationStatus.CheckVerificationStatus.ExtendedRequest -class CheckVerificationStatusService(private val httpService: HttpService) : Service { +class CheckVerificationStatusService( + private val httpService: Service> +) : Service { override suspend fun invoke(request: ExtendedRequest) = with(request) { - httpService.execute( + val httpRequest = Http.Request( body = payload, serviceName = CHECK_VERIFICATION_STATUS, accessToken = accessToken, host = gatewayHost ) + httpService.invoke(httpRequest) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/service/sendVerificationMessage/SendVerificationMessage.kt b/src/main/kotlin/dev/voroby/telegram/gateway/service/sendVerificationMessage/SendVerificationMessage.kt index 709cef5..3976ef1 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/service/sendVerificationMessage/SendVerificationMessage.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/service/sendVerificationMessage/SendVerificationMessage.kt @@ -28,4 +28,4 @@ object SendVerificationMessage { val gatewayHost: String? = null, val payload: Request ) -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/service/sendVerificationMessage/SendVerificationMessageService.kt b/src/main/kotlin/dev/voroby/telegram/gateway/service/sendVerificationMessage/SendVerificationMessageService.kt index 98db089..4e5dcbf 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/service/sendVerificationMessage/SendVerificationMessageService.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/service/sendVerificationMessage/SendVerificationMessageService.kt @@ -1,18 +1,21 @@ package dev.voroby.telegram.gateway.service.sendVerificationMessage -import dev.voroby.telegram.gateway.common.service.HttpService +import dev.voroby.telegram.gateway.common.service.Http import dev.voroby.telegram.gateway.common.service.Service import dev.voroby.telegram.gateway.service.ServiceName.SEND_VERIFICATION_MESSAGE import dev.voroby.telegram.gateway.service.sendVerificationMessage.SendVerificationMessage.ExtendedRequest -class SendVerificationMessageService(private val httpService: HttpService) : Service { +class SendVerificationMessageService( + private val httpService: Service> +) : Service { override suspend fun invoke(request: ExtendedRequest) = with(request) { - httpService.execute( + val httpRequest = Http.Request( body = payload, serviceName = SEND_VERIFICATION_MESSAGE, accessToken = accessToken, host = gatewayHost ) + httpService.invoke(httpRequest) } -} \ No newline at end of file +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/util/Http.kt b/src/main/kotlin/dev/voroby/telegram/gateway/util/Http.kt index 40c0e0b..aac27d0 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/util/Http.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/util/Http.kt @@ -1,9 +1,6 @@ package dev.voroby.telegram.gateway.util -class Http { +object Http { - companion object { - - const val DEFAULT_GATEWAY_HOST = "gatewayapi.telegram.org" - } -} \ No newline at end of file + const val DEFAULT_GATEWAY_HOST = "gatewayapi.telegram.org" +} diff --git a/src/main/kotlin/dev/voroby/telegram/gateway/util/ObjectMapper.kt b/src/main/kotlin/dev/voroby/telegram/gateway/util/ObjectMapper.kt index 45839c4..8a4929c 100644 --- a/src/main/kotlin/dev/voroby/telegram/gateway/util/ObjectMapper.kt +++ b/src/main/kotlin/dev/voroby/telegram/gateway/util/ObjectMapper.kt @@ -4,9 +4,9 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper object ObjectMapper { - val objectMapper by lazy { jacksonObjectMapper() } + val mapper by lazy { jacksonObjectMapper() } - fun toJson(obj: T): String = objectMapper.writeValueAsString(obj) + fun toJson(obj: T): String = mapper.writeValueAsString(obj) - inline fun fromJson(json: String): T = objectMapper.readValue(json, T::class.java) -} \ No newline at end of file + inline fun fromJson(json: String): T = mapper.readValue(json, T::class.java) +} diff --git a/src/test/kotlin/dev/voroby/telegram/gateway/IntegrationTests.kt b/src/test/kotlin/dev/voroby/telegram/gateway/IntegrationTests.kt index 19d4c65..a0ff60b 100644 --- a/src/test/kotlin/dev/voroby/telegram/gateway/IntegrationTests.kt +++ b/src/test/kotlin/dev/voroby/telegram/gateway/IntegrationTests.kt @@ -28,4 +28,4 @@ class IntegrationTests : FunSpec({ response.isRight().shouldBeTrue() response.onRight { onProtocolSuccess(it) } } -}) \ No newline at end of file +})