Created
October 17, 2016 21:16
-
-
Save trbngr/420e273a07fe770a368e92336a62546a to your computer and use it in GitHub Desktop.
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
| package com.company | |
| import akka.actor.ActorSystem | |
| import akka.http.scaladsl.model.StatusCodes._ | |
| import akka.http.scaladsl.model.Uri | |
| import akka.util.Timeout | |
| import com.company.Crawler.Destination | |
| import com.company.model.AliasRequest | |
| import scala.concurrent.duration._ | |
| import scala.concurrent.{ExecutionContext, Future} | |
| class HttpMediator(repository: Repository)(implicit system: ActorSystem, ex: ExecutionContext) { | |
| import akka.pattern.ask | |
| val crawler = system.actorOf(Crawler.props) | |
| def getAliases(userId: UserId): Future[(Success, Set[AliasEntity])] = { | |
| repository.getAliases(userId) map { aliases ⇒ | |
| OK → aliases | |
| } | |
| } | |
| def createLink(pixelId: PixelId, request: AliasRequest): Future[(Success, AliasEntity)] = { | |
| implicit val timeout = Timeout(5 seconds) | |
| val future = crawler ? Crawler.Crawl(Uri(request.url)) | |
| future flatMap { | |
| case d: Destination ⇒ | |
| val screenshot = d.screenShot | |
| val entity = AliasEntity(pixelId, AliasId.generate, request.url, d.resolvedUrl.toString(), d.title, ScreenShots(screenshot.thumbnail, screenshot.fullPage), Tags.normalize(request.tags)) | |
| repository saveAlias entity map (_ ⇒ OK → entity) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment