Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Humble feedback #95

Open
jasenkoh opened this issue Nov 30, 2023 · 0 comments
Open

Humble feedback #95

jasenkoh opened this issue Nov 30, 2023 · 0 comments

Comments

@jasenkoh
Copy link

Hey there,

I stumbled on this repo looking for some examples of Google Subscription API implementation and noticed how you parse a message in PaymentController. Instead, I would suggest you too look at spring integration where you get parsing out of the box and you can bypass decoding instead, you can just deserialze from json string.

Additionally, when you registered AndroidPublisher bean, you can potentially simplify authentication part. The way I did it, is I created a service account with managed permissions and store the credentials file in AWS SM

Sharing you example how I did below in case you want to do:

@Configuration
class GoogleConfig(
    @Value("\${spring.cloud.gcp.pubsub.billing-subscription}")
    private val billingSubscription: String,
    private val awsSecretsManagerService: AwsSecretsManagerService
) {
    private val jsonFactory = GsonFactory.getDefaultInstance()
    private val httpTransport = GoogleNetHttpTransport.newTrustedTransport()

    @Bean
    fun androidPublisher(
        credentialsProvider: CredentialsProvider
    ): AndroidPublisher {
        return AndroidPublisher.Builder(httpTransport, jsonFactory, HttpCredentialsAdapter(credentialsProvider.credentials))
            .setApplicationName("project-name")
            .build()
    }

    @Bean
    fun pubsubInputChannel(): MessageChannel {
        return DirectChannel()
    }

    @Bean
    fun messageChannelAdapter(
        //Declaring a qualifier for a message handling defined in BillingSubscriber
        @Qualifier("pubsubInputChannel") inputChannel: MessageChannel?,
        pubSubTemplate: PubSubTemplate?
    ): PubSubInboundChannelAdapter {
        val adapter = PubSubInboundChannelAdapter(pubSubTemplate, billingSubscription)
        adapter.outputChannel = inputChannel
        adapter.setAckMode(AckMode.MANUAL)
        return adapter
    }

    @Bean
    fun credentialsProvider(): CredentialsProvider {
        val credentialsStream = awsSecretsManagerService.getSecrets().gcpCredentials.byteInputStream()
        val credentials = GoogleCredentials.fromStream(credentialsStream)

        return CredentialsProvider { credentials }
    }
}

BillinSubscriber.kt

@Component
class BillingSubscriber(
    private val objectMapper: ObjectMapper
) {

    @Bean
    @ServiceActivator(inputChannel = "pubsubInputChannel")
    fun messageReceiver(): MessageHandler {
        return MessageHandler { message ->
            val messageString = String((message.payload as ByteArray))
            val originalMessage = GcpPubSubHeaders.getOriginalMessage(message)
            if (originalMessage.isPresent) {
                val notification = objectMapper.readValue(messageString, DeveloperNotification::class.java)
                // Handle published message
                originalMessage.get().ack()
            } else {
                logger.error { "Message can not be acknowledged" }
            }
        }
    }
}

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant