-
Notifications
You must be signed in to change notification settings - Fork 53
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
Only add path prefix if the path does not contain it already #103
Conversation
hm, i see the use case you have. the change could however lead to other problems, if for whatever reason i have an url that accidentally contains the prefix. i can't think of a really good example, so this contreived example: my prefix should be is there a way for the plugin to detect if it has already seen "this" request? unfortunately, psr requests are re-created with each change, so keeping an object reference won't help. psr requests don't have attributes that we could use to mark a request as "seen" that are not sent to the server, right? a header would be a problem as it pollutes the request being sent. |
Yes, I do agree this could a possible error flow after this patch. My another idea was to create a hash from "$request->getUri()->getPath()" (so the original path) and use that to identify whether the plugin has already "seen" and modified that path before or not. But this can possibly lead to another unexpected errors, I would need to confirm that. |
Actually what the retry plugin does can work in this case too. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this looks good to me, and should avoid the problem without any edge case regressions.
i have one performance optimization suggestion (which we probably should also do on other plugins that use this pattern)
src/Plugin/AddPathPlugin.php
Outdated
); | ||
$identifier = spl_object_hash((object) $first); | ||
|
||
if (!in_array($identifier, $this->alteredRequests)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could we make alteredRequests a hashmap of $identifier => true
instead? then we could check with array_has_key
instead of in_array
. for long running processes that send lots of requests, the in_array
will become expensive once the list of altered requests grows.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That came in my mind but IMO the chance of this is really low. Anyway, I updated to code to store identifier => identifier
in the array therefore we do not need to check what is the value of an identifier. Storing boolean values in the array would be also misleading because someone may think that $identifier => false
means the request has not been altered yet.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks a lot for this, looks good to me now!
Hi. I have a use case where when developing locally a path is not needed (I use mock servers) and depending on the installation of my project I may need to add a prefix path... So sometimes it is empty and sometimes it is not... At this day it does not allow it to be empty. I thought maybe this PR could solve it ? |
@Neirda24 please open a new issue to discuss this topic, as its not related to the pull request. (the good thing though is that it makes me notice we never merged this, will do that now...) |
i have added a changelog entry and squashed the commits. merged in e35c5f3 |
… been altered before Follow up on php-http#103. Everything worked fine until I refactored my tests and started to use a singleton API client for offline testing. If I call the same API endpoints in the same test with the client then the first API request gets prefixed properly but the second one does not, because the generated `$identifier` is already exist in the ` $this->alteredRequests`. I tried to use something else for identifier, I tried different ways to make the identifier more unique but I could not figure out a better solution than this for s start.
Why would you do I understand |
oh, indeed, did not realize that. but from the context of the description (oauth) i assume the request was waited for to get the response and pull the oauth token out of that response. on oauth specifically, we recently discussed a similar case and said the best implementation would be to not make that a plugin but a authentication that is given to the existing AuthenticationPlugin, and inject a separate client instance to the oauth authentication. that client does at least not need the authentication, and possibly also not some other plugins. |
What's in this PR?
This is a revisit of the #74.
Why?
(Just second my comments from the original PR)
If you try to restart a failed API call by calling
return $first($request)->wait()
in a plugin (ex.: when an Oauth access token expires and you want your API client to automatically resend the same request with a new access token provided by a custom Oauth auth plugin) then the current behaviour of AddPathPlugin corrupts the URL by adding the same API version prefix to the URL again.What is the exact use case behind adding a path prefix to a path that already has the prefix anyway? I can not imagine any.
Example Usage
Checklist
To Do