From e6a7d202181ecef09f2faab3f90ff35602617f7b Mon Sep 17 00:00:00 2001 From: Michael Baumberger Date: Fri, 3 Jan 2025 17:56:39 +0100 Subject: [PATCH 1/4] Remove redundant FeignResponseCoderConfig classes because spring web is usen now and not webflux. The FeignResponseCoderConfig classes were removed from all services as they are no longer necessary. Feign configuration is now handled automatically or elsewhere, making these classes redundant and simplifying the codebase. --- .../security/FeignResponseCoderConfig.kt | 30 ------------------- .../security/FeignResponseCoderConfig.kt | 30 ------------------- .../security/FeignResponseCoderConfig.kt | 30 ------------------- .../security/FeignResponseCoderConfig.kt | 30 ------------------- .../security/FeignResponseCoderConfig.kt | 30 ------------------- .../security/FeignResponseCoderConfig.kt | 30 ------------------- .../security/FeignResponseCoderConfig.kt | 30 ------------------- .../security/FeignResponseCoderConfig.kt | 30 ------------------- 8 files changed, 240 deletions(-) delete mode 100644 admin-service/src/main/kotlin/com/michibaum/admin_service/security/FeignResponseCoderConfig.kt delete mode 100644 authentication-service/src/main/kotlin/com/michibaum/authentication_service/security/FeignResponseCoderConfig.kt delete mode 100644 chess-service/src/main/kotlin/com/michibaum/chess_service/security/FeignResponseCoderConfig.kt delete mode 100644 fitness-service/src/main/kotlin/com/michibaum/fitness_service/security/FeignResponseCoderConfig.kt delete mode 100644 music-service/src/main/kotlin/com/michibaum/music_service/security/FeignResponseCoderConfig.kt delete mode 100644 registry-service/src/main/kotlin/com/michibaum/registry_service/security/FeignResponseCoderConfig.kt delete mode 100644 usermanagement-service/src/main/kotlin/com/michibaum/usermanagement_service/security/FeignResponseCoderConfig.kt delete mode 100644 website-service/src/main/kotlin/com/michibaum/websiteservice/security/FeignResponseCoderConfig.kt diff --git a/admin-service/src/main/kotlin/com/michibaum/admin_service/security/FeignResponseCoderConfig.kt b/admin-service/src/main/kotlin/com/michibaum/admin_service/security/FeignResponseCoderConfig.kt deleted file mode 100644 index 4ace8794..00000000 --- a/admin-service/src/main/kotlin/com/michibaum/admin_service/security/FeignResponseCoderConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.michibaum.admin_service.security - -import feign.codec.Decoder -import feign.codec.Encoder -import org.springframework.beans.factory.ObjectFactory -import org.springframework.boot.autoconfigure.http.HttpMessageConverters -import org.springframework.cloud.openfeign.support.SpringDecoder -import org.springframework.cloud.openfeign.support.SpringEncoder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/** - * https://github.com/spring-cloud/spring-cloud-openfeign/issues/235 - * fanticat opened this issue on Oct 22, 2019 · 10 comments - */ -@Configuration -class FeignResponseCoderConfig { - - private val messageConverters = ObjectFactory { HttpMessageConverters() } - - @Bean - fun feignEncoder(): Encoder { - return SpringEncoder(messageConverters) - } - - @Bean - fun feignDecoder(): Decoder { - return SpringDecoder(messageConverters) - } -} diff --git a/authentication-service/src/main/kotlin/com/michibaum/authentication_service/security/FeignResponseCoderConfig.kt b/authentication-service/src/main/kotlin/com/michibaum/authentication_service/security/FeignResponseCoderConfig.kt deleted file mode 100644 index 5bb6ca01..00000000 --- a/authentication-service/src/main/kotlin/com/michibaum/authentication_service/security/FeignResponseCoderConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.michibaum.authentication_service.security - -import feign.codec.Decoder -import feign.codec.Encoder -import org.springframework.beans.factory.ObjectFactory -import org.springframework.boot.autoconfigure.http.HttpMessageConverters -import org.springframework.cloud.openfeign.support.SpringDecoder -import org.springframework.cloud.openfeign.support.SpringEncoder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/** - * https://github.com/spring-cloud/spring-cloud-openfeign/issues/235 - * fanticat opened this issue on Oct 22, 2019 · 10 comments - */ -@Configuration -class FeignResponseCoderConfig { - - private val messageConverters = ObjectFactory { HttpMessageConverters() } - - @Bean - fun feignEncoder(): Encoder { - return SpringEncoder(messageConverters) - } - - @Bean - fun feignDecoder(): Decoder { - return SpringDecoder(messageConverters) - } -} diff --git a/chess-service/src/main/kotlin/com/michibaum/chess_service/security/FeignResponseCoderConfig.kt b/chess-service/src/main/kotlin/com/michibaum/chess_service/security/FeignResponseCoderConfig.kt deleted file mode 100644 index eceef1e9..00000000 --- a/chess-service/src/main/kotlin/com/michibaum/chess_service/security/FeignResponseCoderConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.michibaum.chess_service.security - -import feign.codec.Decoder -import feign.codec.Encoder -import org.springframework.beans.factory.ObjectFactory -import org.springframework.boot.autoconfigure.http.HttpMessageConverters -import org.springframework.cloud.openfeign.support.SpringDecoder -import org.springframework.cloud.openfeign.support.SpringEncoder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/** - * https://github.com/spring-cloud/spring-cloud-openfeign/issues/235 - * fanticat opened this issue on Oct 22, 2019 · 10 comments - */ -@Configuration -class FeignResponseCoderConfig { - - private val messageConverters = ObjectFactory { HttpMessageConverters() } - - @Bean - fun feignEncoder(): Encoder { - return SpringEncoder(messageConverters) - } - - @Bean - fun feignDecoder(): Decoder { - return SpringDecoder(messageConverters) - } -} diff --git a/fitness-service/src/main/kotlin/com/michibaum/fitness_service/security/FeignResponseCoderConfig.kt b/fitness-service/src/main/kotlin/com/michibaum/fitness_service/security/FeignResponseCoderConfig.kt deleted file mode 100644 index d5d3d12c..00000000 --- a/fitness-service/src/main/kotlin/com/michibaum/fitness_service/security/FeignResponseCoderConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.michibaum.fitness_service.security - -import feign.codec.Decoder -import feign.codec.Encoder -import org.springframework.beans.factory.ObjectFactory -import org.springframework.boot.autoconfigure.http.HttpMessageConverters -import org.springframework.cloud.openfeign.support.SpringDecoder -import org.springframework.cloud.openfeign.support.SpringEncoder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/** - * https://github.com/spring-cloud/spring-cloud-openfeign/issues/235 - * fanticat opened this issue on Oct 22, 2019 · 10 comments - */ -@Configuration -class FeignResponseCoderConfig { - - private val messageConverters = ObjectFactory { HttpMessageConverters() } - - @Bean - fun feignEncoder(): Encoder { - return SpringEncoder(messageConverters) - } - - @Bean - fun feignDecoder(): Decoder { - return SpringDecoder(messageConverters) - } -} diff --git a/music-service/src/main/kotlin/com/michibaum/music_service/security/FeignResponseCoderConfig.kt b/music-service/src/main/kotlin/com/michibaum/music_service/security/FeignResponseCoderConfig.kt deleted file mode 100644 index fdb43edd..00000000 --- a/music-service/src/main/kotlin/com/michibaum/music_service/security/FeignResponseCoderConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.michibaum.music_service.security - -import feign.codec.Decoder -import feign.codec.Encoder -import org.springframework.beans.factory.ObjectFactory -import org.springframework.boot.autoconfigure.http.HttpMessageConverters -import org.springframework.cloud.openfeign.support.SpringDecoder -import org.springframework.cloud.openfeign.support.SpringEncoder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/** - * https://github.com/spring-cloud/spring-cloud-openfeign/issues/235 - * fanticat opened this issue on Oct 22, 2019 · 10 comments - */ -@Configuration -class FeignResponseCoderConfig { - - private val messageConverters = ObjectFactory { HttpMessageConverters() } - - @Bean - fun feignEncoder(): Encoder { - return SpringEncoder(messageConverters) - } - - @Bean - fun feignDecoder(): Decoder { - return SpringDecoder(messageConverters) - } -} diff --git a/registry-service/src/main/kotlin/com/michibaum/registry_service/security/FeignResponseCoderConfig.kt b/registry-service/src/main/kotlin/com/michibaum/registry_service/security/FeignResponseCoderConfig.kt deleted file mode 100644 index cbb082b9..00000000 --- a/registry-service/src/main/kotlin/com/michibaum/registry_service/security/FeignResponseCoderConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.michibaum.registry_service.security - -import feign.codec.Decoder -import feign.codec.Encoder -import org.springframework.beans.factory.ObjectFactory -import org.springframework.boot.autoconfigure.http.HttpMessageConverters -import org.springframework.cloud.openfeign.support.SpringDecoder -import org.springframework.cloud.openfeign.support.SpringEncoder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/** - * https://github.com/spring-cloud/spring-cloud-openfeign/issues/235 - * fanticat opened this issue on Oct 22, 2019 · 10 comments - */ -@Configuration -class FeignResponseCoderConfig { - - private val messageConverters = ObjectFactory { HttpMessageConverters() } - - @Bean - fun feignEncoder(): Encoder { - return SpringEncoder(messageConverters) - } - - @Bean - fun feignDecoder(): Decoder { - return SpringDecoder(messageConverters) - } -} diff --git a/usermanagement-service/src/main/kotlin/com/michibaum/usermanagement_service/security/FeignResponseCoderConfig.kt b/usermanagement-service/src/main/kotlin/com/michibaum/usermanagement_service/security/FeignResponseCoderConfig.kt deleted file mode 100644 index 79fdaef6..00000000 --- a/usermanagement-service/src/main/kotlin/com/michibaum/usermanagement_service/security/FeignResponseCoderConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.michibaum.usermanagement_service.security - -import feign.codec.Decoder -import feign.codec.Encoder -import org.springframework.beans.factory.ObjectFactory -import org.springframework.boot.autoconfigure.http.HttpMessageConverters -import org.springframework.cloud.openfeign.support.SpringDecoder -import org.springframework.cloud.openfeign.support.SpringEncoder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/** - * https://github.com/spring-cloud/spring-cloud-openfeign/issues/235 - * fanticat opened this issue on Oct 22, 2019 · 10 comments - */ -@Configuration -class FeignResponseCoderConfig { - - private val messageConverters = ObjectFactory { HttpMessageConverters() } - - @Bean - fun feignEncoder(): Encoder { - return SpringEncoder(messageConverters) - } - - @Bean - fun feignDecoder(): Decoder { - return SpringDecoder(messageConverters) - } -} diff --git a/website-service/src/main/kotlin/com/michibaum/websiteservice/security/FeignResponseCoderConfig.kt b/website-service/src/main/kotlin/com/michibaum/websiteservice/security/FeignResponseCoderConfig.kt deleted file mode 100644 index 828ff15c..00000000 --- a/website-service/src/main/kotlin/com/michibaum/websiteservice/security/FeignResponseCoderConfig.kt +++ /dev/null @@ -1,30 +0,0 @@ -package com.michibaum.websiteservice.security - -import feign.codec.Decoder -import feign.codec.Encoder -import org.springframework.beans.factory.ObjectFactory -import org.springframework.boot.autoconfigure.http.HttpMessageConverters -import org.springframework.cloud.openfeign.support.SpringDecoder -import org.springframework.cloud.openfeign.support.SpringEncoder -import org.springframework.context.annotation.Bean -import org.springframework.context.annotation.Configuration - -/** - * https://github.com/spring-cloud/spring-cloud-openfeign/issues/235 - * fanticat opened this issue on Oct 22, 2019 · 10 comments - */ -@Configuration -class FeignResponseCoderConfig { - - private val messageConverters = ObjectFactory { HttpMessageConverters() } - - @Bean - fun feignEncoder(): Encoder { - return SpringEncoder(messageConverters) - } - - @Bean - fun feignDecoder(): Decoder { - return SpringDecoder(messageConverters) - } -} From 96264dd5a102f52828ee1560e89eb6652ff22a3d Mon Sep 17 00:00:00 2001 From: Michael Baumberger Date: Fri, 3 Jan 2025 19:10:03 +0100 Subject: [PATCH 2/4] Remove SSL validation disabling from application configs The `httpclient.disable-ssl-validation` property was removed across multiple service configurations. This enhances security by enforcing proper SSL validation and preventing potential vulnerabilities. --- admin-service/src/main/resources/application.yml | 2 -- admin-service/src/test/resources/application.yml | 2 -- authentication-service/src/main/resources/application.yml | 2 -- chess-service/src/main/resources/application.yml | 2 -- fitness-service/src/main/resources/application.yml | 2 -- gateway-service/src/main/resources/application.yml | 2 -- music-service/src/main/resources/application.yml | 2 -- usermanagement-service/src/main/resources/application.yml | 2 -- 8 files changed, 16 deletions(-) diff --git a/admin-service/src/main/resources/application.yml b/admin-service/src/main/resources/application.yml index 3ce2c9b0..de8244be 100644 --- a/admin-service/src/main/resources/application.yml +++ b/admin-service/src/main/resources/application.yml @@ -37,8 +37,6 @@ spring: include-description: true openfeign: lazy-attributes-resolution: true - httpclient: - disable-ssl-validation: true main: allow-bean-definition-overriding: true diff --git a/admin-service/src/test/resources/application.yml b/admin-service/src/test/resources/application.yml index 2a6849dc..f9689751 100644 --- a/admin-service/src/test/resources/application.yml +++ b/admin-service/src/test/resources/application.yml @@ -34,8 +34,6 @@ spring: include-description: true openfeign: lazy-attributes-resolution: true - httpclient: - disable-ssl-validation: true main: allow-bean-definition-overriding: true diff --git a/authentication-service/src/main/resources/application.yml b/authentication-service/src/main/resources/application.yml index 000d268a..3947966b 100644 --- a/authentication-service/src/main/resources/application.yml +++ b/authentication-service/src/main/resources/application.yml @@ -22,8 +22,6 @@ spring: include-description: true openfeign: lazy-attributes-resolution: true - httpclient: - disable-ssl-validation: true main: allow-bean-definition-overriding: true flyway: diff --git a/chess-service/src/main/resources/application.yml b/chess-service/src/main/resources/application.yml index 25f1984a..731edc8f 100644 --- a/chess-service/src/main/resources/application.yml +++ b/chess-service/src/main/resources/application.yml @@ -22,8 +22,6 @@ spring: include-description: true openfeign: lazy-attributes-resolution: true - httpclient: - disable-ssl-validation: true main: allow-bean-definition-overriding: true flyway: diff --git a/fitness-service/src/main/resources/application.yml b/fitness-service/src/main/resources/application.yml index 213b7dec..8bc1fc43 100644 --- a/fitness-service/src/main/resources/application.yml +++ b/fitness-service/src/main/resources/application.yml @@ -22,8 +22,6 @@ spring: include-description: true openfeign: lazy-attributes-resolution: true - httpclient: - disable-ssl-validation: true main: allow-bean-definition-overriding: true flyway: diff --git a/gateway-service/src/main/resources/application.yml b/gateway-service/src/main/resources/application.yml index 13e89c90..04095902 100644 --- a/gateway-service/src/main/resources/application.yml +++ b/gateway-service/src/main/resources/application.yml @@ -62,8 +62,6 @@ spring: include-description: true openfeign: lazy-attributes-resolution: true - httpclient: - disable-ssl-validation: true main: allow-bean-definition-overriding: true diff --git a/music-service/src/main/resources/application.yml b/music-service/src/main/resources/application.yml index 78f08f98..4fdfa624 100644 --- a/music-service/src/main/resources/application.yml +++ b/music-service/src/main/resources/application.yml @@ -22,8 +22,6 @@ spring: include-description: true openfeign: lazy-attributes-resolution: true - httpclient: - disable-ssl-validation: true main: allow-bean-definition-overriding: true flyway: diff --git a/usermanagement-service/src/main/resources/application.yml b/usermanagement-service/src/main/resources/application.yml index 49d5751d..1c5f3395 100644 --- a/usermanagement-service/src/main/resources/application.yml +++ b/usermanagement-service/src/main/resources/application.yml @@ -22,8 +22,6 @@ spring: include-description: true openfeign: lazy-attributes-resolution: true - httpclient: - disable-ssl-validation: true main: allow-bean-definition-overriding: true flyway: From b1315b4f59f6a3f405b3f9aaab4cb241e8711ba3 Mon Sep 17 00:00:00 2001 From: Michael Baumberger Date: Fri, 3 Jan 2025 19:30:35 +0100 Subject: [PATCH 3/4] Refactor Discord client to use synchronous API calls Removed mono/reactor dependencies in favor of synchronous method calls for better compatibility and improved code clarity. Updated HTTP client implementation to leverage Spring's RestClient, simplifying request and response handling. Adjusted dependencies in the pom.xml to reflect these changes. --- .../notifier/CustomDiscordNotifier.kt | 4 +- spring-boot-starter-discord/pom.xml | 26 +---- .../michibaum/discord/api/DiscordClient.kt | 36 +++--- .../discord/api/DiscordClientImpl.kt | 108 +++++++++--------- .../discord/logging/ApplicationListener.kt | 6 +- .../discord/logging/DiscordAppender.kt | 1 - 6 files changed, 85 insertions(+), 96 deletions(-) diff --git a/admin-service/src/main/kotlin/com/michibaum/admin_service/notifier/CustomDiscordNotifier.kt b/admin-service/src/main/kotlin/com/michibaum/admin_service/notifier/CustomDiscordNotifier.kt index 86e7c7de..e7798b17 100644 --- a/admin-service/src/main/kotlin/com/michibaum/admin_service/notifier/CustomDiscordNotifier.kt +++ b/admin-service/src/main/kotlin/com/michibaum/admin_service/notifier/CustomDiscordNotifier.kt @@ -15,9 +15,9 @@ class CustomDiscordNotifier(private val discordClient: DiscordClient, private va return Mono.empty() } - return Mono.fromRunnable { + return Mono.fromRunnable { val createMessageDto = CreateMessageDto(createContent(event, instance)) - discordClient.sendMessage(adminDiscordProperties.channelId, createMessageDto).block() + discordClient.sendMessage(adminDiscordProperties.channelId, createMessageDto) } } diff --git a/spring-boot-starter-discord/pom.xml b/spring-boot-starter-discord/pom.xml index 5f8d5fdd..fd5b4249 100644 --- a/spring-boot-starter-discord/pom.xml +++ b/spring-boot-starter-discord/pom.xml @@ -29,33 +29,13 @@ org.springframework.boot - spring-boot-configuration-processor - true - - - - io.projectreactor - reactor-core - - - - io.projectreactor.netty - reactor-netty-core - - - - io.projectreactor.netty - reactor-netty-http - - - - io.projectreactor.kotlin - reactor-kotlin-extensions + spring-boot-starter-json org.springframework.boot - spring-boot-starter-json + spring-boot-configuration-processor + true diff --git a/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/api/DiscordClient.kt b/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/api/DiscordClient.kt index ce0a5929..6a6c3411 100644 --- a/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/api/DiscordClient.kt +++ b/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/api/DiscordClient.kt @@ -1,38 +1,46 @@ package com.michibaum.discord.api import com.michibaum.discord.api.dtos.* -import reactor.core.publisher.Mono interface DiscordClient { /** * Sends a message to the specified Discord channel. * * @param channelId The unique identifier of the Discord channel where the message will be sent. - * @param messageDto The data transfer object containing the content of the message to be sent. - * @return A Mono emitting the DTO of the sent message, containing details such as its unique identifier and content. + * @param messageDto A data transfer object containing the content of the message to be sent. + * @return A `GetMessageDto` object containing details of the message that was sent, including its unique ID, content, and the channel ID where it was sent. */ - fun sendMessage(channelId: String, messageDto: CreateMessageDto): Mono + fun sendMessage(channelId: String, messageDto: CreateMessageDto): GetMessageDto /** * Retrieves information about the guild associated with the Discord client. * - * @return A Mono emitting the DTO containing details of the guild, such as ID, name, description, features, owner ID, application ID, verification level, roles, default message - * notifications, max members, max video channel users, and preferred locale. + * @return A `GetGuildeDto` object containing details such as the guild's ID, name, description, features, owner ID, + * roles, and other attributes. */ - fun getGuild(): Mono + fun getGuild(): GetGuildeDto /** - * Retrieves a list of channels for the associated Discord guild. + * Retrieves a list of channels associated with the configured Discord guild. * - * @return A Mono emitting a list of GetChannelDto objects, each representing details of a Discord channel. + * This method makes an HTTP request to the Discord API to fetch all channels within the guild + * using the client's guild ID. Each channel is represented as a `GetChannelDto` object containing + * details such as the channel's ID, name, type, and other attributes. + * + * @return A list of `GetChannelDto` objects representing the channels in the guild. + * The list is non-null but could be empty if the guild has no channels. */ - fun getChannels(): Mono> + fun getChannels(): List /** - * Creates a new Discord channel based on the provided channel details. + * Creates a new Discord channel in the configured guild. + * + * This method sends a request to the Discord API to create a channel based on the supplied parameters. * - * @param channelDto The data transfer object containing the details required to create the new channel. - * @return A Mono emitting the DTO of the created channel, containing its unique identifier, name, type, guild ID, and other relevant details. + * @param channelDto A data transfer object containing the details required to create a new channel, + * such as its name, type, and optional parent category ID. + * @return A `GetChannelDto` object containing details of the newly created channel, + * including its ID, name, type, and associated guild ID. */ - fun createChannel(channelDto: CreateChannelDto): Mono + fun createChannel(channelDto: CreateChannelDto): GetChannelDto } \ No newline at end of file diff --git a/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/api/DiscordClientImpl.kt b/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/api/DiscordClientImpl.kt index 6c91cdb9..78b545de 100644 --- a/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/api/DiscordClientImpl.kt +++ b/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/api/DiscordClientImpl.kt @@ -3,9 +3,9 @@ package com.michibaum.discord.api import com.fasterxml.jackson.databind.ObjectMapper import com.michibaum.discord.api.dtos.* import com.michibaum.discord.config.DiscordProperties -import reactor.core.publisher.Flux -import reactor.netty.ByteBufFlux -import reactor.netty.http.client.HttpClient +import org.springframework.core.ParameterizedTypeReference +import org.springframework.web.client.RestClient +import org.springframework.web.client.bodyWithType /** * Implementation of the Discord client for making API calls to Discord services. @@ -44,86 +44,88 @@ open class DiscordClientImpl( val authToken = "Bot ${properties.botToken}" /** - * A configured HTTP client for interacting with the Discord API. + * A pre-configured REST client used to interact with the Discord API. * - * This client is set up with a base URL and common headers needed for authentication and - * content type specification. The headers include: - * - Authorization: required for API access - * - Content-Type: set to application/json for JSON data exchange - * - User-Agent: identifies the client as a Discord bot + * This client is responsible for making HTTP requests to various Discord API endpoints + * and includes default settings such as the base URL, authorization token, and headers required + * for communication with the Discord servers. * - * The client is primarily used for making HTTP requests such as GET and POST to various - * Discord API endpoints to manage guilds, channels, and other resources. + * The `httpClient` is primarily utilized by the `DiscordClientImpl` class to perform operations + * including retrieving guild information, fetching channel details, creating new channels, and + * sending messages. + * + * Default headers added to each request include: + * - `Authorization`: Contains the bot token for authentication. + * - `Content-Type`: Specifies the `application/json` format for requests and responses. + * - `User-Agent`: Identifies the client, including its version and source. */ - val httpClient = HttpClient.create() - .baseUrl(url) - .headers { headers -> - headers.add("Authorization", authToken) - headers.add("Content-Type", "application/json") - headers.add("User-Agent", "DiscordBot (https://github.com/MichiBaum/Microservices)") - } + private val httpClient: RestClient = RestClient.builder() + .baseUrl(url) + .defaultHeaders { headers -> + headers.add("Authorization", authToken) + headers.add("Content-Type", "application/json") + headers.add("User-Agent", "DiscordBot (https://github.com/MichiBaum/Microservices)") + }.build() + /** - * Retrieves the guild information for the configured guild from the Discord API. + * Retrieves detailed information about the guild associated with the Discord client. * - * This method makes an HTTP GET request to the /guilds/{guildId} endpoint of the Discord API. - * The response content is aggregated into a single string and then deserialized into a `GetGuildeDto` object - * using the configured ObjectMapper. + * This method makes an HTTP GET request to the Discord API's `/guilds/{guildId}` endpoint, + * where `{guildId}` is replaced with the guild ID configured in the client properties. + * The response is deserialized into a `GetGuildeDto` object, which contains information + * such as the guild's ID, name, description, features, and other attributes. * - * @return a `Mono` representing the guild information. + * @return A `GetGuildeDto` object containing information about the guild. The result is non-null + * but may require additional checks if the guild information cannot be successfully retrieved. */ override fun getGuild() = httpClient.get() .uri("/guilds/${properties.guildId}") - .responseContent() - .aggregate() - .asString() - .map { x -> objectMapper.readValue(x, GetGuildeDto::class.java) } + .retrieve() + .body(GetGuildeDto::class.java)!! // TODO maybe nullable /** - * Retrieves the list of channels for the configured guild from the Discord API. + * Retrieves a list of channels for the configured guild from the Discord API. * - * This method makes an HTTP GET request to the /guilds/{guildId}/channels endpoint of the Discord API. - * The response content is aggregated into a single string and then deserialized into a list of `GetChannelDto` objects - * using the configured ObjectMapper. + * This method performs an HTTP GET request to the /guilds/{guildId}/channels endpoint, where {guildId} + * is replaced with the guildId value from the Discord client properties. * - * @return a `Mono>` representing the list of channels in the guild. + * The request result is deserialized into a list of `GetChannelDto` objects, each representing + * detailed information about a channel within the guild. + * + * @return A list of `GetChannelDto` objects representing the channels of the guild. + * The result is non-null but may be empty if the guild has no channels. */ override fun getChannels() = httpClient.get() .uri("/guilds/${properties.guildId}/channels") - .responseContent() - .aggregate() - .asString() - .map> { x -> objectMapper.readValue(x, objectMapper.typeFactory.constructCollectionType(List::class.java, GetChannelDto::class.java)) } + .retrieve() + .body(object:ParameterizedTypeReference>(){})!! // TODO maybe nullable /** - * Creates a new Discord channel within the specified guild. + * Creates a new Discord channel in the configured guild by making a POST request to the Discord API. * - * @param channelDto The details of the channel to be created, encapsulated in a `CreateChannelDto` object. + * @param channelDto The data transfer object containing the details required to create a new channel, + * including properties such as the channel name, type, and optional parent category ID. */ override fun createChannel(channelDto: CreateChannelDto) = httpClient.post() .uri("/guilds/${properties.guildId}/channels") - .send(ByteBufFlux.fromString(Flux.just(objectMapper.writeValueAsString(channelDto)))) - .responseContent() - .aggregate() - .asString() - .map { x -> objectMapper.readValue(x, GetChannelDto::class.java)} + .bodyWithType(channelDto) + .retrieve() + .body(GetChannelDto::class.java)!! // TODO maybe nullable - // Create Message /** * Sends a message to a specified Discord channel. * - * @param channelId the ID of the channel where the message will be sent. - * @param messageDto the details of the message to be sent, encapsulated in a `CreateMessageDto` object. - * @return a `Mono` representing the sent message's details. + * @param channelId The unique identifier of the Discord channel where the message will be sent. + * @param messageDto A data transfer object containing the content of the message to be sent. + * @return A `GetMessageDto` object containing details of the sent message, including its ID and content. */ override fun sendMessage(channelId: String, messageDto: CreateMessageDto) = httpClient.post() .uri("/channels/$channelId/messages") - .send(ByteBufFlux.fromString(Flux.just(objectMapper.writeValueAsString(messageDto)))) - .responseContent() - .aggregate() - .asString() - .map { x -> objectMapper.readValue(x, GetMessageDto::class.java) } - + .bodyWithType(messageDto) + .retrieve() + .body(GetMessageDto::class.java)!! // TODO maybe nullable + // Get Channel // https://discord.com/developers/docs/resources/channel#get-channel diff --git a/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/logging/ApplicationListener.kt b/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/logging/ApplicationListener.kt index e2008535..d01592b8 100644 --- a/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/logging/ApplicationListener.kt +++ b/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/logging/ApplicationListener.kt @@ -23,7 +23,7 @@ import org.springframework.context.event.EventListener class ApplicationStartedListener(private val discordClient: DiscordClient, private val discordLoggingProperties: DiscordLoggingProperties): ApplicationListener { override fun onApplicationEvent(event: ApplicationStartedEvent) { val message = CreateMessageDto("Application started, took ${event.timeTaken.toMillis()} ms") - discordClient.sendMessage(discordLoggingProperties.channelId, message).block() + discordClient.sendMessage(discordLoggingProperties.channelId, message) } } @@ -39,7 +39,7 @@ class ApplicationStartedListener(private val discordClient: DiscordClient, priva class ApplicationReadyListener(private val discordClient: DiscordClient, private val discordLoggingProperties: DiscordLoggingProperties): ApplicationListener { override fun onApplicationEvent(event: ApplicationReadyEvent) { val message = CreateMessageDto("Application ready, took ${event.timeTaken.toMillis()} ms") - discordClient.sendMessage(discordLoggingProperties.channelId, message).block() + discordClient.sendMessage(discordLoggingProperties.channelId, message) } } @@ -61,7 +61,7 @@ class ApplicationAvailabilityListener(private val discordClient: DiscordClient, @EventListener fun onEvent(event: AvailabilityChangeEvent) { val message = CreateMessageDto("Application availability ${event.state.name}") - discordClient.sendMessage(discordLoggingProperties.channelId, message).block() + discordClient.sendMessage(discordLoggingProperties.channelId, message) } } \ No newline at end of file diff --git a/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/logging/DiscordAppender.kt b/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/logging/DiscordAppender.kt index d9fc26eb..63344f38 100644 --- a/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/logging/DiscordAppender.kt +++ b/spring-boot-starter-discord/src/main/kotlin/com/michibaum/discord/logging/DiscordAppender.kt @@ -24,7 +24,6 @@ class DiscordAppender(private val discordClient: DiscordClient, private val disc val message = DiscordLogMessage(logEvent).discordMessage try { discordClient.sendMessage(discordLoggingProperties.channelId, message) - .block() } catch (_: Exception) { } } From 2960a07f9d5e343c37a78f0860ff1f9a1853f4ee Mon Sep 17 00:00:00 2001 From: Michael Baumberger Date: Fri, 3 Jan 2025 19:34:19 +0100 Subject: [PATCH 4/4] Enhance DiscordNotifier with detailed event information. Added event instance, version, timestamp, and state details to the notification content. This improvement ensures more comprehensive and actionable notifications for users. --- .../admin_service/notifier/CustomDiscordNotifier.kt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/admin-service/src/main/kotlin/com/michibaum/admin_service/notifier/CustomDiscordNotifier.kt b/admin-service/src/main/kotlin/com/michibaum/admin_service/notifier/CustomDiscordNotifier.kt index e7798b17..5da20aea 100644 --- a/admin-service/src/main/kotlin/com/michibaum/admin_service/notifier/CustomDiscordNotifier.kt +++ b/admin-service/src/main/kotlin/com/michibaum/admin_service/notifier/CustomDiscordNotifier.kt @@ -24,7 +24,10 @@ class CustomDiscordNotifier(private val discordClient: DiscordClient, private va protected fun createContent(event: InstanceEvent, instance: Instance?): String { return """ - + Instance: ${event.instance.value} + Version: ${event.version} + Timestamp: ${event.timestamp} + State: ${event.type} """.trimIndent() }