Drupal is a registered trademark of Dries Buytaert
drupal 11.3.7 Update released for Drupal core (11.3.7)! drupal 11.2.11 Update released for Drupal core (11.2.11)! drupal 10.6.7 Update released for Drupal core (10.6.7)! drupal 10.5.9 Update released for Drupal core (10.5.9)! cms 2.1.1 Update released for Drupal core (2.1.1)! drupal 11.3.6 Update released for Drupal core (11.3.6)! drupal 10.6.6 Update released for Drupal core (10.6.6)! cms 2.1.0 Update released for Drupal core (2.1.0)! bootstrap 8.x-3.40 Minor update available for theme bootstrap (8.x-3.40). menu_link_attributes 8.x-1.7 Minor update available for module menu_link_attributes (8.x-1.7). eca 3.1.1 Minor update available for module eca (3.1.1). layout_paragraphs 2.1.3 Minor update available for module layout_paragraphs (2.1.3). ai 1.3.3 Minor update available for module ai (1.3.3). ai 1.2.14 Minor update available for module ai (1.2.14). node_revision_delete 2.0.3 Minor update available for module node_revision_delete (2.0.3). moderated_content_bulk_publish 2.0.52 Minor update available for module moderated_content_bulk_publish (2.0.52). klaro 3.0.10 Minor update available for module klaro (3.0.10). klaro 3.0.9 Minor update available for module klaro (3.0.9). layout_paragraphs 2.1.2 Minor update available for module layout_paragraphs (2.1.2). geofield_map 11.1.8 Minor update available for module geofield_map (11.1.8).

post_api

1 sites Security covered
View on drupal.org
POST API

INTRODUCTION

Post API is a module written for developers that would like to utilize Drupal
Queue API for POSTing to specified endpoint(s).

POST API QUEUE UI

This module provides the most basic Queue management UI from which you can
review and manually process the queue. Visit /admin/config/post-api/queue
or navigate to (Admin > Configuration > Web Services > Post API > Queue).

DISPATCHED EVENTS

Post API module dispatches two events.

1. post_api_queue_item_processed_event

Event is dispatched once single queue item is processed by QueueWorker.
The Event provides the following data to Event Subscribers:

  • API request response code
  • Queue item data

2. post_api_queue_processing_complete_event

Event is dispatched once the Post API queue processing is complete. The Event
provides the following data to Event Subscribers:

  • Number of queue items at the beginning of queue processing
  • Number of queue items released back to the queue (unprocessed/errored)
    at the end of queue processing

An example of a custom Event Subscriber use case could be: "A Slack notification
should be sent if the Post API queue worker did not process any items during
queue execution." Your use case will differ.

REQUIREMENTS

This module has no dependencies.

NOTE: Core has a queue bug, where claimItem() method in the database and memory
queues does not use expire lease timestamps correctly. Since this module uses
DatabaseQueue, its proper function depends on the following core patch being
applied - https://www.drupal.org/project/drupal/issues/2893933#comment-13413895

INSTALLATION

Install post_api module via Composer.

composer require drupal/post_api

Example use case: As an engineer I need to post content of a new or updated node
to an endpoint, using API key authentication, and I want to use Queue API, so
that I can trigger my content sync on Cron runs.

STEP 1

Enable Post API module.

STEP 2

In the custom module, add hook_entity_insert() and hook_entity_update() that
will call a custom helper function that uses queue service from Post API to
queue desired info for later processing.

Example code:

/**
 * Implements hook_entity_update().
 *
 * Queues updated items for sync.
 */
function post_api_entity_update(EntityInterface $entity) {
  _post_api_add_entity_to_queue($entity);
}

function _post_api_add_facility_to_queue(EntityInterface $entity) {
  // Let's assume we need to queue only nodes of type example_bundle_ and
  // example_bundle_2.
  $entity_bundles = [
    'example_bundle_1',
    'example_budnle_2',
  ];

  if ($entity->getEntityTypeId() === 'node'
    && in_array($entity->bundle(), $entity_bundles)) {
    // Load Post API Queue service.
    $queue = Drupal::service('post_api.add_to_queue');

    // Form queue item data.
    // (Optional) Queue item's Unique ID. This is needed if your use case
    // requires removing duplicate items from queue.
    $data['uid'] = 'GENERATE_UNIQUE_ID_BASED_ON_ENTITY_PROPS';

    // All $data properties listed below are required for successful queue item
    //processing.
    // NOTE: endpoint_path should only contain endpoint path, not the host.
    $data['endpoint_path'] = 'your-endpoint-path';

    // Set payload. Default payload provided by this module is empty. You
    // must set your own payload.
    // Entity fields (updated and original) can be compared and processed in
    // order to structure the payload array.
    $data['payload'] = _post_api_get_custom_payload($entity);

    // Only add to queue if payload is not empty.
    // If its empty, it means that there is no new information to send
    // to the endpoint.
    if (!empty($data['payload'])) {
    // Second parameter is a value of $flagDedupe,
    // which defines whether or not pre-existing queue items
    // with same unique id should be removed from queue before
    // before adding current item.
      $queue->addToQueue($data, TRUE);
    }
  }
}

Check how VA.gov
is using this module.

STEP 3

Set post_api_endpoint_host and post_api_apikey variables in settings.php.
Once variables are set and cache is cleared, you may check if values are
recognized by Post API at /admin/config/post-api/config.

$settings['post_api_endpoint_host'] = getenv('API_URL') ?: FALSE;
$settings['post_api_apikey'] = getenv('API_KEY') ?: FALSE;

STEP 4

Configure Cron to run at desired intervals.

CONFIGURATION

See available settings at /admin/config/post-api/config.

Settings include:

  • API rate limit per minute
  • Pause processing on Cron
  • QueueWorker max runtime (sec)

Activity

Total releases
1
First release
Feb 2025
Latest release
1 year ago
Release cadence
Stability
100% stable

Releases

Version Type Release date
2.0.6 Stable Feb 25, 2025