Home > Mobile >  Should I replace my AWS Android SDK calls to DynamoDB with Retrofit?
Should I replace my AWS Android SDK calls to DynamoDB with Retrofit?

Time:10-17

I have a specific repo class used to make AWS DynamoDB database calls full of methods similar to the next:

public List<TestType> getTestTypes()
{
    List<TestType> scanResult = null;
    DynamoDBScanExpression scanExpression = new DynamoDBScanExpression();

    try {
        scanResult = awsConnection.getMapper().scan(TestType.class, scanExpression);
    } catch (Exception e) {
        AWSHelper.logError(e);
    }

    return scanResult;
}

I use AWSRepository, which is located in a repo package. It's full of AWS DynamoDB calls (which are done in the background) and the class starts like this:

public class AWSRepository implements IAWSDAO
{
    private static AWSRepository instance;
    static AWSConnection awsConnection;

    private AWSRepository()
    {
        awsConnection = new AWSConnection();
    }

    public static synchronized AWSRepository getInstance()
    {
        if(instance == null) instance = new AWSRepository();
        return instance;
    }

    ...AWS call methods...
}

And then the AWSConnection class:

class AWSConnection
{
    private DynamoDBMapper mapper;
    private AmazonDynamoDBClient ddbClient;

    private static String poolId;
    private static final Regions databaseRegion = Regions.EU_WEST_1;

    AWSConnection()
    {
        Context context = AppSettings.getAppContext();
        Cryptography crypto = new Cryptography();
        String cryptedPoolId = "xxx";
        poolId = crypto.decrypt(cryptedPoolId, TMSecurity.getCryptoParams());

        this.setMapper(initializeAWSIdentityPool_DynamoDBMapper(context));
        this.setDdbClient(initializeAWSIdentityPool_AmazonDynamoDBClient(context));
    }

    DynamoDBMapper getMapper()
    {
        return mapper;
    }

    private void setMapper(DynamoDBMapper mapper)
    {
        this.mapper = mapper;
    }

    AmazonDynamoDBClient getDdbClient()
    {
        return ddbClient;
    }

    private void setDdbClient(AmazonDynamoDBClient ddbClient)
    {
        this.ddbClient = ddbClient;
    }

    // initialize connection

    private static DynamoDBMapper initializeAWSIdentityPool_DynamoDBMapper(Context context)
    {
        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                context,
                poolId,
                databaseRegion
        );

        AmazonDynamoDBClient ddbClient = Region.getRegion(databaseRegion)
                .createClient(
                        AmazonDynamoDBClient.class,
                        credentialsProvider,
                        new ClientConfiguration()
                );

        return DynamoDBMapper.builder().dynamoDBClient(ddbClient).build();
    }

    private static AmazonDynamoDBClient initializeAWSIdentityPool_AmazonDynamoDBClient(Context context)
    {
        CognitoCachingCredentialsProvider credentialsProvider = new CognitoCachingCredentialsProvider(
                context,
                poolId,
                databaseRegion
        );

        return Region.getRegion(databaseRegion)
                .createClient(
                        AmazonDynamoDBClient.class,
                        credentialsProvider,
                        new ClientConfiguration()
                );
    }
}

build.gradle

...
api group: 'com.amazonaws', name: 'aws-android-sdk-cognito', version: '2.20.1'
api group: 'com.amazonaws', name: 'aws-android-sdk-core', version: '2.33.0'
api group: 'com.amazonaws', name: 'aws-android-sdk-ddb', version: '2.33.0'
api group: 'com.amazonaws', name: 'aws-android-sdk-ddb-mapper', version: '2.33.0'
...

This is working just fine, but I've heard whenever you need to make such calls it's a good practice to use a REST client (like Retrofit for example), and I'm trying to include all possible best practices into my app, so I'm in doubt about my case.

My confusion comes because I've asked AWS tech staff and they've told me I can leave it just like it is now, with no rest client with no problem at all, that AWS is ready to work this way (with no rest client) so my question is the next:

Should I really include a REST client like Retrofit?

What would be the real benefits to invest the necessary time?

Could I leave it just like it is now or am I "violating" any best practice this way with AWS?

CodePudding user response:

Should I really include a REST client like Retrofit?

No, as you are using the AWS SDK for Android.

It would actually be moving backwards to use a REST client to call the AWS REST API when you have an SDK available which gives you type safety, is regularly updated, has extensive documentaion and examples, seamless integration, in-built automatic retry logic & a whole lot of other benefits.

Using the official Amazon SDKs is always the best choice unless there isn't an SDK available for your target language.


Depending on your use case, the GitHub readme for the SDK may recommend using the Amplify Framework for new projects.

For new projects, we recommend interacting with AWS using the Amplify Framework.

It depends on your project but perhaps it might be better to use Amplify DataStore for example which has pretty cool features like real-time updates, offline data access etc. built-in.


I'd keep your project as-is & take a look at Amplify to see if you'd like any of the extensive features it offers for either migration or new app functionality.

If not, then you're following best practices already by using the SDK.

  • Related