9 Custom methods

 

This chapter covers

  • Why standard methods can’t cover all possible scenarios
  • Using custom methods for cases where side effects are necessary
  • How to use stateless custom methods for computation-focused API methods
  • Determining the target for a custom method (resource versus collection)

Often, there will be actions we need to perform on our API resources that won’t fit nicely into one of the standard methods. And while these behaviors could technically be handled by the resource’s standard update method, the behavioral requirements of many of these operations would be quite out of place for a standard method, leading to a surprising, confusing, and overly complex interface. To address this, this pattern explores how to safely support these actions on resources in a web API while maintaining a simple, predictable, and functional API using what we’ll call custom methods.

9.1 Motivation

9.1.1 Why not just standard methods?

9.2 Overview

9.3 Implementation

9.3.1 Side effects

9.3.2 Resources vs. collections

9.3.3 Stateless custom methods

9.3.4 Final API definition

9.4 Trade-offs

9.5 Exercises

Summary