Last active
April 9, 2018 18:28
-
-
Save EdgeCaseBerg/6d5d787e25f078795b855a1bb0220fda to your computer and use it in GitHub Desktop.
How to use Vimeo API from Scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
scalaVersion := "2.11.7" | |
scalacOptions := Seq("-deprecation") | |
libraryDependencies ++= List( | |
"com.vimeo.networking" % "vimeo-networking" % "1.1.1" | |
) | |
resolvers += Resolver.jcenterRepo | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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