Skip to content

Add Requests::has_capability() which can be used to determine if SSL is available. - #492

Merged
schlessera merged 1 commit into
developfrom
feature/251-add-requests-has-capability
Nov 10, 2021
Merged

schlessera merged 1 commit into
developfrom
feature/251-add-requests-has-capability

Conversation

@jrfnl

@jrfnl jrfnl commented Jun 18, 2021

Copy link
Copy Markdown
Member

Note: this is a recreation of PR #251, which was closed due to the branch rename and the remote no longer existing.
Please see PR #251 for the original discussion on the PR.

Add Requests::has_capability() which can be used to determine if SSL is available.

An example of it's usage: Requests::has_capability( array( Capability::SSL => true ) );

See #250 and #251

/cc @dd32


After review of the original PR, the following additional changes have been made and are included in this commit:

  • Add a WpOrg\Requests\Capability interface which holds constants for the queryable capabilities.
  • Split the Requests::get_transport() method in two:
    • Requests::get_transport_class() which will retrieve the fully qualified class name for a working transport and will cache the result.
    • Requests::get_transport() to get an instantiated instance of a working transport, same as before.
  • Adjusting the Transport interface and classes implementing it, to take a $capabilities parameter for the test() method and use the constants from the Capability interface internally.
  • Add tests for the new Requests::has_capabilities() method.
  • Update the Transport related tests and mocks.
  • Add a usage example to the prose documentation.

@schlessera

Copy link
Copy Markdown
Member

Having a way to test for a capability makes sense, but the $capabilities array is not the best way to define what to actually test.

We'll rework this in the following way:

  • Change the signature to has_capability( string $capability ).
  • Create an interface Capability which contains constants for all the known capabilities (which seems to be only ssl right now). This will mean the usage looks like this: Requests::has_capability( Capability::SSL ).
  • Document usage for has_capability() on where to find the list of supported capabilities.
  • Add tests.

@schlessera

Copy link
Copy Markdown
Member

While going through the code to make the changes I suggested above, I noticed an issue with the current implementation.

The thing is that the way this is implemented, it always triggers the transport constructor if it has the capabilities, only to then discard that transport (the check returns a boolean only).

While the Requests_Transport_fsockopen doesn't have a constructor at all, the Requests_Transport_cURL (which should be used in most cases) has a constructor that configure cURL and then create a new resource handle, only to then discard that resource handle again in the destructor.

I think this is extremely wasteful for such a simple check. Avoiding this will need a change to the Requests::get_transport() method as well.

@schlessera
schlessera force-pushed the feature/251-add-requests-has-capability branch from 095c819 to 09e07df Compare September 3, 2021 08:48
@schlessera

Copy link
Copy Markdown
Member

@jrfnl I can't set you as reviewer, as you've created the PR. Please review this latest change.

@schlessera
schlessera force-pushed the feature/251-add-requests-has-capability branch from 7d50f66 to 9cf8068 Compare September 3, 2021 09:01

@jrfnl jrfnl left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Gone through it in detail and left some comments and questions. I look forward to the next iteration.

Comment thread tests/Mock/TestTransportMock.php Outdated
Comment thread library/Requests/Capability.php Outdated
Comment thread library/Requests/Transport/cURL.php Outdated
Comment thread library/Requests/Transport/fsockopen.php Outdated
Comment thread tests/RequestsTest.php
Comment thread library/Requests.php Outdated
Comment thread library/Requests.php Outdated
Comment thread library/Requests.php Outdated
Comment thread library/Requests.php Outdated
Comment thread library/Requests.php Outdated
@jrfnl

jrfnl commented Sep 10, 2021

Copy link
Copy Markdown
Member Author

Oh and one more question: should this method and this new class be mentioned anywhere in the docs/are there docs which need updating for the changes which will now be made ?

@jrfnl jrfnl added this to the 2.0.0 milestone Sep 12, 2021
@schlessera
schlessera force-pushed the feature/251-add-requests-has-capability branch 2 times, most recently from b4a6800 to 0fedc8f Compare September 17, 2021 13:22
@schlessera

