Command-line REST client: building a working exampleYou’ve looked into a number of interesting concepts about Scala in this chapter, and it’ll be nice to see some of these concepts in action together. In this section you’ll build a command-line-based REST client in Scala script.You’re going to use the Apache HttpClient library to handle HTTP connections and various HTTP methods.
To make a REST call to a RESTful service, you have to be aware of the operations supported by the service. To test your client you need a RESTful web service. You could use free public web services to test the client, but to have better control of the operations on the service you’ll create one. You could use any REST tool or a framework to build the REST service. I’ll use a Java servlet (Java developers are familiar with it) to build the service to test the REST client. Understanding how the service is implemented isn’t important for this example.
The simple way to create a RESTful service for now is to use a Java servlet, as shown in the following listing.
Introducing HttpClient library
HttpClient encapsulates each HTTP method type in an object, and they’re available under the org.apache.http.client.methods package. In this script you’re going to use four types of requests: GET, POST, DELETE, and OPTIONS.11 The previous example implemented only GET, POST, and DELETE because the web container will automatically implement the OPTIONS method. HttpClient provides a default client and is good enough for our purpose. To execute an HTTP DELETE method you have to do the following:
Building the client, step by step (p49)
Now, because the service is up and running, you can focus on the client script. To make the script useful, you have to know the type of operation (GET or POST), request parameters, header parameters, and the URL to the service. The request parameters and header parameters are optional, but you need an operation and a URL to make any successful REST call:
The request parameters and header parameters are determined by a prefix parameter, –d or –h. One way to define a parseArgs method to parse request and header parameters is shown in the following listing.
- Listing 2.6 Parsing headers and parameters passed to the program
The last interesting thing I’d like to mention about the parseArgs method is the Map. A Map is an immutable collection of keys and values. Chapter 4 discusses Map in detail. In this example you’re creating a Map of parameter name(-d or –h) and listing all the parameters as values. When you pass a tuple of two elements to Map, it takes the first element of the tuple as the key and the second element as the value:
For now you’ll support only four types of REST operations: POST, GET, DELETE, and OPTIONS, but I encourage you to implement other HTTP methods like PUT and HEAD. To check what type of operation is requested, you can use simple pattern matching:
- Listing 2.7 Preparing a GET request and invoking the REST service
- Listing 2.8 Preparing a POST request and invoking the REST service
The following listing contains the complete RestClient.scala script.
- Listing 2.9 RestClient.scala
To run the REST client you can use any build tool that can build Scala code. This example uses a build tool called simple build tool (SBT). You’ll learn about this tool in detail in chapter 6, but for now go ahead and install the tool following the instructions from the SBT wiki (http://www.scala-sbt.org). Take a look at the codebase for this chapter for an example.
* [ Gossip in Java(2) ] 網路 : 程式實例 (簡單 HTTP 伺服器)