liveBook · Manning

GetOperation method

As explained in API Design Patterns

The GetOperation method is an API method that retrieves an operation resource and reports errors encountered while retrieving that resource separately from errors produced by the operation.

GetOperation retrieves an operation resource by identifier so clients can determine whether a long-running operation is complete, inspect its result, or monitor its metadata. It should distinguish retrieval failures—such as missing or inaccessible operation resources—from errors produced by the operation itself. section 10.3.3 section 10.3.4

Use the method with these design rules:

  • Return the operation resource, including its done state, result, and typed metadata. section 10.3.3
  • Let clients control polling frequency and decide when to stop; repeated requests trade network and compute cost for shorter completion delay. section 10.3.3
  • Throw an HTTP error only when retrieving the operation fails; represent an underlying operation failure inside the retrieved result as an OperationError. section 10.3.4
  • Give OperationError a unique machine-readable code, a human-readable message, and structured details; do not require clients to parse message text. section 10.3.4

Polling until a chat room is created

CreateChatRoom returns an operation. The client repeatedly calls GetOperation with operation.id, waits between requests, and returns operation.result when done is true. A client library may perform this polling automatically, optionally using exponential back-off. section 10.3.3

Listing 10.5 Polling for updates about an LRO until it’s done
async function createChatRoomAndWait(): Promise<ChatRoom> {
  let operation: Operation<ChatRoom, CreateChatRoomMetadata>;
  operation = CreateChatRoom({                                 #1
    resource: { title: "Cool Chat" }
  });
 
  while (!operation.done) {                                    #2
    operation = GetOperation<ChatRoom, 
      CreateChatRoomMetadata>({                              #3
      id: operation.id
    });
    await new Promise( (resolve) => {                          #4
      setTimeout(resolve, 1000)
    });
  }
  
  return operation.result as ChatRoom;                         #5
}

Follow the complete polling loop, including the operation identifier, one-second delay, completion check, and result extraction. section 10.3.3

Listing 10.9 Checking for an error or returning the result
function isOperationError(result: any):
    result is OperationError {                           #1
  const err = result as OperationError;
  return err.code !== undefined && err.message !== undefined;
}
 
function createChatRoomAndWait(): Promise<ChatRoom> {
  let operation: Operation<ChatRoom, CreateChatRoomMetadata>;
  operation = CreateChatRoom( { resource: { title: "Cool Chat" } });
  operation = WaitOperation({ id: operation.id });
  if (isOperationError(operation.result)) {              #2
    // ...
  } else {
    return operation.result as ChatRoom;
  }
}

Follow the client-side distinction between an OperationError result and a successful resource result. section 10.3.4

How API Design Patterns covers it

How deep it goes
Defines the method contract, polling trade-offs, error boundary, and progress usage, but defers cancellation and alternative waiting methods.
Examples
Demonstrates CreateChatRoom polling, AnalyzeMessages progress metadata, and client-side OperationError discrimination.
What it assumes
Assumes familiarity with long-running operations and operation resources while demonstrating polling and typed result-or-error handling.
What you can do after
Apply GetOperation to poll an asynchronous request, inspect progress, and handle retrieval failures separately from operation failures.
How it connects
Connects long-running operations with polling, exponential back-off, asynchronous error handling, operation metadata, and progress monitoring.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of API Design Patterns. The section links point to the parts of the book it draws on.