Skip to content

Instantly share code, notes, and snippets.

View brunapereira's full-sized avatar

Bruna Pereira brunapereira

View GitHub Profile
@brunapereira
brunapereira / providers.tf
Created April 11, 2021 02:52
terraform-github-providers
provider "github" {
owner = "terraform-github-article"
}
terraform {
required_providers {
github = {
version = "~> 4.6"
}
}
plugins {
kotlin("jvm") version "1.3.72"
id("com.github.johnrengelman.shadow") version "4.0.4"
id("org.unbroken-dome.test-sets") version "3.0.1"
application
}
group = "com.brunapereira"
version = "1.0-SNAPSHOT"
@brunapereira
brunapereira / VendasExceptionHandler.kt
Last active May 11, 2020 14:58
Exception Handler
@RestControllerAdvice
@Order(0)
class VendasExceptionHandler {
@ResponseStatus(BAD_REQUEST)
@ExceptionHandler(ProductNotFound::class)
fun onProductNotFound(e: ProductNotFound): ApiError {
log.error("error=product not found", e)
return ApiError(code = "PRODUCT_NOT_FOUND", message = e.message ?: "")
}
@brunapereira
brunapereira / ExceptionHandler.kt
Last active May 11, 2020 02:59
Example of a Spring ControllerAdvice
@RestControllerAdvice
class ExceptionHandler {
@ResponseStatus(INTERNAL_SERVER_ERROR) [1]
@ExceptionHandler(Exception::class)
fun onException(exception: Exception): ApiError {
log.error("error=general message=${exception.message}", exception)
return ApiError("INTERNAL_SERVER_ERROR", exception.message ?: "")
}
class ClientBuilder {
private var failOnUnknownProperty: Boolean = false
private var namingStrategy: PropertyNamingStrategy = PropertyNamingStrategy.LOWER_CAMEL_CASE
private var headers: MutableMap<String, String> = mutableMapOf()
private lateinit var baseUrl: String
fun withFailOnUnknownProperty(failOnUnknown: Boolean): ClientBuilder {
this.failOnUnknownProperty = failOnUnknown
return this
}
get(
url = "/login",
responseClass = Token::class,
queryParameters = mapOf("admin", "true")
)
get(
url = "/login",
responseClass = Token::class,
headers = mapOf("Accept-Version", "v1")
abstract class MicroserviceGateway {
abstract var webClient: WebClient
// outros métodos HTTP
fun <ResponseT : Any> get(
url: String,
responseClass: KClass<ResponseT>,
queryParameters: Map<String, String> = emptyMap(),
headers: Map<String, String> = emptyMap()
@Component
@ConfigurationProperties("service.authentication")
class AuthenticationProperties {
final lateinit var baseUrl: String
final lateinit var username: String
final lateinit var password: String
}
@Component
class AuthenticationGateway(private val properties: AuthenticationProperties [1]) : MicroserviceGateway() [2] {
override var webClient: WebClient = ClientBuilder() [3]
.withBaseUrl(properties.baseUrl)
.withFailOnUnknownProperty(false)
.withNamingStrategy(PropertyNamingStrategy.SNAKE_CASE)
.build()
fun authenticate(): AuthenticationToken [4] {
val request =
@Configuration
class SwaggerResourceConfig {
@Primary
@Bean
fun swaggerResourcesProvider(defaultResourcesProvider: InMemorySwaggerResourcesProvider): SwaggerResourcesProvider {
return SwaggerResourcesProvider {
val resource = SwaggerResource()
resource.name = "Open API"
resource.swaggerVersion = "2.9.2"