/leanpub-client

A Leanpub-API client library written in Scala

Primary LanguageScalaMIT LicenseMIT

Leanpub-Client

A Leanpub-API client library

Build Status

This client library provides an object-oriented interface to the Leanpub API and is written in Scala, using the Akka HTTP client. You can preview and publish your books through this library, create and update coupons and get sales information. Previewing a single file, polling the job status and getting the latest version of your book is not yet implemented.

Usage Example

Add this to your build.sbt:

resolvers += "innoq" at "https://dl.bintray.com/innoq/maven"
libraryDependencies += "com.innoq" %% "leanpub-client" % "0.1.0"

Note that the library is only available for Scala 2.12.

To create an instance of LeanPubClient, we need to pass it a few things: an akka.play.api.libs.ws.ahc.StandaloneAhcWSClient, a LeanPub API key, a request timeout, as well as an implicit akka.stream.Materializer and a scala.concurrent.ExecutionContext. One recommended way of providing the API key is to read it from an environment variable.

If we want to get all the coupons for our book "myfancybook", we would do it like so:

import com.innoq.leanpubclient.LeanPubClient
import scala.concurrent.duration._

implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
import system.dispatcher // to get an implicit ExecutionContext

val wsClient = StandaloneAhcWSClient()

try {
  val client = new LeanPubClient(wsClient, sys.env("LEANPUB_API_KEY"), 2000.millis)
  val response = client.getCoupons("myfancybook")
  println(Await.result(response, 5.seconds))
}
finally {
  wsClient.close()
  system.terminate()
}

If we want to create a coupon for our book, we can do it (using the same setup) like this:

val coupon = CreateCoupon("testcoupon", List(PackageDiscount("book", 1.0)), LocalDate.of(2016, 7, 1))
val response = client.createCoupon("myfancybook", coupon)

For obtaining all individual purchases of our book, we can create a source which emits individual purchases one by one. Here is an example of the usage:

val source = client.getIndividualPurchaseSource("myfancybook")
source.watchTermination()((_, f) => f.onComplete {_ =>    
  wsClient.close()
  actorSystem.terminate()
}).runWith(Sink.foreach(println))

Contributors

Tina Schönborn

License

MIT

Copyright

Copyright 2016-2017 innoQ Deutschland GmbH