Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

recreate endpoints for signage, sitenow, etc to consume #7550

Closed
4 of 5 tasks
Tracked by #7487
briand44 opened this issue Apr 8, 2024 · 6 comments · Fixed by #7647
Closed
4 of 5 tasks
Tracked by #7487

recreate endpoints for signage, sitenow, etc to consume #7550

briand44 opened this issue Apr 8, 2024 · 6 comments · Fixed by #7647
Assignees

Comments

@briand44
Copy link
Contributor

briand44 commented Apr 8, 2024

Story

As an Emergency site owner, I would like Hawk Alert information to get consumed by Signage and SiteNow websites so folks can quickly and easily be made aware of emergencies without having to go to emergency.uiowa.edu.

Additional Info

Currently seeing feeds at the following endpoints:

Looks like signage is using https://emergency.uiowa.edu/api/active.json
Looks like the D7 uiowa_alerts module uses https://emergency.uiowa.edu/api/active.json
Looks like D10 uiowa_alerts module is using https://emergency.uiowa.edu/api/active.json

Is anything using https://emergency.uiowa.edu/api/active.xml? - Not that we found

We should adjust the more_info_link in the endpoint to be emergency.uiowa.edu rather than e.uiowa.edu

Proposed Solution

Tasks

@briand44
Copy link
Contributor Author

Grooming conversation... After some conversation, we decided we want to do a probe to see if we can understand who is using these endpoints. Is it just us or are others. Is it just the json endpoint or is there usage on the xml endpoint as well. Check splunk logs to see where referring traffic is coming from to understand. Shouldn't need to look back very far. Size the probe as a medium.

@briand44 briand44 added the blocked Blocked by another issue label Apr 10, 2024
@briand44 briand44 added blocked Blocked by another issue and removed blocked Blocked by another issue labels Apr 11, 2024
@briand44
Copy link
Contributor Author

Grooming conversation... haven't found usage of the XML endpoint so we think we can retire that with communication. Seems like we are in favor of creating a new endpoint to replace the current JSON endpoint and update Signage/SiteNow and help others update to the new endpoint. We'll need to communicate this out.

Once we have the new endpoint we can have this live in a non-production environment for folks to test with. Could communicate when a test alert is happening. Communicate launch date for folks to switch to the new endpoint. We may need to use stage for the v3 site for endpoint testing and revert back to having dev available for the D7 site.

Need to include the more_info_link in the endpoint. That link should be set to emergency.uiowa.edu. This is not a field that needs to be edited or changed per alert node. This is more of a site wide setting. Possibly a pseudo field?

Take a look at OnIowa events work for previous JSON API related work.

We will need to test caching with this to make sure endpoint is getting updated as content gets created/edited.

Scoped this to getting the endpoint setup with correct data and having it reviewed/tested. Additional slices can be created to get it available in stage and to handle communication.

@briand44 briand44 added medium story pointing and removed needs grooming blocked Blocked by another issue labels Apr 29, 2024
@joewhitsitt joewhitsitt self-assigned this May 2, 2024
@joewhitsitt
Copy link
Contributor

Did a short dive into create a custom resource as there is only one resource per entity bundle by default. It worked filtering on field_hawk_alert_complete but it was not easily apparent how to make it visible to json api extras where i previously was able to decide what fields should be visible, easy ability to override labels or enhance value output. Because we only have a single use-case so far for this resource (show active) and we want to mimic the old feed as much as possible I don't think this is the time to carve off this kind of custom approach.

Looks like there will already be some unrelated custom work to account for more_info_link

I was following this article which got me up and running within a few minutes.

routing.yml

# Defines a route for a collection containing featured articles nodes.
emergency_core.jsonapi_resources.active_hawk_alerts:
  # %jsonapi% is a placeholder for the JSON:API base path, which can be
  # configured in a site's services.yml file.
  path: '/%jsonapi%/active'
  defaults:
    # Every JSON:API resource route must declare a _jsonapi_resource. The
    # value can either be a class or a service ID. Unlike the _controller
    # route default, it is not possible to declare a method name to be called.
    _jsonapi_resource: Drupal\emergency_core\Resource\ActiveHawkAlerts
    _jsonapi_resource_types: ['node--hawk_alert']
  requirements:
    _permission: 'access content'
<?php

namespace Drupal\emergency_core\Resource;

use Drupal\Core\Cache\CacheableMetadata;
use Drupal\jsonapi\ResourceResponse;
use Drupal\jsonapi_resources\Resource\EntityQueryResourceBase;
use Drupal\node\NodeInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Processes a request for a collection containing active hawk_alert nodes.
 */
class ActiveHawkAlerts extends EntityQueryResourceBase {

  /**
   * Process the resource request.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The request.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   *
   * @return \Drupal\jsonapi\ResourceResponse
   *   The response.
   */
  public function process(Request $request): ResourceResponse {
    $cacheability = new CacheableMetadata();

    /** @var \Drupal\Core\Entity\ContentEntityType $entity_type */
    $entity_type = $this->entityTypeManager->getDefinition('node');
    /** @var string $bundle_field */
    $bundle_field = $entity_type->getKey('bundle');
    /** @var string $status_field */
    $status_field = $entity_type->getKey('status');

    $entity_query = $this->getEntityQuery('node')
      ->condition($bundle_field, 'hawk_alert')
      ->condition('field_hawk_alert_complete', 0)
      ->condition($status_field, NodeInterface::PUBLISHED);

    $cacheability->addCacheContexts(['url']);

    $paginator = $this->getPaginatorForRequest($request);
    $paginator->applyToQuery($entity_query, $cacheability);

    $data = $this->loadResourceObjectDataFromEntityQuery($entity_query, $cacheability);

    $pagination_links = $paginator->getPaginationLinks($entity_query, $cacheability, TRUE);

    /** @var \Drupal\jsonapi\CacheableResourceResponse $response */
    $response = $this->createJsonapiResponse($data, $request, 200, [], $pagination_links);
    $response->addCacheableDependency($cacheability);

    return $response;
  }

}

@joewhitsitt
Copy link
Contributor

With JSON API Defaults as a submodule of JSON API Extras I was able to simplify the URL structure compared to #6580. I know the app-devs have hardcoded that long URL string for their app, but we could support a simpler URL if we were to revisit.

@joewhitsitt
Copy link
Contributor

Emergency website active hawk alerts endpoint changes

  • Removed the /api/active.xml endpoint option.
    • We couldn't determine significant usage for this endpoint. Consumers are encouraged to switch to the JSON format.
  • Changed the /api/active.json endpoint to /api/active
    • It is still JSON but without the file extension. The endpoint won't resolve with .json appended.

The other changes are in order to follow the JSON 1.0 specification:

  • The outside wrapper uihphawkalert becomes data.
  • The inside wrapper hawkalert becomes attributes.
  • date changes from UNIX timestamp to ISO-8601 YYYY-MM-DDThh:mm:ssTZD
  • more_info_link has been updated to output the website URL with https:// prefix.
    • Public Safety would like to deprecate use of e.uiowa.edu going forward.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants