在Scala Gatling中将GRPC响应的响应值传递给REST API

lyr7nygr  于 6个月前  发布在  Scala
关注(0)|答案(1)|浏览(76)

希望你今天过得好
言归正传,我一直在问一些有趣到愚蠢的问题,这一次在我提出问题之前,我会在问之前仔细研究一下,以避免疲劳。
我的新更新代码块

val speaker = scenario("BasicSimu")
  .exec(
    fromClientSide
      .start(thePayload)
      .header(Authorization)(s"Bearer $TokenKey")
      .extract(.pk.some)(_ saveAs "theString")
      .sessionCombiner(SessionCombiner.pick("theString"))
      .endCheck(statusCode is Status.Code.OK)
  )
  .exec{
      session => 
        val responseString = session.attributes.get("theString").toString().slice(21,28)
        val newKeyVar = session.set("myKey",responseString)
      newKeyVar
  }
  .exec(
        http("Complete_Pairing") //the little body I made here
          .post(s"url")
          .header("Content-Type","application/json")
          .body(StringBody(s"""{
                      "pk": {"pairingKey": "#{newKeyVar}"}
                      }"""))
          .check(status.is(200))
  )
  .exec(fromClientSide.reconciliate(waitFor = StreamEnd))

字符串
由于这个代码块的引入,我需要pairingKey来把它取出来,所以我把它转换成一个String,然后把它切片,天哪,返回的是密钥。
继续调查,我自己回答了一些问题,除此之外,还有一些我不能回答,所以我在这里提出来。
1/不必说 pairingKey(也就是 responseString 保持它,在会话中)有它的用途,它用于传递到POST REST调用的主体中,是否有可能获得 responseString?使用aspect上面的代码块,未找到 responseString(可以理解,它在一个会话中),在示例代码中,您可以通过使用session.attributes.get直接转换它以获取值,但当插入REST时,我认为session.attributes.get不能直接转换。
简介:我看到了会议。设置,我会看到更多关于这一点
2/在第一个问题之后,我想到了两种方法,一种是将数据保存到csv中,另一种是直接调用值。我应该直接调用它以避免缓慢的处理吗?
3/昨天我确实问了一种方法来保持流活着查看结果,但现在我需要流运行,直到它得到响应0 OK,当REST API被制作时,它会立即弹出,尝试使用reconciliate(waitFor = NextMessage),但似乎流立即取消,不确定我是否正确转换它。
4/题外话,只是一个问题,我可以把我的演示(如果我设法在你的支持下实现这一点),到你的Github吗?

brvekthn

brvekthn1#

经过几天的调查,我已经完成了一个结合REST的ServerStream的完整代码,这是一个简单的制作,因为有很多方法可以与数据交互,我只是太菜鸟了,现在无法改进它。

val speaker = scenario("BasicSimulation")
  .exec(
    fromClientSide //read my prev questions to know my configuration
      .start(thePayload)
      .header(Authorization)(s"Bearer $TokenKey") 
      .extract(."yourParam".some)(_ saveAs "theString")
      .sessionCombiner(SessionCombiner.pick("theString"))
      .endCheck(statusCode is Status.Code.OK)
  ) 
  .exec(fromClientSide.reconciliate(waitFor = NextMessage)) //tell the stream to wait
  .exec{
    session => 
        //Extract will return a Some type data, cast it to String type, then slice it to get the key
        val responseString = session.attributes.get("theString").toString().slice(your, position)

        // Print out to verify see the value of the key, comment it later if no use
        println(s"the pairingKey is: ${responseString}")

        // Init a new session to get the value
        val newKeyVar = session.set("myKey", responseString)
    newKeyVar
  }

  .exec(  
      http("Complete_Pairing")
        .post("your url")
        
        //sample header
        .header("Authorization", s"$tokenREST")
        .header("Content-Type","application/json")
        
        //sample Body
        .body(StringBody("""{"pk": {"pairingKey": "#{myKey}"}"}"""))
        .check(status.is(200))
        .check(bodyString.saveAs("responseBody"))
        .check(jsonPath("$.session.id").saveAs("sessionID"))
        .check(jsonPath("$.at.token").saveAs("unpairToken"))
  )
  //uncomment this block to check your response
  // .exec{
  //     session => 
  //       val responseBody = session("responseBody").as[String]
  //       println(s"Response Body: $responseBody")
  //     session
  // }

字符串
我的一些评论可能是错误的理解,主要是你必须建立在你自己的,所以一些变量/对象可以是抽象的,所以如果有什么需要修复,请让我知道,这样我就可以立即修复

相关问题