Copy link
Copy Markdown
Member

@jrfnl This can be reviewed again.

@jrfnl

jrfnl commented Sep 19, 2021

Copy link
Copy Markdown
Member Author

The thing is that the way this is implemented, it always triggers the transport constructor if it has the capabilities, only to then discard that transport (the check returns a boolean only).

While the Requests_Transport_fsockopen doesn't have a constructor at all, the Requests_Transport_cURL (which should be used in most cases) has a constructor that configure cURL and then create a new resource handle, only to then discard that resource handle again in the destructor.

I think this is extremely wasteful for such a simple check. Avoiding this will need a change to the Requests::get_transport() method as well.

There may actually be good reason for this - I think this may be loosely related to #429.

@schlessera
schlessera force-pushed the feature/251-add-requests-has-capability branch from 42c59ad to 5e93ef5 Compare November 5, 2021 10:39
@schlessera

Copy link
Copy Markdown
Member

@jrfnl This should be ready for a new review.

@jrfnl jrfnl left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@schlessera Gone through it with a fine toothcomb. Hope I caught everything. Nearly there! Thanks for all your work on this.

Comment thread docs/usage-advanced.md
Comment thread src/Requests.php Outdated
Comment thread src/Requests.php Outdated
Comment thread src/Requests.php Outdated
Comment thread src/Requests.php Outdated
Comment thread src/Transport/Fsockopen.php Outdated
Comment thread src/Capability.php Outdated
Comment thread src/Capability.php
Comment thread tests/RequestsTest.php Outdated
Comment thread tests/Transport/BaseTestCase.php Outdated
Comment thread src/Capability.php Outdated
@schlessera
schlessera force-pushed the feature/251-add-requests-has-capability branch from 3aeacc1 to 0c036e4 Compare November 10, 2021 14:16
Comment thread docs/usage-advanced.md Outdated
Comment thread src/Capability.php Outdated
Comment thread src/Requests.php

@jrfnl jrfnl left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Nearly done, will fix up those last few things now.

@jrfnl
jrfnl force-pushed the feature/251-add-requests-has-capability branch from 0c036e4 to c643985 Compare November 10, 2021 15:01
@jrfnl

jrfnl commented Nov 10, 2021

Copy link
Copy Markdown
Member Author

Ready for merge IMO

…is available.

_Note: this is a recreation of PR 251, which was closed due to the branch rename and the remote no longer existing._

Add `Requests::has_capability()` method which can be used to determine if SSL is available.

An example of it's usage: `Requests::has_capability( array( 'ssl' => true ) );`

---

After review of the original PR, the following additional changes have been made and are included in this commit:
* Add a `WpOrg\Requests\Capability` interface which holds constants for the queryable capabilities.
* Split the `Requests::get_transport()` method in two:
    * `Requests::get_transport_class()` which will retrieve the fully qualified class name for a working transport and will cache the result.
    * `Requests::get_transport()` to get an instantiated instance of a working transport, same as before.
* Adjusting the `Transport` interface and classes implementing it, to take a `$capabilities` parameter for the `test()` method and use the constants from the `Capability` interface internally.
* Add tests for the new `Requests::has_capabilities()` method.
* Update the `Transport` related tests and mocks.
* Add a usage example to the prose documentation.

Co-authored-by: Dion Hulse <contact@dd32.id.au>
Co-authored-by: Alain Schlesser <alain.schlesser@gmail.com>
@jrfnl
jrfnl force-pushed the feature/251-add-requests-has-capability branch from c643985 to 0038704 Compare November 10, 2021 15:04
@schlessera
schlessera merged commit 5f09e38 into develop Nov 10, 2021
@schlessera
schlessera deleted the feature/251-add-requests-has-capability branch November 10, 2021 15:21
jrfnl added a commit that referenced this pull request Nov 15, 2021
Follow up on #492

Found another place where the `Capability::SSL` constant should be used.
jrfnl added a commit that referenced this pull request Nov 15, 2021
Follow up on #492

Found another place where the `Capability::SSL` constant should be used.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants