Skip to content

feat: datafiles action type json #1790

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

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

nitrosx
Copy link
Member

@nitrosx nitrosx commented Mar 18, 2025

Description

This PR add the option to create a datafile action in json format.
It will submit a request to the specified URL with the payload in json format in the body. The results from the request will be saved by the browser in a file with the named specified in the action configuration.
The structure of the payload can be specified in configuration

There is the matching backend PR to update the frontend config file: 1800

Motivation

sciwyrm (Jupiter notebook generator) accepts only requests formatted as json.
ESS is currently deploying it in their infrastructure.

Changes:

Please provide a list of the changes implemented by this PR

  • datafiles actions controller

Tests included

  • Included for each change/fix?
  • Passing? (Merge will not be approved unless this is checked)

Documentation

  • swagger documentation updated [required]
  • official documentation updated [nice-to-have] - in progress

official documentation info

If you have updated the official documentation, please provide PR # and URL of the pages where the updates are included

Backend version

  • Does it require a specific version of the backend
  • which version of the backend is required:

Summary by Sourcery

New Features:

  • Introduces a new 'json-download' action type for datafiles, enabling requests to be sent with a JSON payload.

@nitrosx nitrosx self-assigned this Mar 18, 2025
Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @nitrosx - I've reviewed your changes - here's some feedback:

Overall Comments:

  • Consider adding error handling to the fetch call in type_json.
Here's what I looked at during the review
  • 🟡 General issues: 1 issue found
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Complexity: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@@ -159,21 +161,27 @@ export class DatafilesActionComponent implements OnInit, OnChanges {
return true;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion (bug_risk): Consider handling the fetch promise to manage asynchronous errors.

The current implementation calls fetch without awaiting its result or handling potential errors. This might lead to unhandled rejections or race conditions if the response is used later. Consider refactoring this method as an async function and using async/await, or at least adding a .then/.catch to deal with the promise resolution.

Suggested implementation:

  async handleAction() {
    try {
      const response = await fetch(this.actionConfig.url, {
        method: this.actionConfig.method || "POST",
        headers: { "Content-Type": "application/json" },
        body: JSON.stringify(data),
      });

      if (!response.ok) {
        throw new Error(`Network response was not ok: ${response.statusText}`);
      }

      return true;
    } catch (error) {
      console.error("Fetch error:", error);
      return false;
    }

Ensure that any callers of handleAction() are updated to handle the Promise returned by this async method.

@nitrosx nitrosx changed the title Datafiles action type json feat: datafiles action type json Mar 18, 2025
@bpedersen2
Copy link
Contributor

Yes, I am looking forward, as this makes writing action consumers much easier.

Copy link
Contributor

@bpedersen2 bpedersen2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ai comments are not related to this change, but should probably be considered fr a separate change.

@Junjiequan Junjiequan force-pushed the swap-4596-datafiles-action-json branch from cbf4891 to f1fa6ff Compare March 28, 2025 11:33
@nitrosx nitrosx requested a review from Junjiequan March 31, 2025 12:31
@nitrosx nitrosx requested a review from Junjiequan April 1, 2025 11:23
nitrosx added a commit to SciCatProject/scicat-backend-next that referenced this pull request Apr 7, 2025
## Description
This PR update the frontend configuration with the updated examples of
datafile actions.
Now there are two datafiles actions: `form` and `json-download`. The
`form` option will submit the information as a submitted form, while the
`json-download` will POST the information to the specified URL in json
format and expect the results to be saved in a file. The file name is
specified in the action configuration

Matching FE PR:
[1790](SciCatProject/frontend#1790)

## Motivation
While integrating FE with sciwyrm (jupyter notebook generator), we
realized that FE was sending the data as submitted form, while the
generator needed a json format. Also the structure of the data was
different.

## Changes:
* refactor the datafiles action controller

## Tests included
The feature has been manually tested
- [ ] Included for each change/fix?
- [ ] Passing? <!-- Merge will not be approved unless tests pass -->

## Documentation
- [ ] swagger documentation updated (required for API changes) - Not
needed
- [ ] official documentation updated - In progress
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants