Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.gmantra.ixchel.task
- import akka.actor._
- import akka.event.Logging
- import akka.http.scaladsl._
- import akka.http.scaladsl.client._
- import akka.http.scaladsl.marshallers.argonaut._
- import akka.http.scaladsl.model._
- import akka.http.scaladsl.model.headers._
- import akka.stream._
- import akka.stream.scaladsl._
- import argonaut._, Argonaut._
- import com.typesafe.config.ConfigFactory
- import org.slf4j.LoggerFactory
- import scala.collection.immutable.Seq
- import com.github.ghik.silencer.silent
- import org.gmantra.ixchel.actor._
- import org.gmantra.ixchel.helpers._
- import org.gmantra.ixchel.models._
- import org.gmantra.ixchel.models._, ix._
- object FakeIt extends App with IxchelWorker with ArgonautSupport {
- implicit val system = ActorSystem("fakeit")
- implicit val ec = system.dispatcher
- implicit val materializer = ActorMaterializer()
- lazy val logger = LoggerFactory.getLogger(this.getClass)
- val config = ConfigFactory.load()
- /**
- * Parse args and set user id, stream id, and stream item count
- */
- val (userId, streamId, count) = args.toList match {
- case n :: Nil ⇒ ("hackcave", "fakeit", n.toLong)
- case s :: n :: Nil ⇒ ("hackcave", s, n.toLong)
- case u :: s :: n :: Nil ⇒ (u, s, n.toLong)
- case _ ⇒ ("hackcave", "fakeit", 10L)
- }
- /**
- * The user to create
- */
- val user = SignUp(
- id = userId,
- name = s"${userId}",
- email = s"${userId}@dev.gmantra.net",
- bio = s"${userId}: created using fakeit",
- properties = jEmptyObject,
- password = Option(userId.reverse)
- )
- /**
- * The stream to create
- */
- val stream =
- Stream.Create(
- id = streamId,
- name = s"${streamId}: created using fakeIt",
- properties = Map.empty
- )
- /**
- * The source of stream items generated from the fortune source
- */
- @silent
- val itemSource =
- Source
- .actorPublisher[(String, List[String])](FortunePublisher.props)
- .map {
- case (cookie, tags) ⇒
- StreamItem.Create(
- source = Map(
- "type" → "textile",
- "content" → cookie
- ).asJson,
- tags = tags,
- keywords = Map.empty,
- properties = jEmptyObject,
- mentions = Nil,
- actions = Nil,
- published = true
- )
- }
- import RequestBuilding._
- /**
- * The cookie used to authenticate
- */
- val (_, timestamp, auth) = Authenticate(userId)
- val cookie = Cookie(
- ("id", userId),
- ("timestamp", s"${timestamp}"),
- ("auth", auth)
- )
- val addAuth = addHeader(cookie)
- /**
- * The xsrf token
- */
- val xsrf = "42"
- val addXSRF = addHeaders(
- Cookie("XSRF-TOKEN", xsrf),
- RawHeader("X-XSRF-TOKEN", xsrf)
- )
- val userCreateRequest =
- Post(s"/?method=account.create", user) ~>
- addHeader(RawHeader("X-Captcha-Response", config.getString("recaptcha.dummyResponse")))
- val streamRequest =
- Post(s"/${userId}", stream) ~> addAuth
- val itemRequestSource = itemSource.map { item ⇒
- Post(s"/${userId}/${stream.id}/items", item) ~> addAuth
- }
- /**
- * Connect to http://localhost:7771
- */
- val http = Http().outgoingConnection("localhost", 7771)
- val createUser = Source.single(userCreateRequest)
- val createStream = Source.single(streamRequest)
- val createStreamItems = itemRequestSource.take(count)
- createUser
- /* .concat(createStream)
- .concat(createStreamItems)*/
- .map(addXSRF)
- .map(
- logRequest { req ⇒
- logger.info(s"""${req.method.value} ${req.uri} ${req.headers.mkString(" ")}""")
- }
- )
- .via(http)
- .runWith {
- Sink.foreach { resp ⇒
- logger.info(s"""${resp.status.intValue} ${resp.headers.mkString(" ")}""")
- }
- }
- .onComplete {
- implicit val userIdentity = userId
- implicit val driver = IXDriver(null)
- DO {
- val crud = new UserCRUD {}
- for {
- resp ← runDB { crud.setActive(userIdentity) }
- } yield resp
- }
- _ ⇒ system.terminate()
- }
- createStream
- .concat(createStreamItems)
- .map(addXSRF)
- .map(
- logRequest { req ⇒
- logger.info(s"""${req.method.value} ${req.uri} ${req.headers.mkString(" ")}""")
- }
- )
- .via(http)
- .runWith {
- Sink.foreach { resp ⇒
- logger.info(s"""${resp.status.intValue} ${resp.headers.mkString(" ")}""")
- }
- }
- .onComplete { _ ⇒
- system.terminate()
- }
- }
Add Comment
Please, Sign In to add comment