-
Notifications
You must be signed in to change notification settings - Fork 180
Replace ServerRequestFactory with RequestFactory #321
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
Merged
kbsali
merged 5 commits into
kbsali:v2.x
from
Art4:replace-serverrequestfactory-with-requestfactory
Sep 14, 2023
+105
−48
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c52d54f
remove need for ServerRequestFactoryInterface
Art4 f146bd0
Change tests to use RequestFactory
Art4 32db23a
Allow RequestFactoryInterface in Psr18Client
Art4 48f4cad
Update CHANGELOG.md
Art4 7a9bdf4
Merge branch 'v2.x' into replace-serverrequestfactory-with-requestfac…
kbsali File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,13 @@ | |
|
|
||
| namespace Redmine\Client; | ||
|
|
||
| use Exception; | ||
| use Psr\Http\Client\ClientExceptionInterface; | ||
| use Psr\Http\Client\ClientInterface; | ||
| use Psr\Http\Message\ResponseInterface; | ||
| use Psr\Http\Message\RequestFactoryInterface; | ||
| use Psr\Http\Message\RequestInterface; | ||
| use Psr\Http\Message\ServerRequestFactoryInterface; | ||
| use Psr\Http\Message\ServerRequestInterface; | ||
| use Psr\Http\Message\StreamFactoryInterface; | ||
| use Redmine\Exception\ClientException; | ||
|
|
||
|
|
@@ -24,22 +26,45 @@ final class Psr18Client implements Client | |
| private ?string $password; | ||
| private ?string $impersonateUser = null; | ||
| private ClientInterface $httpClient; | ||
| private ServerRequestFactoryInterface $requestFactory; | ||
| private RequestFactoryInterface $requestFactory; | ||
| private StreamFactoryInterface $streamFactory; | ||
| private ?ResponseInterface $lastResponse = null; | ||
|
|
||
| /** | ||
| * $apikeyOrUsername should be your ApiKey, but it could also be your username. | ||
| * $password needs to be set if a username is given (not recommended). | ||
| * @param RequestFactoryInterface|ServerRequestFactoryInterface $requestFactory | ||
| * @param string $apikeyOrUsername should be your ApiKey, but it could also be your username. | ||
| * @param ?string $password needs to be set if a username is given (not recommended). | ||
| */ | ||
| public function __construct( | ||
| ClientInterface $httpClient, | ||
| ServerRequestFactoryInterface $requestFactory, | ||
| $requestFactory, | ||
| StreamFactoryInterface $streamFactory, | ||
| string $url, | ||
| string $apikeyOrUsername, | ||
| string $password = null | ||
| ) { | ||
| if ($requestFactory instanceof ServerRequestFactoryInterface) { | ||
|
Collaborator
Author
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. We should also check if |
||
| @trigger_error( | ||
| sprintf( | ||
| '%s(): Providing Argument #2 ($requestFactory) as %s is deprecated since v2.3.0, please provide as %s instead.', | ||
| __METHOD__, | ||
| ServerRequestFactoryInterface::class, | ||
| RequestFactoryInterface::class | ||
| ), | ||
| E_USER_DEPRECATED | ||
| ); | ||
|
|
||
| $requestFactory = $this->handleServerRequestFactory($requestFactory); | ||
| } | ||
|
|
||
| if (! $requestFactory instanceof RequestFactoryInterface) { | ||
| throw new Exception(sprintf( | ||
| '%s(): Argument #2 ($requestFactory) must be of type %s', | ||
| __METHOD__, | ||
| RequestFactoryInterface::class | ||
| )); | ||
| } | ||
|
|
||
| $this->httpClient = $httpClient; | ||
| $this->requestFactory = $requestFactory; | ||
| $this->streamFactory = $streamFactory; | ||
|
|
@@ -155,9 +180,9 @@ private function runRequest(string $method, string $path, string $body = ''): bo | |
| return $this->lastResponse->getStatusCode() < 400; | ||
| } | ||
|
|
||
| private function createRequest(string $method, string $path, string $body = ''): ServerRequestInterface | ||
| private function createRequest(string $method, string $path, string $body = ''): RequestInterface | ||
| { | ||
| $request = $this->requestFactory->createServerRequest( | ||
| $request = $this->requestFactory->createRequest( | ||
| $method, | ||
| $this->url.$path | ||
| ); | ||
|
|
@@ -215,4 +240,25 @@ private function createRequest(string $method, string $path, string $body = ''): | |
|
|
||
| return $request; | ||
| } | ||
|
|
||
| /** | ||
| * We accept ServerRequestFactoryInterface for BC | ||
| */ | ||
| private function handleServerRequestFactory(ServerRequestFactoryInterface $factory): RequestFactoryInterface | ||
| { | ||
| return new class($factory) implements RequestFactoryInterface | ||
| { | ||
| private ServerRequestFactoryInterface $factory; | ||
|
|
||
| public function __construct(ServerRequestFactoryInterface $factory) | ||
| { | ||
| $this->factory = $factory; | ||
| } | ||
|
|
||
| public function createRequest(string $method, $uri): RequestInterface | ||
| { | ||
| return $this->factory->createServerRequest($method, $uri); | ||
| } | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.