1. Introduction

Micronaut Recurly provides configuration for Recurly Java Client.

2. Installation

The library is available in Maven Central:

Gradle Installation
repositories {
    mavenCentral()
}

dependencies {
    implementation "com.agorapulse:micronaut-recurly:1.1.2-micronaut-3.0"
}

3. Usage

The only required configuration property is recurly.api-key

recurly:
  api-key: recurly_api_key
You can also specify the key as the RECURLY_API_KEY environment variable.

Then you can inject com.recurly.v3.Client client into your services:

import com.recurly.v3.Client;
import javax.inject.Singleton;

@Singleton
public class MyRecurlyService {

    private final Client recurlyClient;

    MyRecurlyService(Client recurlyClient) {
        this.recurlyClient = recurlyClient;
    }

    // more code here

}

3.1. Testing

You can use the recurly.api-url configuration property to override the server URL to direct your calls to a mock server. You must also set the RECURLY_UNSAFE environment variable to confirm the change.

import com.recurly.v3.Client
import com.recurly.v3.resources.Account
import com.stehno.ersatz.ContentType
import com.stehno.ersatz.ErsatzServer
import io.micronaut.context.ApplicationContext
import spock.lang.AutoCleanup
import spock.lang.Specification

import static com.github.stefanbirkner.systemlambda.SystemLambda.*

class RecurlyFactorySpec extends Specification {

    private static final String API_KEY = 'someapikey'
    private static final String ACCOUNT_ID = 'account-id'

    @AutoCleanup ApplicationContext context
    @AutoCleanup ErsatzServer server

    Client recurlyClient

    void 'client is configured'() {
        given:
            String token = Base64.encoder.encodeToString("$API_KEY:".bytes)
            String accountJson = RecurlyFactorySpec.getResourceAsStream('account.json').text

            server = new ErsatzServer({                                                 (1)
                reportToConsole()
            })

            server.expectations {
                get '/accounts/' + ACCOUNT_ID, {
                    header 'Authorization', "Basic $token"
                    responds().body(accountJson, ContentType.APPLICATION_JSON)
                }
            }

            server.start()

        when:
            Account account = withEnvironmentVariable('RECURLY_INSECURE', 'true').execute { (2)
                context = ApplicationContext.builder(
                    'recurly.api-key': API_KEY,
                    'recurly.api-url': server.httpUrl                                   (3)
                ).build()

                context.start()

                recurlyClient = context.getBean(Client)

                recurlyClient.getAccount(ACCOUNT_ID)                                    (4)
            }

        then:
            server.verify()

            account.id == ACCOUNT_ID                                                    (5)
    }

}
1 Setup the mock Ersatz Server
2 Alter the environment variable to allow overriding the recurly URL
3 Use the URL of the mock server as recurly.api-url property to direct the calls to it
4 Perform the API call using the client
5 Verify that the response matches the mock response