Skip to content

HTTP API cleanup#4272

Draft
willmmiles wants to merge 4 commits intomainfrom
http-api-refactor
Draft

HTTP API cleanup#4272
willmmiles wants to merge 4 commits intomainfrom
http-api-refactor

Conversation

@willmmiles
Copy link
Member

Along with #4271, I took the opportunity to clean up the surrounding code a small amount. This PR:

  • Renames 'handleSet' to 'handleHttpApi', clarifying what this function does;
  • Moves the check for leading 'win' to the one context where it matters (the web server), removing the need for all other callers to re-add this fixed string;
  • Moves the response generation out to the web server, eliminating an otherwise unnecessary parameter;
  • Removes the 'IN' tag, as that internal API was superseded by passing nullptr for the request. The conditional was broken anyhow, and HTTP requests are required to generate a response.

Clarify what this function does: it implements the legacy HTTP
API parser.
Rather add this token in to all call sites, check for it only where it
matters: when we've got an HTTP request.  If it's passed in other
contexts, it will be safely ignored.
Internal requests are indicated by passing nullptr for the request
argument.  If we *are* in fact called from an HTTP request, we must
generate some kind of reply anyways, so this parameter is obsolete.
No need to even consider this for non-web requests.  Move the request
special case to that context as well.
Copy link
Contributor

@blazoncek blazoncek left a comment

Choose a reason for hiding this comment

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

Seems like a good improvement. Have yet to test it, though.


if(handleSet(request, request->url())) return;
if (request->url().indexOf("win") == 0) {
if (handleHttpApi(request->url().substring(3))) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it 3 or is it 4?
I would think 4 as you do not compare & in handleHttpApi() or there is no need to add equality to comparisons there.

Copy link
Member Author

Choose a reason for hiding this comment

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

The leading '&' is intentionally left on as some of the test strings in handleHttpApi() have a leading "&", eg. pos = req.indexOf(F("&S="));. I'm not really sure why some do and some don't -- I should probably just clean that up too while I'm in there.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually, given that, I'm going to convert this to draft for now and do some more testing. I think there's some corner cases where this code will differ from the original.

if(handleSet(request, request->url())) return;
if (request->url().indexOf("win") == 0) {
if (handleHttpApi(request->url().substring(3))) {
if (request->url().indexOf(F("&FX=")) > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it be better to move this into handleHttpApi()?
IMO it's place is there.

Copy link
Member Author

Choose a reason for hiding this comment

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

In the original code, we call unloadPlaylist() only the FX was changed from an HTTP request, but not from any other context. It made more sense to me to place that code only in the HTTP request context, rather than add a special parameter to handleHttpApi() just to switch this call on or off. It does mean naming the tag twice, but I think that's the lesser evil -- https://sandimetz.com/blog/2016/1/20/the-wrong-abstraction

An alternative might be to augment the return code to indicate if the FX value was changed, rather than parsing the tag multiple times.

Copy link
Contributor

Choose a reason for hiding this comment

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

IMO any change to FX should unload playlist - unless loaded from playlist itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

I agree -- although this PR was aimed to be a refactor with no behavioural changes, and the original logic only unloaded the playlist in the context of an HTTP request.

I'll do a more thorough review of the other call contexts and see if I can draw a better map. No reason not to further improve things as we go.

@willmmiles willmmiles marked this pull request as draft November 15, 2024 15:14
@netmindz netmindz changed the base branch from 0_15 to main December 16, 2024 13:26
@github-actions
Copy link

Hey! This pull request has been open for quite some time without any new comments now. It will be closed automatically in a week if no further activity occurs.
Thank you for contributing to WLED! ❤️

@github-actions github-actions bot added the stale This issue will be closed soon because of prolonged inactivity label Apr 16, 2025
@coderabbitai
Copy link
Contributor

coderabbitai bot commented Apr 16, 2025

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@github-actions github-actions bot closed this Apr 24, 2025
@blazoncek
Copy link
Contributor

@willmmiles shall we reopen this?

@willmmiles
Copy link
Member Author

Sure. It's not a burning priority but I would like to get back to it at some point.

@willmmiles willmmiles reopened this Apr 27, 2025
@willmmiles willmmiles added the keep This issue will never become stale/closed automatically label Apr 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

keep This issue will never become stale/closed automatically stale This issue will be closed soon because of prolonged inactivity

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants