6 Money converter: CLI around an HTTP call

 

This chapter covers

  • Writing a command-line interface
  • Making an HTTP call to an external URL
  • Mocking an HTTP call for unit tests
  • Grasping floating-point precision errors
  • Parsing an XML-structured string
  • Inspecting error types

Thousands of websites nowadays expose useful APIs that can be called via HTTP. Common examples include the famous open source system Kubernetes for automating deployment, weather forecast services, international clocks, social networks, online databases such as BoardGameGeek or the Internet Movie Database (IMDb), and content managers such as WordPress. Some of them also provide a command-line tool that calls these APIs. Why? Even though nice and clickable interfaces are wonderful, they are still very slow. Here’s an example: when we look up a sentence in our favorite search engine, it still takes an extra click to access the first link that isn’t an advertisement or the first one we haven’t opened yet. The terminal shell, on the other hand, allows us to manipulate inputs and outputs of programs—and even to combine them—reducing the number of command lines and helping automate more of our work.

6.1 Business definitions

6.1.1 Converting money

6.2 Representing money

6.2.1 Floating-point numbers

6.2.2 Back to money

6.2.3 Floating-point number operations

6.2.4 Implementing decimals

6.2.5 Currency value object

6.2.6 NewAmount

6.3 Applying conversion logic

6.3.1 Applying an exchange rate

6.3.2 Validating the result

6.4 Writing the CLI

6.4.1 Flags and arguments

6.4.2 Parse into business types

6.4.3 Stringer

Summary