将 GRPC 响应中的响应值传递到 Scala Gadling 中的 REST API

问题描述 投票:0回答:1

希望你今天表现出色

切入正题,我一直在问一些有趣又愚蠢的问题,这一次在我提出一些问题之前,我会仔细研究一下,然后再提出以避免疲劳。 Ngl 这对我来说是一次有趣又痛苦的经历

我的新更新代码块

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来将其取出,因此我将其转换为字符串,然后对其进行切片,天哪,密钥返回了。

继续调查,我自己也回答了一些问题,另外还有一些我无法自我回答,所以我在这里提出来。

1/ 不用说 pairingKey (又名 responseString 将其保留在会话中)有其目的,它用于传递到 POST REST 调用的正文中,是否可以获取 responseString ?使用上面的代码块,找不到responseString(可以理解,它在会话中),在示例代码中,您可以使用 session.attributes.get 直接转换它来获取值,但是当插入 REST 时,我认为session.attributes.get 无法直接转换。

简要:我看到了 session.set,我会看到更多关于此的内容

2/ 在第一个问题之后,我想到了两种处理方法,一种是将数据保存到csv,另一种是直接调用值。我应该直接调用它以避免处理缓慢吗?

3/ 昨天我确实询问了一种保持流活动以查看结果的方法,但现在我需要流运行直到它得到响应 0 OK,当 REST API 生成时它会立即弹出,尝试使用 reconciliate (waitFor = NextMessage) 但似乎流立即取消,不确定我是否正确投射。

4/ 脱离主题,只是担心,我可以将我的演示推送到您的 Github(如果我设法在您的支持下实现这一目标)吗?

scala grpc gatling
1个回答
0
投票

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

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
  // }

我的一些评论可能是错误的理解,大多数情况下你必须自己构建,因此某些变量/对象可以是抽象的,所以如果需要修复某些内容,请告诉我,以便我可以立即修复

© www.soinside.com 2019 - 2024. All rights reserved.