concept this pattern in category microservice

appears as: this pattern, This patterns
API Design Patterns

This is an excerpt from Manning's book API Design Patterns.

To address this concern, we can use an interesting tactic where we switch our focus away from lots of custom actions and instead shift toward a set of standard actions shared across the entire API (or in many cases across all APIs). Once we agree on a set of standard actions (most commonly these are Create, Get, List, Update, and Delete), we can design our API in terms of the things on which we can perform these standard actions. In other words, we focus on “nouns” (and relationships between them) instead of “verbs”. APIs that follow this pattern of thinking in terms of things rather than actions are called resource-oriented APIs.

For example, if you want to add debug logging to your system, you’ll likely want one and only one way to log messages. There are lots of ways you could do this (for example, using a single shared global variable), but there happens to be a design pattern aimed at solving this software problem. This pattern, described in the seminal work Design Patterns (Gamma et al., 1994), is called the Singleton pattern, and it ensures that only a single instance of a class is created. This “blueprint” calls for a class with a private constructor and a single static method called getInstance(), which always returns a single instance of the class (it handles creating that single instance if and only if it doesn’t exist yet). This pattern is not at all complete (after all, what good is it to have a singleton class that does nothing?), however it’s a well-defined and well-tested pattern to follow when you need to solve this small compartmentalized problem of always having a single instance of a class.

As we’ll learn in Chapter 8, the Import/Export pattern is aimed at problems just like this one: we have some amount of data in our API service and consumers want a way of getting it out (or in). However, unlike the Pagination pattern that we discussed before, this pattern will rely on some others, such as the Long-running operations pattern (discussed in Chapter 7), to get the job done. Let’s start by defining the API and then looking more closely at how each piece works together. Just like before, keep in mind that we won’t go into all the details of every aspect of the pattern, but instead will try to give a high-level view of the relevant pieces.

Listing 2.4. API to export messages using a design pattern.
abstract class Twapi {
  static version = "v1";
  static title = "Twapi API";
 
  @post("/{parent=users/*}/messages:export")
  ExportMessages(req: ExportMessagesRequest):
      LongRunningOperation<ExportMessagesResponse, ExportMessagesMetadata>;  // #A
}
 
interface ExportMessagesRequest {
  // The parent user of the messages to be exported.
  parent: string;
  outputConfig: MessageOutputConfig;  // #B
}
 
interface MessageOutputConfig {
  destination: Destination;  // #C
  compressionConfig?: CompressionConfig;  // #D
  encryptionConfig?: EncryptionConfig;
}
 
interface ExportMessagesResponse {
  outputConfig: MessageOutputConfig;  // #E
}
 
interface ExportMessagesMetadata {
  // An integer between 0 and 100 representing the progress of the operation.
  progressPercent: Number;  // #F
}

So what’s so great about this pattern? First, by relying on an encapsulated “output configuration” interface, we’re able to accept the various parameters at request-time and then re-use this same content in our response back as confirmation to the consumer. Next, inside this configuration we’re able to define several different configuration options, which we’ll look at in more detail in Listing 2.X below. Finally, we’re able to keep track of the progress of the export operation using the long-running operation’s metadata information, which stores the progress of the operation as a percentage (0% being “not started” and 100% being “complete”).