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 API cleanup #4272

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

HTTP API cleanup #4272

wants to merge 4 commits into from

Conversation

willmmiles
Copy link
Collaborator

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
Collaborator

@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.

@@ -482,7 +482,17 @@ void initServer()
return;
}

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

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
Collaborator 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
Collaborator 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
Collaborator

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
Collaborator 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
Collaborator

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
Collaborator 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
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.

3 participants