Git Product home page Git Product logo

gapi's Introduction

GAPI

Build Status Maven Central

Overview

Scala wrapper for Google APIs - specifically drive. Focus is on making it simple to do common tasks such as listing files, creating folders, etc while still offering full access to the underlying Google APIs.

GAPI also simplifies the auth process for installed applications and akka-http based servers

Installation

TBW

Configuration

TBW

Authenticate from a server

Check out the full source code in ServerApp.scala

The following description assumes familiarity with akka-http

First we create a an object that will serve as our example akka server.

object ServerApp {

  import Directives._
  import org.mellowtech.gapi.GApiImplicits._

  implicit val actorSystem = ActorSystem()
  implicit val executor: ExecutionContext = actorSystem.dispatcher
  implicit val log: LoggingAdapter = Logging(actorSystem, getClass)
  implicit val materializer: ActorMaterializer = ActorMaterializer()
  implicit val conf: GApiConfig = GApiConfig()
}

Next we instantiate our database service. This is needed to store tokens. GApi uses slick so you need to provide a slick configuration (see above).

val dbService = new DbService
val tokenDAO: TokenService = TokenDAO(dbService)

Next we setup our router for google as well as the callback to store the token in our Db.

class ServerCallback(val tokenService: TokenService) extends DefaultAuthenticated with CredentialListener {
  val hasDrive: AtomicBoolean = new AtomicBoolean(false)
  var gdrive: Option[DriveService] = None
}

val serverCallback = new ServerCallback(tokenDAO)
val gAuth = new GoogleRouter(serverCallback)

Although not strictly needed it can be a good idea to setup your own exception handler for your akka routes in case you want to handle any GAPI Exception separately. This can be done like so

val gApiExceptionHandler = ExceptionHandler {
    case x: GApiException =>
      extractUri { uri =>
        log.error(s"Request to $uri could not be handled normally")
        complete(HttpResponse(StatusCodes.InternalServerError, entity = "" + x.jsonError.getOrElse("no json error")))
      }
    case z =>
      log.error(z, "exception")
      complete(StatusCodes.InternalServerError)
  }

Next we need to setup an initialise the specific google service we want to use (in this case drive).

  def drive: DriveService = serverCallback.gdrive.get

  def initGoogleServices: Unit = {
    //First try to create a drive-service if we already have credentials
    val f: Future[Option[Credential]] = for {
      opt <- tokenDAO.getDefault
    } yield opt match {
      case Some(t) => Some(GoogleHelper.credential(t, Some(jsonFactory), Some(httpTransport), Some(serverCallback)))
      case None => None
    }
    val cred = Await.result(f, 1 seconds)
    cred match {
      case Some(c) => {
        serverCallback.gdrive = Some(DriveService(c))
        serverCallback.hasDrive.set(true)
      }
      case None => serverCallback.gdrive = None
    }
  }

The final thing is to create a main method and bind our routes to our akka http server

  def main(args: Array[String]): Unit = {
    initGoogleServices
    Http().bindAndHandle(authRoute ~ gAuth.route ~ defRoute, conf.httpHost.get, conf.httpPort.get)
  }

For the actual routes please check the example source code ServerApp.scala

Authenticate from an installed application

Authentication from an installed application is simpler since we can rely on the APIs provided by google.

You can use the helper Installed.scala to get away with almost all boiler plate. To create an application that lists your google drive root you can do this:

object LocalApp extends App{

    implicit val conf: GApiConfig = GApiConfig()
    implicit val ec = ExecutionContext.global

    import scala.concurrent.duration._
    import scala.collection.JavaConverters._

    val ds = DriveService(Installed.credential(conf))

    val rootFolder = for{
      f <- ds.root
      fl <- ds.list(f)
    } yield fl

  Await.ready(rootFolder, 10 seconds).value.get match {
    case Success(fl) => {
      val nl = fl.getFiles.asScala.map(_.getName)
      println(nl.mkString("\n"))
    }
    case Failure(e) => e match {
      case x: GApiException => println(x.jsonError)
    }
  }
}

Use Google Drive API

TODO

Version History

gapi's People

Contributors

msvens avatar

Watchers

 avatar James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.