10 Building an HTTP REST API CLI

 

This chapter covers

  • Deciding which tools and libraries to use
  • Designing the CLI
  • Declaring the commands
  • Implementing the commands
  • Implementing requests
  • Handling errors gracefully
  • Testing our CLI

Continuing the work we did in the previous chapter, we’ll write a CLI tool for the API service we wrote. Using a CLI tool, we’ll demonstrate another way to interact asynchronously in Rust by making HTTP requests with a separate service (which we also wrote). Our CLI tool will provide a convenient way to interact with our todo app backend (the API service). We’ll use Rust’s async features to showcase the basics of writing async Rust from the client side of a client-server relationship.

Writing CLI tools is one way to use software to solve problems for us, and building tools is how we avoid repetition, mistakes, and time wasted doing tasks computers are better suited for. Most versions of the Unix philosophy (which has several variations) include the “do one thing and do it well” tenet, which we’ll apply to our CLI tool. We’ll also make it easy to pipe the output from our CLI into another tool (another point from the Unix philosophy), making it possible to string commands together.

10.1 Deciding which tools and libraries to use

10.2 Designing the CLI

10.3 Declaring the commands

10.4 Implementing the commands

10.5 Implementing requests

10.6 Handling errors gracefully

10.7 Testing our CLI

Summary