Scala http client8/1/2023 ![]() ![]() I've been using akka-http, a collaboration between the spray and akka teams. Two Six years after originally responding to this post, I would have a different answer. It's got a nice DSL for building requests, supports both sync and async execution, as well as a variety of (un)marshalling types (JSON, XML, forms). (basic usage) val wsClient = NingWSClient()Ī little late to the party here, but I've been impressed with spray-client. Spray-client is a bit arcane in its syntax. The only serious ones currently are spray-client and Play! WS. I did a comparison of most major HTTP client libraries availableĭispatch, and a few others libraries, are not maintained anymore. The older 0.8 "dispatch-classic" docs helped me understand how they used the url builders, and gave some hints on how things are tied together that did carry forward to 0.9. The "Periodic" Table of operators, also 0.8 related. ScalaDoc for the 0.8 release a substantially different beast (today) than 0.9. I can't find a ScalaDoc link for the 0.9.* release, so I browse the source code for the 0.9.* release Here are some additional links I've found helpful: Though I can't find anything in docs to state that it is thread-safe general reading of source suggests that it is.ĭo be aware that the RequestBuilder's are mutable, and therefore are NOT thread-safe. once you get past that initial learning curve.ĭispatch's Http "builder" is immutable, and seems to work well in a threaded environment. I give the 0.9 release a solid thumbs up (so far!) on getting the job done very simply. In practice, the only thing I really had to look at was the RequestBuilder - everything else falling nicely into my understanding of HTTP. Dispatch 0.9.1 is a Scala wrapper around Ning's Async Http Client to fully understand what going on requires introducing one's self to that library. The read method is probably the most intriguing one from the whole series.I've recently started using Dispatch, a bit arcane (great general intro, serious lack of detailed scenario/use-case based docs). Val p = create("add.xml",person.toXml))Ĭonvert the response body from an Elem to the desired type Assuming we have a domain object, Person, that provides serialization and deserialization methods for xml a CRUD call sequence with the RestClientHelper trait would look as follows: To get an understanding how the RestClientHelper trait is supposed to be used let’s look at an example. The Int always represents the http status code, the Option represents the converted output if the service call was successful. The return type of these methods are of type T, Int or Tuple2]. The second parameter list is a function that converts the output of a rest service into the desired type. The first parameter list represents the rest call input such as target uri and request body or parameter map. The RestClientHelper will be a trait that offers the following client side CRUD methods:ĭef create(target: String, reqBody: String)(fromRespStr: String => T): Tĭef create(target: String, reqBody: Elem)(fromRespXml: Elem => T): Tĭef query(target: String, params: Seq)( fromRespStr: String => T): (Int, Option)ĭef query(target: String, params: Seq)( fromRespXml: Elem => T): (Int, Option)ĭef update(target: String, reqBody: String)(fromRespStr: String => T): Tĭef update(target: String, reqBody: Elem)(fromRespXml: Elem => T): Tĭef update(target: String, reqBody: String): Intĭef update(target: String, reqBody: Elem): IntĪs you can see, most methods consist of two parameter lists. With these methods in place we will be able to call and therefore test restful service APIs very easily. The goal of this blog is it to create a RestClientHelper that offers a set of generic restful client methods and explain how they’re implemented using Dispatch. ![]() Support for security aspects such as https and basic/digest authentication.Support for reading http status codes to test results of erroneous responses like 404 (Not Found). ![]() Support for XML and Json as input and output payloads.Support for CRUD operations with http’s POST, GET, PUT and DELETE methods.These will most likely apply for many other rest services as well: In this blog I’ll decipher it for you and show how easy it is to test restful services with mere oneliners.īefore we dive into Dispatch’s mysterious DSL let’s look at the prerequisites I had for testing my restful service. This DSL, however, is not very well documented and rather hard to decipher due to it’s heavy usage of symbolic method names but nevertheless highly appealing when understood. My search led me to Dispatch, which is a highly compact Scala DSL wrapper around Apache’s reliable HttpClient. For testing a restful service API I was looking for a lean library, which would allow me to test CRUD operations of rest services with as little code as possible. ![]()
0 Comments
Leave a Reply.AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |