rustedbones / pekko-http-thrift   1.1.0

Apache License 2.0 GitHub

pekko-http thrift and json marshalling/unmarshalling for Thrift structs

Scala versions: 3.x 2.13

pekko-http-thrift

Continuous Integration Maven Central Software License Scala Steward badge

pekko-http thrift and json marshalling/unmarshalling for Thrift structs

Versions

Version Release date pekko Http version Thrift version Scala versions
1.1.0 2024-04-09 1.0.1 0.20.0 3.3, 2.13
1.0.0 2023-11-02 1.0.0 0.19.0 3.3, 2.13

The complete list can be found in the CHANGELOG file.

Getting pekko-http-thrift

Libraries are published to Maven Central. Add to your build.sbt:

libraryDependencies += "fr.davit" %% "pekko-http-thrift" % <version> // thrift support

Quick start

For the examples, we are using the following thrift domain model

struct Item {
  1: string name
  2: i64 id
}

struct Order {
  1: list<Item> items
}

Marshalling/Unmarshalling of the generated classes depends on the Accept/Content-Type header sent by the client:

  • Content-Type: application/json: json
  • Content-Type: application/vnd.apache.thrift.json: json
  • Content-Type: application/vnd.apache.thrift.binary: binary
  • Content-Type: application/vnd.apache.thrift.compact: compact

-No Accept header or matching several (eg Accept: application/*) will take the 1st matching type from the above list.

Thrift

The implicit marshallers and unmarshallers for your generated thrift classes are defined in ThriftSupport. Specific (un)marshallers can be imported from ThriftBinarySupport, ThriftCompactSupport and ThriftJsonSupport. You simply need to have them in scope.

import org.apache.pekko.http.scaladsl.server.Directives
import fr.davit.pekko.http.scaladsl.marshallers.thrift.ThriftSupport


class MyThriftService extends Directives with ThriftSupport {

  // format: OFF
  val route =
    get {
      pathSingleSlash {
        complete(Item("thing", 42))
      }
    } ~
      post {
        entity(as[Order]) { order =>
          val itemsCount = order.items.size
          val itemNames = order.items.map(_.name).mkString(", ")
          complete(s"Ordered $itemsCount items: $itemNames")
        }
      }
  // format: ON
}

Limitation

Entity streaming (http chunked transfer) is at the moment not supported by the library.