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

Fix handling of CSV responses #3

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

michaelbaudino
Copy link

This PR fixes 2 issues (thus 2 separate commits) I had with endpoints that return CSV responses (such as reports).

CSV are still not automatically parsed (because HTTParty does not detect they are CSV since the API doesn't seem to provide the appropriate Content-Type headers 🙄), but at least they do not raise an error anymore 😅

Because some of them are CSV (e.g. reports), which raises a `JSON::ParserError` when trying to parse their body as JSON.

This commit proposes an alternative way of enforcing usage of `JsonStruct`: overriding the method that parses JSON (`HTTParty::Parser#json`) rather than overriding the method that parses all formats (`HTTParty::Parser#parse`) and assume we always receive JSON.
This commit does 2 things when an UTF8 BOM is present in the response (even when HTTParty detects another encoding):
1. remove the BOM
2. force encoding of the body
@michaelbaudino
Copy link
Author

Just as a side note, I'm not sure how CSV data could be parsed automatically (with CSV.parse like we do with JSON.parse) since the API returns CSV data with the following headers:

Content-Type: application/octet-stream
Content-Disposition: attachment; filename="TeamsUserActivityUserCounts3/24/2023 8:59:02 AM.csv"

But if anyone has a good idea (better than checking the extension of the filename 😉), I'd be happy to implement it.

@michaelbaudino
Copy link
Author

@kklimuk Is there anything I can add to help push this PR forward?

This allows using `version: "beta"` 🚧
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant