GitHub Maven Central Version Scaladoc

scala-aws-client

This Scala library wraps selected parts of the AWS SDK for Java 2.x to offer simpler, scala-idiomatic API.

Table of contents

Services

Usage

Use with SBT

libraryDependencies += "org.encalmo" %% "scala-aws-client" % "0.9.8"

or with SCALA-CLI

//> using dep org.encalmo::scala-aws-client:0.9.8

Dependencies

Working with DynamoDB

Using AwsDynamoDbApi

  import org.encalmo.aws.AwsClient
  import org.encalmo.aws.AwsDynamoDbApi.*
  import org.encalmo.aws.AwsDynamoDbApi.given
  import org.encalmo.models.{CUID2, Amount}

  val id = CUID2.randomCUID2()

  AwsClient.maybe {
    putItemInTable(
      tableName = "items",
      item = DynamoDbItem(
        "item_id" -> id,
        "status" -> "fine",
        "price" -> 12345,
        "is_for_sale" -> true
      )
    )
  }

  val item3 = AwsClient.optionally {
    getItemFromTable("items", ("item_id" -> id))
  }

Using DynamoDbTable trait

    import org.encalmo.aws.DynamoDbTable
    import org.encalmo.aws.AwsDynamoDbApi.{*,given}
    import org.encalmo.models.{CUID2, Amount}

    given DynamoDbEnvironment = DefaultDynamoDbEnvironment
    given ErrorContext = DefaultErrorContext

    case class Item(
      item_id: CUID2,
      price: Amount,
      `is-for-sale`: Boolean,
      description: Option[String] = None
  )

    object Items extends DynamoDbTable[CUID2]("item_id") {
      override inline def baseTableName: String = "items"
    }

    val id = CUID2.randomCUID2()

    val item = Item(
      item_id = id,
      price = Amount(1234),
      is_for_sale = false
    )

    Items.setItem(item)
    val item2 = Items.getItemAsClass[Item](id)
    assert(item2.isDefined)

    Items.getItem(id)
    Items.setItemProperties(id, "is_for_sale" -> true, "price" -> Amount(1234))
    Items.setItemProperties(id, "description" -> "some old crap")
    Items.getItem(id)
    Items.removeItem(id)

Using DynamoDbTableWithSortKey trait

    import org.encalmo.aws.DynamoDbTable
    import org.encalmo.aws.AwsDynamoDbApi.{*,given}
    import org.encalmo.models.{CUID2, Amount}

    object OrdersTable extends DynamoDbTableWithSortKey[String, Long]("order_id", "createdAt") {
      override inline def baseTableName: String = "orders"
    }

    given DynamoDbEnvironment = DefaultDynamoDbEnvironment
    given ErrorContext = DefaultErrorContext

    val id = CUID2.randomCUID2()
    val createdAt = Instant.now().getEpochSecond()

    assert(OrdersTable.getItemOrError(id, createdAt).isLeft)
    OrdersTable.setItemProperties(id, createdAt, "status" -> "submitted")
    assert(OrdersTable.getItemOrError(id, createdAt).isRight)
    OrdersTable.removeItemProperty(id, createdAt, "status")
    assert(OrdersTable.getItemOrError(id, createdAt).isRight)
    OrdersTable.removeItem(id, createdAt)
    assert(OrdersTable.getItemOrError(id, createdAt).isLeft)

Project content

├── .github
│   └── workflows
│       ├── pages.yaml
│       ├── release.yaml
│       └── test.yaml
│
├── .gitignore
├── .scalafix.conf
├── .scalafmt.conf
├── AwsApiGatewayApi.scala
├── AwsApiGatewayV2Api.scala
├── AwsClient.scala
├── AwsClient.test.scala
├── AwsClientStatefulStub.scala
├── AwsClientStatefulStub.test.scala
├── AwsClientStatelessStub.scala
├── AwsClientStatelessStub.test.scala
├── AwsDynamoDbApi.scala
├── AwsDynamoDbApi.test.scala
├── AwsIamApi.scala
├── AwsKmsApi.scala
├── AwsKmsApi.test.scala
├── AwsLambdaApi.scala
├── AwsLambdaApi.test.scala
├── AwsS3Api.scala
├── AwsSecretsManagerApi.scala
├── AwsSqsApi.scala
├── AwsStsApi.scala
├── AwsStsApi.test.scala
├── DynamoDbEnvironment.scala
├── DynamoDbTable.scala
├── DynamoDbTable.test.scala
├── DynamoDbTableWithSortKey.scala
├── DynamoDbTableWithSortKey.test.scala
├── ErrorContext.scala
├── LICENSE
├── Macros.scala
├── Macros.test.scala
├── project.scala
├── README.md
├── test.sh
├── TestSuite.test.scala
└── Utils.scala