move cookie handling to Guzzle capabilities

This commit is contained in:
Daniel Seifert 2024-12-21 19:17:08 +01:00
parent e1e08ee313
commit 58a20c3387
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
4 changed files with 14 additions and 18 deletions

View File

@ -19,8 +19,10 @@ use Composer\InstalledVersions;
use D3\KlicktippPhpClient\Exceptions\BaseException;
use GuzzleHttp\Client;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Cookie\CookieJar;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\Exception\RequestException;
use GuzzleHttp\RequestOptions;
use Psr\Http\Message\ResponseInterface;
use RuntimeException;
@ -34,7 +36,7 @@ class Connection
protected string $secret_key;
protected ?string $cookie = null;
protected CookieJar $cookies_jar;
/**
* Contains the HTTP client (e.g. Guzzle)
@ -45,6 +47,7 @@ class Connection
{
$this->client_key = $client_key;
$this->secret_key = $secret_key;
$this->cookies_jar = new CookieJar();
}
public function getClientKey(): string
@ -94,16 +97,12 @@ class Connection
{
try {
$options['query'] = $options['query'] ?? [];
$options[RequestOptions::COOKIES] = $this->getCookiesJar();
if (! empty($options['body'])) {
$options['body'] = json_encode($options['body']);
}
$header = [
'Cookie' => $this->cookie ?? ''
];
$options['headers'] = $header;
return $this->getClient()->request($method, $uri, $options);
} catch (RequestException $e) {
if ($e->hasResponse()) {
@ -166,8 +165,8 @@ class Connection
}
}
public function setCookie(?string $cookie): void
public function getCookiesJar(): CookieJar
{
$this->cookie = $cookie;
return $this->cookies_jar;
}
}

View File

@ -24,10 +24,6 @@ class Account extends Model
]
);
$this->connection->setCookie(
current($response->getHeader('set-cookie'))
);
return $this->connection->parseResponse($response);
}
@ -42,10 +38,6 @@ class Account extends Model
['query' => $this->getQuery()]
);
if (current($response)) {
$this->connection->setCookie(null);
}
return (bool) current($response);
}
}

View File

@ -3,6 +3,7 @@
namespace D3\KlicktippPhpClient\Resources;
use D3\KlicktippPhpClient\Connection;
use GuzzleHttp\RequestOptions;
abstract class Model
{
@ -59,7 +60,10 @@ abstract class Model
return array_filter(array_merge(
$this->query,
$this->filters,
[ 'limit' => $this->getLimit(), 'offset' => $this->getOffset() ]
[
'limit' => $this->getLimit(),
'offset' => $this->getOffset()
]
));
}

View File

@ -5,6 +5,7 @@ namespace D3\KlicktippPhpClient\Resources;
use D3\KlicktippPhpClient\Entities\Tag as TagEntity;
use D3\KlicktippPhpClient\Exceptions\BaseException;
use GuzzleHttp\Exception\GuzzleException;
use GuzzleHttp\RequestOptions;
class Tag extends Model
{
@ -31,7 +32,7 @@ class Tag extends Model
'GET',
'tag/'.urlencode(trim($tagId)),
[
'query' => $this->getQuery()
RequestOptions::QUERY => $this->getQuery()
]
);