chapter six

6 Money converter: CLI around an HTTP call

 

This chapter covers

  • Writing a CLI
  • 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

A long list of websites nowadays exposes useful APIs that can be called via HTTP. Common examples are the famous open-source system for automating deployment Kubernetes, weather forecast services, international clocks, social networks, online databases such as BoardGameGeek or the Internet Movie Database, content managers like WordPress, the list is long. A small number 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 favourite search engine, it still takes an extra click to access the first link that isn't an advert, 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, which reduces the number of command lines and helps automate more of our work.

Requirements

Limitations

6.1 Business definitions

6.1.1 money.Convert converts money

6.2 How to represent money

6.2.1 Decimal implementation

6.2.2 Currency value object

6.2.3 NewAmount

6.3 Conversion logic

6.3.1 Apply change rate

6.3.2 Validate result

6.4 Command-Line Interface

6.4.1 Flags and arguments

6.4.2 Parse into business types

6.4.3 Stringer

6.4.4 Convert

6.5 Call the bank

6.5.1 Dependency injection - the theory

6.5.2 ECB package

6.5.3 HTTP call, easy version

6.5.4 Parse the response

6.5.5 Use in the money package

6.5.6 Dependency injection in main

6.5.7 Sharing the executable