Repository navigation
Use the request's API version in URL mode when the connector has one #567
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
base: v4
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,11 +61,21 @@ public function bootHasApiVersion(PendingRequest $pendingRequest): void | |
| VersionMode::Header => $pendingRequest->headers()->add($this->versionKey, $this->apiVersion), | ||
| VersionMode::QueryParam => $pendingRequest->query()->add($this->versionKey, $this->apiVersion), | ||
| VersionMode::Url => $pendingRequest->setUrl( | ||
| str_replace('{version}', $this->ensureVersionIsSafeForUrl($this->apiVersion), $pendingRequest->getUrl()) | ||
| str_replace('{version}', $this->ensureVersionIsSafeForUrl($this->getRequestApiVersion($pendingRequest) ?? $this->apiVersion), $pendingRequest->getUrl()) | ||
| ), | ||
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Get the request's API version, which takes precedence in the URL because the connector boots first and replaces the placeholder before the request can. | ||
| */ | ||
| protected function getRequestApiVersion(PendingRequest $pendingRequest): ?string | ||
| { | ||
| $request = $pendingRequest->getRequest(); | ||
|
|
||
| return method_exists($request, 'getApiVersion') ? $request->getApiVersion() : null; | ||
| } | ||
|
Comment on lines
+72
to
+77
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please could you rename this to Couple of other suggestions too: Only look at the request when the connector is booting ( Only use the request’s version if the request is also in Detect
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like: /**
* Resolve which version to substitute into `{version}` for URL mode.
*/
protected function resolveUrlApiVersion(PendingRequest $pendingRequest): string
{
if ($pendingRequest->getConnector() === $this) {
$request = $pendingRequest->getRequest();
if (in_array(self::class, Helpers::classUsesRecursive($request), true)
&& $request->getVersionMode() === VersionMode::Url) {
$requestVersion = $request->getApiVersion();
if (is_string($requestVersion) && $requestVersion !== '') {
return $requestVersion;
}
}
}
return $this->apiVersion;
}Then a getter: public function getVersionMode(): VersionMode
{
return $this->versionMode;
} |
||
|
|
||
| /** | ||
| * Ensure the version can only ever form a single subdomain label or path segment. | ||
| * | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -138,6 +138,19 @@ | |
| return true; | ||
| }); | ||
| }); | ||
|
|
||
| test('the request can override the connector api version in the url', function () { | ||
| $mockClient = sendVersionedRequest( | ||
| makeVersionedConnector('https://api.provider.com/{version}', VersionMode::Url, 'v1'), | ||
| makeVersionedRequest('beta', VersionMode::Url), | ||
| ); | ||
|
|
||
| $mockClient->assertSent(function (Request $request, $response) { | ||
| expect($response->getPendingRequest()->getUrl())->toBe('https://api.provider.com/beta'); | ||
|
|
||
| return true; | ||
| }); | ||
| }); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for this scenario? Connector still uses |
||
| }); | ||
|
|
||
| describe('Without A Version', function () { | ||
|
|
||
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.
Small readability tweak: call a
resolveUrlApiVersion($pendingRequest)helper here (see my next comment for possible implementation). Same behaviour you’re going for, we still substitute the request’s version when the connector boots first, just not inlined asgetRequestApiVersion() ?? $this->apiVersion.