Skip to content

Add AddPathPlugin always_prepend option #74

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

Closed
wants to merge 1 commit into from
Closed

Add AddPathPlugin always_prepend option #74

wants to merge 1 commit into from

Conversation

fbourigault
Copy link
Contributor

@fbourigault fbourigault commented Jun 6, 2017

Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Related tickets N/A
Documentation TODO
License MIT

This allow to make the AddPathPlugin smart. By smart I mean to not prepend the configured URI path to the request URI if already present. This behavior is disabled by default to keep code BC.

The BaseUriPlugin now has a third optional constructor argument to configure the underlying AddPathPlugin.

This is required for https://github.com/m4tthumphrey/php-gitlab-api which has a base URI set to https://example.gitlab.com/api/v3 but is also used to follow pagination Link headers uri which already have the base URI configured.

To do

  • Documentation.
  • Changelog.

@fbourigault
Copy link
Contributor Author

I'm open to suggestions for an alternative to the smart name!

Copy link
Contributor

@dbu dbu left a comment

Choose a reason for hiding this comment

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

looks good to me, apart from my style nitpicks. i am not super happy with "smart" but can't think of a better name. i can live with "smart" :-)

* @param UriInterface $uri
* @param array $config {
*
Copy link
Contributor

Choose a reason for hiding this comment

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

please remove the extra blank line

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like StyleCI wants this extra blank line.

* @param UriInterface $uri
* @param array $config {
*
* @var bool $smart True will prepend path only if request path does not start with path to add.
Copy link
Contributor

Choose a reason for hiding this comment

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

i am somehow confused by the wording. maybe "Set to true to only prepend the path only if the request path does not start with that path already."?

$prepend = $this->uri->getPath();
$path = $request->getUri()->getPath();

if (!($this->smart && substr($path, 0, strlen($prepend)) === $prepend)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

can we rewrite this to if (!$this->smart || !substr(...))? i feel that is more readable.

CHANGELOG.md Outdated
@@ -2,6 +2,11 @@

## Unreleased

### Added

- Add the `$smart` option to `AddPathPlugin` to prepend path only when required.
Copy link
Contributor

Choose a reason for hiding this comment

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

lets write this 'smart' instead of $smart

@fbourigault
Copy link
Contributor Author

fbourigault commented Jun 7, 2017

About the smart name. We can also reverse the boolean meaning and so the behavior (and of course change the default value). Maybe it could help to find a better name.

EDIT: what about always_prepend with true as default value?

@Nyholm
Copy link
Member

Nyholm commented Jun 7, 2017

what about always_prepend with true as default value?

I like that better.

$prepend = $this->uri->getPath();
$path = $request->getUri()->getPath();

if (!$this->smart || substr($path, 0, strlen($prepend)) !== $prepend) {
Copy link
Member

Choose a reason for hiding this comment

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

This only consider the case where $prepend is the beginning of $path. Is that the only case we want?

In your case on Gitlab, you do not want to prepend if a absolute URL is used. IMHO, that logic should only be in BaseUriPlugin.

Copy link
Contributor Author

@fbourigault fbourigault Jun 7, 2017

Choose a reason for hiding this comment

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

I did this way to be more consistant with AddHostPlugin which owns the replace logic.

To get back to my use case, it's about using Link header href attributes. As of https://tools.ietf.org/html/rfc5988 §5.1 this IRI can be relative so I'm not sure that in a future release gitlab will send relative IRIs.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But maybe I'm wrong with the client design and the API path (/api/v3) should be hardcoded in the abstract api class and so, I just need to use the AddHostPlugin as the user will only configure it's gitlab instance domain.

Copy link
Member

Choose a reason for hiding this comment

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

But maybe I'm wrong with the client design and the API path (/api/v3) should be hardcoded in the abstract api class

Yeah, maybe.. :/

Or in every endpoint..

Though. This PR is still useful.

@sagikazarmark
Copy link
Member

How about preserve_uri defaulting to false?

@sagikazarmark
Copy link
Member

Hm, rereading the initial comment not sure what we prepend here. Always prepend sounds a little weird to me. prepend/preserve_path/uri?

@Nyholm
Copy link
Member

Nyholm commented Jun 8, 2017

The AddPath plugin prepends the current path with a predefined value.

The idea is that if the predefined value is "/foo/bar" then we should not prepend that to an url like "/foo/bar/whatever" since it already contains "/foo/bar".

@fbourigault fbourigault changed the title Add AddPathPlugin smart option Add AddPathPlugin always_prepend option Jun 8, 2017
@fbourigault
Copy link
Contributor Author

If prepend is an issue, I can name the option always_add. Didn't know that prepend was a neologism!

@sagikazarmark
Copy link
Member

No, my issue is with always. Despite the word, it does not express the conditions when it matters. Of course we can document it properly, but very likely the next time I meet the config value I will just not remember what it was and look it up again.

Don't know if there is a better name for it though.

@Nyholm
Copy link
Member

Nyholm commented Jun 8, 2017 via email

@sagikazarmark
Copy link
Member

Exactly 😂

@fbourigault
Copy link
Contributor Author

Fabien listen to propositions.

@Nyholm
Copy link
Member

Nyholm commented Jul 6, 2017

Name it prepend_even_if_uri_start_with_prefix

@fbourigault
Copy link
Contributor Author

Closing because no longer required for https://github.com/m4tthumphrey/php-gitlab-api.

@fbourigault fbourigault closed this Jul 6, 2017
@fbourigault fbourigault deleted the smart-add-path-plugin branch July 6, 2017 12:00
@mxr576
Copy link
Contributor

mxr576 commented May 2, 2018

@sagikazarmark @Nyholm Can we re-open this issue? If you try to restart a failed API call by calling $first($request) 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.

@mxr576
Copy link
Contributor

mxr576 commented May 2, 2018

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.

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.

5 participants