Project

General

Profile

Actions

Refactor #30450

closed

Use API hook and Foreman API middleware for all React API requests

Added by Jeremy Lenz over 3 years ago. Updated over 2 years ago.

Status:
Rejected
Priority:
Normal
Assignee:
-
Category:
API
Target version:
Branch:
Difficulty:
Triaged:
Yes
Fixed in Releases:
Found in Releases:

Description

Now that Foreman React API middleware supports all HTTP methods and not just GET (https://github.com/theforeman/foreman/pull/7261), we should start using it for new requests. In addition, we should consider using it for existing requests.

Foreman has also added a useAPI React Hook, which will simplify things even more.

In Katello, I found
  • 7 PUT requests
  • 0 PATCH
  • 15 DELETE (but not sure how many are relevant)
  • 4 POST
  • 41 GET

Currently, we use this redux-thunk pattern to handle most API requests:
1. In a try block, dispatch & await REQUEST action
2. dispatch SUCCESS action
3. dispatch FAILURE action in a catch block

With the API middleware, we would only have to:
1. dispatch API_OPERATIONS.[method] action.

Success and failure actions are automatically dispatched, and the API response is stored in Redux in a standard way. If we need any side effects to happen, handleSuccess and handleFailure callbacks are supplied.

Another benefit is that Redux action creators will no longer have to be thunks (i.e., can return simple objects instead of returning a function.) This makes Redux easier to understand and makes our code more readable.

A third benefit is that it would cut our code base by several dozen lines, since we won't have to repeat the REQUEST -> SUCCESS / FAILURE thunks everywhere.

With the useAPI hook, we can simplify things even further. For components that use hooks, we can just use that instead of creating actions/reducers/etc.

Actions #1

Updated by Chris Roberts over 3 years ago

  • Target version set to Katello 3.18.0
  • Triaged changed from No to Yes
Actions #2

Updated by Jonathon Turel over 3 years ago

  • Target version changed from Katello 3.18.0 to Katello 4.1.0
Actions #3

Updated by Jeremy Lenz over 3 years ago

  • Description updated (diff)
Actions #4

Updated by Jeremy Lenz over 3 years ago

  • Subject changed from Use Foreman API middleware for all React API requests to Use API hook and Foreman API middleware for all React API requests
Actions #5

Updated by Jeremy Lenz almost 3 years ago

  • Target version changed from Katello 4.1.0 to Katello 4.2.0
Actions #6

Updated by Justin Sherrill over 2 years ago

  • Target version deleted (Katello 4.2.0)
  • Triaged changed from Yes to No
Actions #7

Updated by Samir Jha over 2 years ago

  • Target version set to Katello Backlog
  • Triaged changed from No to Yes
Actions #8

Updated by Jeremy Lenz over 2 years ago

  • Status changed from New to Rejected
  • Target version changed from Katello Backlog to Katello Recycle Bin

We will use this pattern for the new pages and for any other refactoring we do. Not enough resources to go back and refactor everything.

Actions

Also available in: Atom PDF