Skip to content
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

Allow custom Http client options #15

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,18 @@ WORDPRESS_URL=https://example.com

## Usage

This package binds a singleton to the Laravel service container, so you can easily resolve the WordPress client directly from the container, or via dependency injection.
This package binds a singleton to the Laravel service container, so you can easily resolve the WordPress client directly from the container, or via dependency injection.
Alternatively, the package also exposes both a Facade and a helper function should you prefer a shorter more expressive option.

Currently, the package has support for the following WordPress resources: *categories, comments, media, pages, posts, users*.
Currently, the package has support for the following WordPress resources: *categories, comments, media, pages, posts, users*.
Adding support for further resources is really easy, but these are the only ones that I need for now! For a list of all available resources please see https://developer.wordpress.org/rest-api/reference. I'm happy to accept PR's for any additions.

```php
// Resolve service directly from container and access the Posts API
app(WordPress::class)->posts();

// Resolve via Facade and access the Posts API
WordPress::posts();
WordPress::posts();

// Resolve service via helper and access the Posts API
wordpress()->posts();
Expand All @@ -106,7 +106,7 @@ Call the `find` method on a resource class in order to get a single resource by
```php
WordPress::posts()->find(1);

// All WordPress resources share a handful of global parameters. https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/
// All WordPress resources share a handful of global parameters. https://developer.wordpress.org/rest-api/using-the-rest-api/global-parameters/
// You can use the relevant fluent builder methods to add these to your query
WordPress::posts()->embed()->fields('title')->find(1);

Expand All @@ -117,7 +117,7 @@ WordPress::posts()->find(1, ['password' => 'pa55w0rd']);
### Retrieve a collection of resources

Call the `get` method on a resource to retrieve a collection of resources. The response you receive can be controlled and filtered using various parameters, https://developer.wordpress.org/rest-api/reference/.
This package provides some fluent builder methods in order to easily and expressively build your desired query. Collection responses are then nicely formatted and include useful pagination information.
This package provides some fluent builder methods in order to easily and expressively build your desired query. Collection responses are then nicely formatted and include useful pagination information.

```php
WordPress::posts()->get();
Expand All @@ -135,24 +135,24 @@ WordPress::posts()
->exlclude(int|array $ids) // Ensure result set excludes specific IDs
->inlude(int|array $ids) // Limit result set to specific IDs
->orderBy(string $field, string $direction) // Sort collection by object attribute, either ascending or descending
// Resources with authors

// Resources with authors
->author() // Limit result set to resources assigned to specific authors
->authorExclude() // Ensure result set excludes resources assigned to specific authors

// Resources with dates
->after(Carbon $after) // Limit response to resources published after a given ISO8601 compliant date
->before(Carbon $before) // Limit response to resources published before a given ISO8601 compliant date
->latest() // Order by date, descending
->olders() // Order by date, ascending

// Resources with slugs
->slug(string $slug)

// When a utility doesn't exist for the parameter
->parameter(string $key, mixed $value) // Add a custom parameter to the query
// Send it!

// Send it!
->get();

// Conditionally adding parameters
Expand All @@ -177,6 +177,22 @@ WordPress::posts()->send('POST', 1, [

```

### HTTP Client options

Once you have a resource you can set Http client options on it before you make the request:

```php
// Using the Facade
WordPress::posts()->withOptions([
'verify' => false,
])->get();

// Using the helper
wordpress()->posts()->withOptions([
'verify' => false,
])->get();
```

## Testing

```bash
Expand Down
16 changes: 16 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ abstract class Resource

protected Query $query;

protected array $httpOptions = [];

protected string $wrap = 'data';

public function __construct(Client $client)
Expand All @@ -30,6 +32,7 @@ public function __construct(Client $client)
*/
public function send(string $method, int $id = null, array $options = []): Response
{
$options = array_merge($options, $this->httpOptions);
return $this->client->send($method, $this->endpoint().($id ? '/'.$id : ''), $options);
}

Expand Down Expand Up @@ -244,6 +247,19 @@ public function name(): string
);
}

/**
* Set HTTP options for the request.
*
* @param array $options
* @return $this
*/
public function withOptions(array $options)
{
$this->httpOptions = $options;

return $this;
}

/**
* @return string
*/
Expand Down
13 changes: 13 additions & 0 deletions tests/ResourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,19 @@ public function testGetCallsCorrectEndpoint(): void
});
}

public function testWithOptions(): void
{
$this->resource->withOptions(['connect_timeout' => 30, 'verify' => false])->get();

// There does not seem to be a way to test that a request was
// sent with a specific option.
// TODO: Find a way to test withOptions()

// Http::assertSent(function (Request $request) {
// return ??;
// });
}

public function testGetBuildsCorrectQuery(): void
{
$this->resource->latest()
Expand Down