Skip to content

Commit

Permalink
Merge pull request #14 from Sammyjo20/feature/xml
Browse files Browse the repository at this point in the history
Added withXMLBody trait
  • Loading branch information
Sammyjo20 authored Jan 23, 2022
2 parents 559d04a + a763d53 commit 9b767ff
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 43 deletions.
62 changes: 38 additions & 24 deletions .github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,8 @@ Sometimes you will need to send custom form data like XML or a stream. In this c
add the `HasBody` trait to your request class. After that, add a `defineBody` method on your request class. This method should return the raw body that you intend to send to the server. You should
also specify headers so the receiving server can understand what Content-Type you are sending.

[If you are sending XML, consider using the HasXMLBody trait.](#xml-data)

```php
<?php

Expand Down Expand Up @@ -546,29 +548,7 @@ class GetForgeServersRequest extends SaloonRequest
{
use HasQueryParams;

/**
* Define the method that the request will use.
*
* @var string|null
*/
protected ?string $method = Saloon::GET;

/**
* The connector.
*
* @var string|null
*/
protected ?string $connector = ForgeConnector::class;

/**
* Define the endpoint for the request.
*
* @return string
*/
public function defineEndpoint(): string
{
return '/servers';
}
// ...

/**
* Define query parameters on the request
Expand Down Expand Up @@ -608,9 +588,43 @@ $request->setQuery([
]);
```

### Other Plugins Available
## XML Data
If you are sending XML data to an XML service, you can use the `HasXMLBody` trait on your request which will automatically add the headers that you need to send XML. Once you add the trait,
make sure to add the `defineXMLBody` method where you can return your XML as a string.

```php
<?php

namespace App\Http\Saloon\Requests;

use App\Http\Saloon\Connectors\ForgeConnector;
use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Traits\Features\HasXMLBody;

class XMLRequest extends SaloonRequest
{
use HasXMLBody;

// ...

public function defineXmlBody(): string
{
return '<?xml version="1.0" encoding="UTF-8"?>';
}
}
```

### All Plugins Available

- AcceptsJson
- AlwaysThrowsOnErrors
- HasJsonBody
- HasBody
- HasXMLBody
- HasFormParams
- HasMultipartBody
- HasQueryParams
- HasTimeout
- WithDebugData
- DisablesSSLVerification (Please be careful with this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use \Exception;

class SaloonHasBodyException extends Exception
class SaloonTraitExistsException extends Exception
{
//
}
9 changes: 1 addition & 8 deletions src/Traits/Features/HasBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,17 @@

namespace Sammyjo20\Saloon\Traits\Features;

use Sammyjo20\Saloon\Exceptions\SaloonHasBodyException;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Exceptions\SaloonTraitExistsException;

trait HasBody
{
/**
* Define any form body.
*
* @return void
* @throws SaloonHasBodyException
* @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException
*/
public function bootHasBodyFeature(): void
{
if ($this instanceof SaloonRequest && $this->traitExistsOnConnector(HasBody::class)) {
throw new SaloonHasBodyException('You can not have the HasBody trait on both the request and the connector at the same time.');
}

$this->addConfig('body', $this->defineBody());
}

Expand Down
33 changes: 33 additions & 0 deletions src/Traits/Features/HasXMLBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace Sammyjo20\Saloon\Traits\Features;

use Sammyjo20\Saloon\Exceptions\SaloonTraitExistsException;

trait HasXMLBody
{
/**
* Add the required headers to send XML
*
* @return void
* @throws SaloonTraitExistsException
* @throws \Sammyjo20\Saloon\Exceptions\SaloonInvalidConnectorException
*/
public function bootHasXMLBodyFeature()
{
$this->addHeader('Accept', 'application/xml');
$this->addHeader('Content-Type', 'application/xml');

$this->addConfig('body', $this->defineXMLBody());
}

/**
* Define your XML body
*
* @return null
*/
public function defineXMLBody()
{
return null;
}
}
42 changes: 42 additions & 0 deletions tests/Resources/Requests/HasXMLRequest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Sammyjo20\Saloon\Tests\Resources\Requests;

use Sammyjo20\Saloon\Constants\Saloon;
use Sammyjo20\Saloon\Http\SaloonRequest;
use Sammyjo20\Saloon\Tests\Resources\Connectors\TestConnector;
use Sammyjo20\Saloon\Traits\Features\HasXMLBody;

class HasXMLRequest extends SaloonRequest
{
use HasXMLBody;

/**
* Define the method that the request will use.
*
* @var string|null
*/
protected ?string $method = Saloon::GET;

/**
* The connector.
*
* @var string|null
*/
protected ?string $connector = TestConnector::class;

/**
* Define the endpoint for the request.
*
* @return string
*/
public function defineEndpoint(): string
{
return '/user';
}

public function defineXMLBody()
{
return '<xml></xml>';
}
}
10 changes: 0 additions & 10 deletions tests/Unit/Features/HasBodyFeatureTest.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
<?php

use Psr\Http\Message\RequestInterface;
use Sammyjo20\Saloon\Exceptions\SaloonHasBodyException;
use Sammyjo20\Saloon\Tests\Resources\Requests\HasBodyConnectorRequest;
use Sammyjo20\Saloon\Tests\Resources\Requests\HasBodyRequest;

test('with the hasBody trait, you can pass in a string body response', function () {
Expand All @@ -18,11 +16,3 @@

$request->send();
});

test('it throws an exception if you try to add the hasBody trait to both the connector and the request', function () {
$request = new HasBodyConnectorRequest;

$this->expectException(SaloonHasBodyException::class);

$request->send();
});
23 changes: 23 additions & 0 deletions tests/Unit/Features/HasXMLBodyFeatureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Psr\Http\Message\RequestInterface;
use Sammyjo20\Saloon\Managers\RequestManager;
use Sammyjo20\Saloon\Tests\Resources\Requests\HasXMLRequest;

test('with the hasXMLBody trait, you can pass in a string body response', function () {
$request = new HasXMLRequest;

$requestManager = new RequestManager($request);
$requestManager->hydrate();

expect($requestManager->getHeaders())->toHaveKey('Accept', 'application/xml');
expect($requestManager->getHeaders())->toHaveKey('Content-Type', 'application/xml');

$request->addHandler('hasBodyHandler', function (callable $handler) {
return function (RequestInterface $request, array $options) use ($handler) {
expect($request->getBody()->getContents())->toEqual('<xml></xml>');

return $handler($request, $options);
};
});
});

0 comments on commit 9b767ff

Please sign in to comment.