twogood / kronslott-sendgrid

DropWizard+Dagger bundle to send e-mails via SendGrid

Home Page:https://medium.com/@davideriksson_91895/dropwizard-kotlin-project-kronslott-e2aa51b277b8

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Project Kronslott

DropWizard SendGrid bundle

DropWizard bundle to set the SendGrid API key in your DropWizard configuration and expose the SendGrid object through Dagger.

A simple mail sending service is also exposed via Dagger.

Usage

example.yml

sendGrid:
  apiKey: ...

example.kt

This also shows how I add Dagger to DropWizard: I run bootstrap.addBundle() with an object expression (anonymous class) that instantiates the main Dagger component for the application and sets it as a property of the DropWizard application.

@Singleton
@Component(dependencies = [SendGridComponent::class], modules = [/*...*/])
interface ExampleComponent {
    // ...
}

class ExampleConfig : Configuration(), SendGridConfig {
    @Valid
    @JsonProperty("sendGrid")
    override var sendGridSettings: SendGridSettings = SendGridSettings()
   
    // ...
}

class ExampleApp : Application<ExampleConfig>() {

    private lateinit var component: ExampleComponent

    override fun initialize(bootstrap: Bootstrap<ExampleConfig>) {
        val sendGridBundle = SendGridBundle<ExampleConfig>()
        bootstrap.addBundle(sendGridBundle)

        bootstrap.addBundle(object : ConfiguredBundle<ExampleConfig> {

            override fun run(configuration: ExampleConfig, environment: Environment) {
                component = DaggerExampleComponent.builder()
                        .exampleModule(ExampleModule(configuration, environment))
                        .sendGridComponent(sendGridBundle.sendGridComponent)
                        .build()
            }

            override fun initialize(bootstrap: Bootstrap<*>) {}
        })

        // ...
    }
    
    // ...
}    

// Class that needs SendGrid
class ExampleService1 @Inject constructor(private val sendGrid: SendGrid) {
    // ...
}

// Class that needs the MailSendingService
class ExampleService2 @Inject constructor(private val mailSendingService: MailSendingService) {
    
    fun sendMail() {
        mailSendingService.sendMail(Email("from@example.com"), "*SUBJECT*", Email("to@example.com"), Content("text/html", "*HTML*"))
    }
    
}

Adding this library to your project

This project is not yet available from Maven Central Repository, but it's available via JitPack.io.

Release

About

DropWizard+Dagger bundle to send e-mails via SendGrid

https://medium.com/@davideriksson_91895/dropwizard-kotlin-project-kronslott-e2aa51b277b8

License:MIT License


Languages

Language:Kotlin 63.0%Language:Java 37.0%