-
Notifications
You must be signed in to change notification settings - Fork 358
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
Allow non-JSON responses in miqFetch (add options.skipJsonParsing
)
#6645
Allow non-JSON responses in miqFetch (add options.skipJsonParsing
)
#6645
Conversation
@miq-bot add_label ivanchuk/yes |
@miq-bot add_label enhancement |
Looks good, except.. is If the latter, we should make sure it doesn't end up in |
And as the code is, the option will get ignored for non-200 responses. If we want to support those as well, maybe something like.. if (response.status === 204) {
// No content
return Promise.resolve(null);
}
const maybeJson = options.skipJsonParsing ? (r) => r.json() : (r) => r; // <--
if (response.status >= 300) {
// Not 1** or 2**
// clone() because otherwise if json() fails, you can't call text()
return maybeJson(response.clone()) // <--
.catch(tryHtmlError(response))
.then(rejectWithData(response));
}
return maybeJson(response); // <-- ? (and possibly a fix for error modal not being able to meaningfully display the response, but not sure that's really possible for binary responses anyway :)) |
Oh, one more idea (just an idea really :)).. do we need an option? Because, theoretically the data is already available in the responses content-type header, right? No harm in being explicit though. |
The latter.
I already did 😄
That's an interesting point, but it seems like (at least with the 404 I tried: /api/red_hat_migration_analytics_payload?task_id=12345) non-200 responses will return JSON to describe the error, even if the endpoint returns non-JSON content on success.
That's true... are you suggesting I just resolve with the raw response like this if |
To be clear, I think it would be ideal if the |
Checked commit mturley@d8428c0 with ruby 2.5.5, rubocop 0.69.0, haml-lint 0.20.0, and yamllint 1.10.0 |
Ah, perfect :) |
Thanks @himdel ! |
Allow non-JSON responses in miqFetch (add `options.skipJsonParsing`) (cherry picked from commit a10fb37) https://bugzilla.redhat.com/show_bug.cgi?id=1805818
Ivanchuk backport details:
|
Needed for https://bugzilla.redhat.com/show_bug.cgi?id=1788730
In RedHatCloudForms/cfme-migration_analytics#37, we are adding a new API endpoint which delivers a binary response (
content-disposition: attachment
) intended for the UI to make available as a file download. Rationale for the decision to handle the file download via the API instead of a UI worker can be found in the relevant discussions at RedHatCloudForms/cfme-migration_analytics#19 and RedHatCloudForms/cfme-migration_analytics#33.Currently, the
API
methods provided here (which we must use to handle authentication) assume API responses will always be JSON, and callresponse.json()
as part of processing thefetch()
response. This causes an error when trying to make a call to the new endpoint, since it is returning binary gzip data.This PR adds a new option to
miqFetch
calledskipJsonParsing
which simply skips that.json()
call, instead resolving the promise with the rawresponse
object directly. This gives the calling function in the UI access toresponse.blob()
andresponse.headers()
, which are necessary for reading the binary data and attachment filename.To test these changes, you can run the following code in a browser's JavaScript console:
Links
BZ for backport to ivanchuk: https://bugzilla.redhat.com/show_bug.cgi?id=1788730
JIRA task for feature: https://issues.redhat.com/browse/MIGENG-241
Issue for feature: RedHatCloudForms/cfme-migration_analytics#19
Dependent PR: RedHatCloudForms/cfme-migration_analytics#37
Initial closed PR with implementation context: RedHatCloudForms/cfme-migration_analytics#33