Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to use Vimeo API from Scala
scalaVersion := "2.11.7"
scalacOptions := Seq("-deprecation")
libraryDependencies ++= List(
"com.vimeo.networking" % "vimeo-networking" % "1.1.1"
)
resolvers += Resolver.jcenterRepo
import com.vimeo.networking._
import com.vimeo.networking.model._
import com.vimeo.networking.model.error.VimeoError;
import com.vimeo.networking.callbacks._
import java.io.File
import scala.concurrent.{Future, Promise}
import scala.reflect.ClassTag
import scala.reflect._
import okhttp3.CacheControl
/* This is how we get nice Future's from the Callbacks Vimeo-networking uses */
object RetrofitCallAdapter {
def apply[T: ClassTag](f: ModelCallback[T] => Unit): Future[T] = {
val p = Promise[T]()
val mc = new ModelCallback[T](classTag[T].runtimeClass) {
def success(t: T) = {
p.success(t)
}
def failure(error: VimeoError) = {
p.failure(error)
}
}
f(mc)
p.future
}
}
val accessCode = "XXXXX"
/* Assuming you want a cache, this is how you set it up */
val configuration = (new Configuration.Builder(accessCode)).setCacheDirectory(new File("cache-dir-you-want"))
VimeoClient.initialize(configuration.build())
/* An actual call */
val uri = "https://api.vimeo.com/videos/VIDEO-ID"
val futureVideo = RetrofitCallAdapter[Video] { mc =>
// You'll want to make your own cache config so you don't always go out to network and pass it instead of FORCE_NETWORK
VimeoClient.getInstance().fetchContent(uri, CacheControl.FORCE_NETWORK, mc)
}
/*Some calling code might look like this: */
import scala.concurrent.ExecutionContext.Implicits.global
futureVideo.map(_.duration) // congrats, you have Future[Int]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment