<commit_before>package com.themillhousegroup.gatsby

import io.gatling.http.request.builder._
import io.gatling.http.config.HttpProtocol
import io.gatling.http.request.HttpRequestDef
import io.gatling.http.request.builder.HttpAttributes
import io.gatling.http.request.builder.CommonAttributes
import io.gatling.http.request.HttpRequestDef
import io.gatling.http.action.{ HttpRequestActionBuilder, HttpRequestAction, HttpActionBuilder }
import akka.actor.ActorRef
import io.gatling.core.config.Protocols
import io.gatling.core.controller.throttle.ThrottlingProtocol
import akka.actor.ActorDSL._
import io.gatling.http.request.builder.HttpAttributes
import io.gatling.core.controller.throttle.ThrottlingProtocol
import io.gatling.http.request.builder.CommonAttributes
import io.gatling.http.request.HttpRequestDef
import org.slf4j.LoggerFactory
import io.gatling.core.session._
import io.gatling.http.request.builder.HttpAttributes
import io.gatling.core.controller.throttle.ThrottlingProtocol
import io.gatling.http.request.builder.CommonAttributes
import io.gatling.http.request.HttpRequestDef
import com.ning.http.client.Request
import io.gatling.core.action.Chainable

class GatsbyHttpRequestWrapper(commonAttributes: CommonAttributes, httpAttributes: HttpAttributes) extends AbstractHttpRequestBuilder[GatsbyHttpRequestWrapper](commonAttributes, httpAttributes) {

  override def newInstance(commonAttributes: CommonAttributes) = new GatsbyHttpRequestWrapper(commonAttributes, httpAttributes)
  override def newInstance(httpAttributes: HttpAttributes) = new GatsbyHttpRequestWrapper(commonAttributes, httpAttributes)

  override def request(protocol: HttpProtocol): Expression[Request] = {
    logger.info("Intercepting request()")
    new HttpRequestExpressionBuilder(commonAttributes, httpAttributes, protocol).build
  }

}

class GatsbyHttpRequestWithParamsWrapper(commonAttributes: CommonAttributes,
  httpAttributes: HttpAttributes,
  formParams: List[HttpParam])(onBuild: => Unit)
    extends HttpRequestWithParamsBuilder(commonAttributes, httpAttributes, formParams) {

  override def build(protocol: HttpProtocol, throttled: Boolean): HttpRequestDef = {
    logger.info("Building NOW!")
    onBuild
    super.build(protocol, throttled)
  }
}

object GatsbyHttpRequestActionBuilder {
  def withStubby(requestBuilder: AbstractHttpRequestBuilder[_]) = {
    new GatsbyHttpRequestActionBuilder(requestBuilder)
  }
}

class GatsbyHttpRequestActionBuilder(requestBuilder: AbstractHttpRequestBuilder[_]) extends HttpActionBuilder {

  private val logger = LoggerFactory.getLogger(getClass)

  logger.info("GatsbyHttpRequestActionBuilder constructed")

  def build(next: ActorRef, protocols: Protocols): ActorRef = {
    logger.info("GatsbyHttpRequestActionBuilder build()")
    val throttled = protocols.getProtocol[ThrottlingProtocol].isDefined
    val httpRequest = requestBuilder.build(httpProtocol(protocols), throttled)


    val tearDown = actor(new TearDown("blah", next))
    val request = actor(new HttpRequestAction(httpRequest, tearDown))
    val spinUp = actor(new SpinUp("blurgh", request))

    // should return spinUp...

    actor(new HttpRequestAction(httpRequest, next))
  }
}

class SpinUp(msg:String, val next:ActorRef) extends Chainable {
  println(s"spinning up $msg")
}

class TearDown(msg:String, val next:ActorRef) extends Chainable {
  println(s"tearing down $msg")
}<commit_msg>A new approach based on composition rather than extension. WIP<commit_after>package com.themillhousegroup.gatsby

import io.gatling.http.request.builder._
import io.gatling.http.config.HttpProtocol
import io.gatling.http.request.HttpRequestDef
import io.gatling.http.request.builder.HttpAttributes
import io.gatling.http.request.builder.CommonAttributes
import io.gatling.http.request.HttpRequestDef
import io.gatling.http.action.{ HttpRequestActionBuilder, HttpRequestAction, HttpActionBuilder }
import akka.actor.ActorRef
import io.gatling.core.config.Protocols
import io.gatling.core.controller.throttle.ThrottlingProtocol
import akka.actor.ActorDSL._
import io.gatling.http.request.builder.HttpAttributes
import io.gatling.core.controller.throttle.ThrottlingProtocol
import io.gatling.http.request.builder.CommonAttributes
import io.gatling.http.request.HttpRequestDef
import org.slf4j.LoggerFactory
import io.gatling.core.session._
import io.gatling.http.request.builder.HttpAttributes
import io.gatling.core.controller.throttle.ThrottlingProtocol
import io.gatling.http.request.builder.CommonAttributes
import io.gatling.http.request.HttpRequestDef
import com.ning.http.client.Request
import io.gatling.core.action.Chainable

class GatsbyHttpRequestWrapper(commonAttributes: CommonAttributes, httpAttributes: HttpAttributes) extends AbstractHttpRequestBuilder[GatsbyHttpRequestWrapper](commonAttributes, httpAttributes) {

  override def newInstance(commonAttributes: CommonAttributes) = new GatsbyHttpRequestWrapper(commonAttributes, httpAttributes)
  override def newInstance(httpAttributes: HttpAttributes) = new GatsbyHttpRequestWrapper(commonAttributes, httpAttributes)

  override def request(protocol: HttpProtocol): Expression[Request] = {
    logger.info("Intercepting request()")
    new HttpRequestExpressionBuilder(commonAttributes, httpAttributes, protocol).build
  }

}

class GatsbyHttpRequestWithParamsWrapper(commonAttributes: CommonAttributes,
  httpAttributes: HttpAttributes,
  formParams: List[HttpParam])(onBuild: => Unit)
    extends HttpRequestWithParamsBuilder(commonAttributes, httpAttributes, formParams) {

  override def build(protocol: HttpProtocol, throttled: Boolean): HttpRequestDef = {
    logger.info("Building NOW!")
    onBuild
    super.build(protocol, throttled)
  }
}

object GatsbyHttpRequestActionBuilder {
  def withStubby(requestBuilder: AbstractHttpRequestBuilder[_]) = {
    new GatsbyHttpRequestActionBuilder(requestBuilder)
  }
}

class GatsbyHttpRequestActionBuilder(requestBuilder: AbstractHttpRequestBuilder[_]) extends HttpActionBuilder {

  private val logger = LoggerFactory.getLogger(getClass)

  logger.info("GatsbyHttpRequestActionBuilder constructed")

  def build(next: ActorRef, protocols: Protocols): ActorRef = {
    logger.info("GatsbyHttpRequestActionBuilder build()")
    val throttled = protocols.getProtocol[ThrottlingProtocol].isDefined
    val httpRequest = requestBuilder.build(httpProtocol(protocols), throttled)

    val tearDown = actor(new TearDown("blah", next))
    val request = actor(new HttpRequestAction(httpRequest, tearDown))
    val spinUp = actor(new SpinUp("blurgh", request))

    //actor(new HttpRequestAction(httpRequest, next))
    spinUp
  }
}

class SpinUp(msg: String, val next: ActorRef) extends Chainable {

  def execute(session: Session): Unit = {

    println(s"spinning up $msg for scenario: ${session.scenarioName}")
  }
}

class TearDown(msg: String, val next: ActorRef) extends Chainable {

  def execute(session: Session): Unit = {
    println(s"tearing down $msg for scenario: ${session.scenarioName}")
  }
}
{'fraction_non_alphanumeric': 0.089666951323655, 'fraction_numerical': 0.0002846569883290635, 'mean_word_length': 6.63804347826087, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5833875', 'n_tokens_mistral': 2030, 'n_tokens_neox': 1993, 'n_words': 385}