liveBook · Manning

Chapter 8: Integrate third-party APIs

What you will learn in chapter 8 of Rust Web Development

The section introduces considerations for integrating external HTTP calls, including failure handling, concurrent operations, and HTTP client choices.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The section introduces considerations for integrating external HTTP calls, including failure handling, concurrent operations, and HTTP client choices.

Key terms: route handler, Tracing, HTTP responses, join!, async operations, concurrently, parallel, thread, Hyper, Reqwest

8.1 Preparing the codebase

This section explains the choice of an asynchronous HTTP client and the costs of using one that depends on a different runtime.

Key terms: Reqwest, HTTP client, asynchronous, runtime, Tokio runtime, operating system thread, runtime complexity, bottlenecks

8.1.1 Picking an API

The section selects a profanity-checking API for the question-and-answer service and explains how to investigate its requirements, responses, and integration choices.

Key terms: third-party APIs, Bad Words API, profanity check, profane words, questions and answers, website setting, API documentation, API key, header, JSON

8.1.2 Getting to know our HTTP crate

This section introduces a small-project workflow for exploring Reqwest and testing an API call before integrating it into the application.

Key terms: Cargo project, Rust Playground, happy path, borrow checker, ownership model, HTTP POST request, API key, parameters, Reqwest, documentation

8.1.3 Adding an example HTTP call with Reqwest

This section adds an example Reqwest call to the question handler and identifies the error-type mismatch that prevents it from compiling.

Key terms: Reqwest, Cargo.toml, JSON feature set, add_question, Bad Words API, censored sentence, warp::Reject, Reqwest error, try!, handle-errors crate

8.1.4 Handling errors for external API requests

This section adapts Reqwest errors to Warp, detects unsuccessful API responses, and separates client-facing errors from diagnostic logs.

Key terms: reqwest::Error, Error enum, Reject, map_err, Result, send, Response, error_for_status, 4xx, 5xx

8.2 Deserializing JSON responses to structs

This section explains why typed API responses are useful and how to gather information for defining their structs.

Key terms: typed responses, BadWordsError, BadWordsResponse, JSON, type-check, API documentation, curl, prototyping, structs

8.2.1 Gathering API response information

This section shows how to inspect API responses and use their contents to guide Rust type design and error handling.

Key terms: Transform tool, deserialize, JSON, struct, Serde JSON, Value enum, generic JSON, error code, pretty-print, res.is_success

8.2.2 Creating types for our API responses

This section introduces structs for API responses and handles external API errors while keeping internal details from clients.

Key terms: structs, API responses, success path, response.status, is_client_error, is_server_error, APILayerError, 4xx, 5xx, internal and external errors

8.3 Sending questions and answers to the API

This section introduces the chosen approach for sanitizing question and answer content through a third-party API.

Key terms: profane words, sanitized, API

8.3.1 Refactoring the add_question route handler

This section extracts profanity checking into a reusable module and updates question creation to check text before storing it.

Key terms: profanity.rs, mod profanity, public function, error handling, title, content, profanity check, censored content, cargo run, curl

8.3.2 Making profanity checks for updating questions

This section distinguishes the data types used to update an existing question and add a new one.

Key terms: update_question, add_question, Question, NewQuestion, id

8.3.3 Updating the add_answer route handler

The add_answer handler checks answer content for profanity before content is added to the database.

Key terms: add_answer, profanity check, blocklist, censored version

8.4 Handling timeouts and multiple requests at once

This section introduces retrying timed-out external API calls and distinguishes concurrent from parallel HTTP calls using Tokio.

Key terms: timeout, retries, 500 server error, tokio::join!, tokio::spawn, futures, concurrently, parallel, task

8.4.1 Implementing a retry for external HTTP calls

This section adds exponential-backoff retries to the profanity HTTP client using Reqwest middleware and adjusts error handling.

Key terms: exponential backoff, retry, timeout, reqwest_retry, reqwest_middleware, Reqwest, profanity abstraction, reqwest_middleware::Error, reqwest::Error, handling-errors

8.4.2 Executing futures concurrently or in parallel

This section distinguishes concurrency from parallelism, compares Tokio approaches for running HTTP calls, and reflects on Rust’s support for the API integration.

Key terms: concurrency, parallelism, tokio::spawn, tokio::join!, futures, context switching, return type, strictly typed, crate, retry

8.5 Summary

Where this is used later

Concepts explained in this chapter

← Chapter 7: Add a database to your application Chapter 9: Add authentication and authorization →

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.