Skip to content

Commit

Permalink
Added CookieJar support to ArtaxHttpOptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Apr 16, 2018
1 parent 339ea83 commit 04a2174
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
18 changes: 18 additions & 0 deletions src/ArtaxHttpOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,26 @@
namespace ScriptFUSION\Porter\Net\Http;

use Amp\Artax\Client;
use Amp\Artax\Cookie\CookieJar;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;

/**
* Encapsulates Artax HTTP client options.
*/
final class ArtaxHttpOptions extends EncapsulatedOptions
{
private $cookieJar;

public function __construct(CookieJar $cookieJar)
{
$this->cookieJar = $cookieJar;
}

public function __clone()
{
$this->cookieJar = clone $this->cookieJar;
}

public function setAutoEncoding(bool $autoEncoding): self
{
return $this->set(Client::OP_AUTO_ENCODING, $autoEncoding);
Expand Down Expand Up @@ -51,6 +64,11 @@ public function setMaxBodyBytes(int $maxBodyBytes): self
return $this->set(Client::OP_MAX_BODY_BYTES, $maxBodyBytes);
}

public function getCookieJar(): CookieJar
{
return $this->cookieJar;
}

public function extractArtaxOptions(): array
{
return $this->copy();
Expand Down
7 changes: 5 additions & 2 deletions src/AsyncHttpConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace ScriptFUSION\Porter\Net\Http;

use Amp\Artax\Cookie\ArrayCookieJar;
use Amp\Artax\DefaultClient;
use Amp\Artax\Response;
use Amp\Promise;
Expand All @@ -14,9 +15,11 @@ class AsyncHttpConnector implements AsyncConnector, ConnectorOptions
{
private $options;

private $cookieJar;

public function __construct(ArtaxHttpOptions $options = null)
{
$this->options = $options ?: new ArtaxHttpOptions;
$this->options = $options ?: new ArtaxHttpOptions($this->cookieJar = new ArrayCookieJar);
}

public function __clone()
Expand All @@ -27,7 +30,7 @@ public function __clone()
public function fetchAsync(ConnectionContext $context, string $source): Promise
{
return \Amp\call(function () use ($context, $source) {
$client = new DefaultClient;
$client = new DefaultClient($this->cookieJar);
$client->setOptions($this->getOptions()->extractArtaxOptions());

return $context->retry(static function () use ($client, $source) {
Expand Down

0 comments on commit 04a2174

Please sign in to comment.