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

http helpers for react, API to new js #3662

Merged
merged 12 commits into from
May 16, 2018
Merged

http helpers for react, API to new js #3662

merged 12 commits into from
May 16, 2018

Conversation

himdel
Copy link
Contributor

@himdel himdel commented Mar 20, 2018

Fixes #3658

This moves our API code from miq_api.js to app/javascript, converts it a more modern style, and splits the implementation into API-specific parts (http_api/api.js) and the common code (http_api/fetch.js - a miqFetch wrapper around the fetch API), making it possible to use that common code to implement a simple http library (http_api/http.js) for accessing rails controllers with the same interface.

This PR keeps all the existing names for the API code for backward compatibility (will remove in a separate PR, once all the code is converted), but changes the preferred default:

  • window.API is the API access code for any plain js / react code
    (window.vanillaJsAPI is a deprecated name for the same)

  • $API is the angular service to acccess the API
    (API is a deprecated name for the same)

  • window.http is the http access code for any plain js / react code
    (until now, the only ways were angular $http or miqJqueryRequest)

  • $http is the standard angular service, configured to be usable for miq pruposes
    (no changes there)

All of these methods should behave the same way:

  • when an authentication failure occurs, redirect to login screen (unless the skipLoginRedirect option is used)
  • when a non-200 response (other than 401) occurs, show the error modal (unless the skipErrors option is used)

(So as not to overload codeclimate, this also fixes the eslint config to respect the root options - the airbnb config is still used, it just respects everything set in the root .eslintrc as well.)

(Please ignore codeclimate for the API code, this will be fixed in #3929.)

@miq-bot miq-bot added the wip label Mar 20, 2018
@ohadlevy
Copy link
Member

ohadlevy commented Mar 21, 2018

please also see https://github.com/priley86/miq_v2v_ui_plugin/blob/master/app/javascript/common/API.js - ideally it should all be combined?

my main concern is around mocking the api calls, /cc @priley86

@miq-bot
Copy link
Member

miq-bot commented Mar 21, 2018

This pull request is not mergeable. Please rebase and repush.

@himdel
Copy link
Contributor Author

himdel commented Apr 9, 2018

Yes, the API.js file should not even exist in v2v, ideally v2v should be doing const API = window.API and use that (after this PR that is).

@AllenBW
Copy link
Member

AllenBW commented May 7, 2018

V2V has skin in this game, bumping for a lil 🏩 😻

@himdel
Copy link
Contributor Author

himdel commented May 10, 2018

Looks like this works now, so just bear with me please @AllenBW while I clean this up and create some nice commits :).

@AllenBW
Copy link
Member

AllenBW commented May 10, 2018

OH EM GEEE MY HERO!!! <3 <3 <3 🙇‍♀️ will always 🐻 with yah @himdel 😋 this is good news.. i'll cool my jets on da wip-pr till this work shows up

@himdel himdel added enhancement and removed wip labels May 11, 2018
@himdel himdel changed the title [WIP] http helpers for react, API to new js http helpers for react, API to new js May 11, 2018
@himdel
Copy link
Contributor Author

himdel commented May 11, 2018

the following (public) methods are provided for API (this did not change, just mentioning)

  • API.delete(url, [options])
  • API.get(url, [options])
  • API.options(url, [options])
  • API.post(url, data, [options])
  • API.put(url, data, [options])

the following methods are provded for http - these are only meant for JSON access points:

  • http.get(url, [options])
  • http.post(url, data, [options])

EDIT: added options to http methods

@AllenBW
Copy link
Member

AllenBW commented May 11, 2018

☝️ the difference being support for options, vs not?

@himdel
Copy link
Contributor Author

himdel commented May 11, 2018

@AllenBW not really, it's just we have no options to support there yet - it does not make sense to do skipErrors when talking to rails controllers since the errors will be a 200 response with a rails error html, and the same for skipLoginRedirect, as no meaningful response will happen without being logged in

EDIT: (The imporant difference is that http sends the CSRF token, while API sends the api login token)

EDIT2: and the http option support may make some sense for transformResponse, adding

…ckend

the error modal code needs to be able to tell whether the error comes from a $http request or from an API request, and uses the `source` field for that (`API` vs `$http`)

to unify http & API access accross the codebase, including react code, we also need a non-angular http access code

this new code will be using the fetch API, the same as our API code - which means we'd have 3 source values, two of which share parts of the implementation

so .. splitting this into `source = 'fetch' | '$http'` and `backendName = <human string>`
just a straight move, with the bare minimum changes

leaving the angular bits in the old code for now, because of potential ordering issues
should not actually change anything, just make sure var is not used, globals are properly "imported", etc.
…on around them

the idea is that miqFetch can become the common implementation, being able to handle logouts, errors, and tokens for both rails http access and rest api
…r the hood

the interface is very simple right now, http.get(url) and http.post(url, data)
both the functions return a Promise, both handle logouts and errors
the idea behind vanillaJsAPI was that since we did angular first, using the angular service was preferred, thus a short name was needed

they still have to have different names so that we can catch the non-angular version being used in angular code

but since the codebase is moving towards vanilla JS / React code first, it makes sense for that version to get the short name - API

the angular version will be called $API from now, but any code using API will keep working until we update it all to the new state

(a second PR will change API to $API in existing angular code and remove the old names completely once there are no users)
@himdel
Copy link
Contributor Author

himdel commented May 11, 2018

Cc @martinpovolny

modelled after $http transformResponse, this can be a custom function to change the response in any way needed

this will only be applied to success responses, not on error
@himdel
Copy link
Contributor Author

himdel commented May 11, 2018

Added transformResponse option support, so that v2v API requests can do something like...

API.get('/foo', { transformResponse: (o) => ({ data: o })}) to get the data in the same format as before.

not strictly useful right now, but since the same set of options is supported between the API and http code, it makes sense to make it possible to use some some day
because jest tests don't load any packs first, we need to set `window.vanillaJsAPI` before using it now
@miq-bot
Copy link
Member

miq-bot commented May 14, 2018

Checked commits https://github.com/himdel/manageiq-ui-classic/compare/91793f5613018cacffb2074dfeda93942b3ea170~...8edc838bd9ffea54d1241fb588c872c5620b6356 with ruby 2.3.3, rubocop 0.52.1, haml-lint 0.20.0, and yamllint 1.10.0
0 files checked, 0 offenses detected
Everything looks fine. 🏆

@AllenBW
Copy link
Member

AllenBW commented May 15, 2018

soooooooo we now have a blocker that could use this pr merged 😢 https://bugzilla.redhat.com/show_bug.cgi?id=1573547

@martinpovolny martinpovolny merged commit fda046b into ManageIQ:master May 16, 2018
@martinpovolny martinpovolny added this to the Sprint 86 Ending May 21, 2018 milestone May 16, 2018
@martinpovolny martinpovolny self-assigned this May 16, 2018
@AllenBW
Copy link
Member

AllenBW commented May 16, 2018

BEST MORNING NEWS EVA <3 📰 👍

@martinpovolny
Copy link
Member

;-)

@himdel
Copy link
Contributor Author

himdel commented May 30, 2018

@miq-bot add_label gaprindashvili/yes, transformation
@miq-bot remove_label gaprindashvili/no

@simaishi This can not be backported directly, depends on the whole es6 support ecosystem and several npm (instead of bower) dependencies => #4005

@simaishi
Copy link
Contributor

simaishi commented Jun 1, 2018

Backported to Gaprindashvili via #4005

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.

Unified error handling (React)
6 participants