Skip to content

Commit

Permalink
http service request refactoring & fix detekt analyze report
Browse files Browse the repository at this point in the history
  • Loading branch information
p-vorobyev committed Dec 1, 2024
1 parent 6c4cb14 commit 402cc8c
Show file tree
Hide file tree
Showing 24 changed files with 82 additions and 57 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
}

group = "dev.voroby"
version = "1.0.0"
version = "1.0.1-SNAPSHOT"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -15,7 +17,7 @@ class HttpTelegramGateway(
private val gatewayHost: String?
): TelegramGateway {

private val httpService by lazy { HttpService(protocol) }
private val httpService: Service<Http.Request<*>> by lazy { HttpService(protocol) }

private val checkSendAbilityService by lazy { CheckSendAbilityService(httpService) }

Expand Down Expand Up @@ -53,4 +55,4 @@ class HttpTelegramGateway(
override fun close() {
protocol.close()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@ interface TelegramGateway : AutoCloseable {
is HttpProtocol -> HttpTelegramGateway(protocol, accessToken, gatewayHost)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,9 @@ data class DeliveryStatus(
@JsonProperty("updated_at") val updatedAt: Int
) {

enum class Status { sent, read, revoked }
}
enum class Status {
@JsonProperty("sent") Sent,
@JsonProperty("read") Read,
@JsonProperty("revoked") Revoked
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ sealed interface Response {
data class Success(val result: RequestStatus) : Response

data class Error(val error: String) : Response
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ class HttpProtocol(

const val TLS_PROTOCOL = "TLSv1.2"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ sealed interface Protocol<T> : AutoCloseable {
httpClientBuilder: HttpClient.Builder? = null
): Protocol<HttpRequest> = HttpProtocol(httpClientBuilder)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ data class ProtocolResponse(
val ok: Boolean,
val result: RequestStatus? = null,
val error: String = ""
)
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ class TrustAllManager: X509TrustManager {
override fun getAcceptedIssuers(): Array<X509Certificate> {
return emptyArray()
}
}
}
11 changes: 11 additions & 0 deletions src/main/kotlin/dev/voroby/telegram/gateway/common/service/Http.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package dev.voroby.telegram.gateway.common.service

object Http {

data class Request<Payload>(
val body: Payload,
val serviceName: String,
val accessToken: String,
val host: String?
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<HttpRequest>) : Service<Http.Request<*>> {

suspend fun <Payload> execute(
body: Payload,
serviceName: String,
accessToken: String,
host: String?
): Either<Throwable, Response> = 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<Throwable, Response> = 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 <Payload> httpRequest(
Expand All @@ -43,4 +40,4 @@ class HttpService(private val protocol: HttpProtocol) {

const val NO_RESULT_MESSAGE = "There is no result from the server"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ import dev.voroby.telegram.gateway.common.domain.Response
interface Service<T> {

suspend operator fun invoke(request: T): Either<Throwable, Response>
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ object ServiceName {
const val SEND_VERIFICATION_MESSAGE = "sendVerificationMessage"
const val CHECK_SEND_ABILITY = "checkSendAbility"
const val CHECK_VERIFICATION_STATUS = "checkVerificationStatus"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ object CheckSendAbility {
val gatewayHost: String? = null,
val payload: Request
)
}
}
Original file line number Diff line number Diff line change
@@ -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<ExtendedRequest> {
class CheckSendAbilityService(
private val httpService: Service<Http.Request<*>>
) : Service<ExtendedRequest> {

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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ object CheckVerificationStatus {
val gatewayHost: String? = null,
val payload: Request
)
}
}
Original file line number Diff line number Diff line change
@@ -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<ExtendedRequest> {
class CheckVerificationStatusService(
private val httpService: Service<Http.Request<*>>
) : Service<ExtendedRequest> {

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)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ object SendVerificationMessage {
val gatewayHost: String? = null,
val payload: Request
)
}
}
Original file line number Diff line number Diff line change
@@ -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<ExtendedRequest> {
class SendVerificationMessageService(
private val httpService: Service<Http.Request<*>>
) : Service<ExtendedRequest> {

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)
}
}
}
9 changes: 3 additions & 6 deletions src/main/kotlin/dev/voroby/telegram/gateway/util/Http.kt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package dev.voroby.telegram.gateway.util

class Http {
object Http {

companion object {

const val DEFAULT_GATEWAY_HOST = "gatewayapi.telegram.org"
}
}
const val DEFAULT_GATEWAY_HOST = "gatewayapi.telegram.org"
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper

object ObjectMapper {

val objectMapper by lazy { jacksonObjectMapper() }
val mapper by lazy { jacksonObjectMapper() }

fun <T> toJson(obj: T): String = objectMapper.writeValueAsString(obj)
fun <T> toJson(obj: T): String = mapper.writeValueAsString(obj)

inline fun <reified T> fromJson(json: String): T = objectMapper.readValue(json, T::class.java)
}
inline fun <reified T> fromJson(json: String): T = mapper.readValue(json, T::class.java)
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ class IntegrationTests : FunSpec({
response.isRight().shouldBeTrue()
response.onRight { onProtocolSuccess(it) }
}
})
})

0 comments on commit 402cc8c

Please sign in to comment.