Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the basic usage of STTP

2025-03-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the basic use of STTP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

If you use AkkaHttp as the backend of STTP to process list of url concurrently, you will get a result like list [FutureResponse [Either [ResponseError [io.circe.Error], T].

In general, we would prefer to return the type Future [(List [Exception], list [T])] to the upper-level caller.

The following code demonstrates how to set:

List[Future[Response[Either[ResponseError[io.circe.Error], NasaData]

Must be changed to

Future [(List [Throwable], list [NasaData]).

The first part of the tuple represents all the exception in the response, and the second part represents all normal data.

Several key technical points are:

1) Future.sequence is used to convert List [Future] into Future [List].

2) ignore other parts of STTP Response except body data, and convert response to Either [Throwable, NasaData].

3) after step 1, Throwable 2, the data type is already Future [List [Either [Throwable, NasaData]].

4) the idea from List [Either [Throwable, NasaData]] to (List [Throwable], List [NasaData]) is:

A) construct an empty Tuple (List [Throwable (), list [NasaData] ()) as an accumulator.

B) use List.folderLeft () to traverse the element, depending on whether left or right accumulates to _ 1 or _ 2 of the accumulator.

The article linked below demonstrates two conversion methods, one is Scala native handwriting, and the other is using CAT.

However, the logic of the statement case Left (err) = > acc.left.map (_ = > err) does not seem to match expectations. If there is a Left in list of either, it does not work.

Val erA: Either [String, Int] = Right (1) val erB: Either [String, Int] = Right (2) val erC: Either [String, Int] = Right (3) val elA: Either [String, Int] = Left ("error1") val elB: Either [String, Int] = Left ("error2") val listOfEither = List (erA, elA, erB, erC, elB) privateval eitherOfList: Either [Either, list [int]] = String List [Int] (List ()) .asInstanceOf [Either [String, List [Int]) ((acc, elem) = > {elem match {case Right (v) = > acc.map (l = > l: + v) case Left (e) = > acc.left.map (_ + e)}) println (s "eitherOfList: $eitherOfList")

The output is as follows, and there is no information in the error section.

EitherOfList: Right (List (1,2,3))

The following code demonstrates the processing code for concurrently issuing 100 RestAPi requests to get data, and the results are stored in Tuple, all failed and successful data, respectively.

Import io.circe.generic.auto._ import sttp.client._ import sttp.client.akkahttp.AkkaHttpBackend import sttp.client.circe._

Import scala.concurrent.Future import scala.concurrent.duration._ import scala.concurrent.Await import scala.concurrent.ExecutionContext.Implicits.global

Val NASA_API_KEY = "XXXXXXXXXXXXXXX" val baseUrl = s "https://api.nasa.gov/neo/rest/v1/neo/browse?api_key=${NASA_API_KEY}"

Var pageSize = 10 var pageNumb = 100

Case class Links (self: String, next: String) case class Page (number: Int, size: Int, total_elements: Int, total_pages: Int) case class NearEarthObject (absolute_magnitude_h: Double, designation: String) case class NasaData (links: Links, page: Page, near_earth_objects: List [NearEarthObject])

Implicit val sttpBackend = AkkaHttpBackend () val listOfFutureResult: list [FutureResponse [Either [ResponseError [io.circe.Error], NasaData] = (0 until pageNumb) .toList.map (pageIndex = > {val pageUrl = s "http://www.neowsapp.com/rest/v1/neo/browse?page=$pageIndex&size=$pageSize&api_key=${NASA_API_KEY}" basicRequest.get (uri" $pageUrl ") .response (asJson [NasaData]) .send ()})

/ / Note: Future [List]-- > List [Future] val futureOfList: FutureList [response [Either [ResponseError [io.circe.Error], NasaData] = Future.sequence (listOfFutureResult)

/ / Note: change response [Throwable [ResponseErr [io.circe.Error], NasaData]] to Either [Either, NasaData] val respOfEither2Either: response [Either [ResponseErr [io.circe.Error], NasaData]] = > Either [Throwable, NasaData] = resp= > {println (s "resp== > code:$ {resp.code.code} ${resp.statusText}") val newEither: Either [Throwable NasaData] = resp.body match {case Left (respErr) = > Left (respErr.getCause) case Right (nasaData) = > Right (nasaData)} newEither}

Val futureOfListOfEither: Future [List [Either [Throwable, NasaData] = futureOfList.map (listOfResp = > {listOfResp.map (respOfEither2Either)})

Val listOfEither2TupleOfList: List [Either [Throwable, NasaData]] = > (List [Throwable], List [NasaData]) = listE = > {listE.foldLeft (acc, eData) = > {eData match {case Right (nasaData) = > (acc._1, acc._2: + nasaData) case Left (t) = > (acc._1: + t, acc._2)})}

Val futureOfTupleOfList = futureOfListOfEither map listOfEither2TupleOfList

Val result = Await.result (futureOfTupleOfList, 1.minute) println (s "Exceptions in Response: $result._1") println (s "Data in Response: $result._2")

In addition, the default number of host connections for Akka HTTP is 32, so you need to modify the applicaiton.confg configuration to support more connections.

Akka.http.host-connection-pool.max-open-requests = 128What is the basic use of STTP? this is the end of the introduction. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report