move cookie handling to Guzzle capabilities
This commit is contained in:
parent
e1e08ee313
commit
58a20c3387
@ -19,8 +19,10 @@ use Composer\InstalledVersions;
|
|||||||
use D3\KlicktippPhpClient\Exceptions\BaseException;
|
use D3\KlicktippPhpClient\Exceptions\BaseException;
|
||||||
use GuzzleHttp\Client;
|
use GuzzleHttp\Client;
|
||||||
use GuzzleHttp\ClientInterface;
|
use GuzzleHttp\ClientInterface;
|
||||||
|
use GuzzleHttp\Cookie\CookieJar;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use GuzzleHttp\Exception\RequestException;
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
use GuzzleHttp\RequestOptions;
|
||||||
use Psr\Http\Message\ResponseInterface;
|
use Psr\Http\Message\ResponseInterface;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
|
|
||||||
@ -34,7 +36,7 @@ class Connection
|
|||||||
|
|
||||||
protected string $secret_key;
|
protected string $secret_key;
|
||||||
|
|
||||||
protected ?string $cookie = null;
|
protected CookieJar $cookies_jar;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Contains the HTTP client (e.g. Guzzle)
|
* Contains the HTTP client (e.g. Guzzle)
|
||||||
@ -45,6 +47,7 @@ class Connection
|
|||||||
{
|
{
|
||||||
$this->client_key = $client_key;
|
$this->client_key = $client_key;
|
||||||
$this->secret_key = $secret_key;
|
$this->secret_key = $secret_key;
|
||||||
|
$this->cookies_jar = new CookieJar();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getClientKey(): string
|
public function getClientKey(): string
|
||||||
@ -94,16 +97,12 @@ class Connection
|
|||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$options['query'] = $options['query'] ?? [];
|
$options['query'] = $options['query'] ?? [];
|
||||||
|
$options[RequestOptions::COOKIES] = $this->getCookiesJar();
|
||||||
|
|
||||||
if (! empty($options['body'])) {
|
if (! empty($options['body'])) {
|
||||||
$options['body'] = json_encode($options['body']);
|
$options['body'] = json_encode($options['body']);
|
||||||
}
|
}
|
||||||
|
|
||||||
$header = [
|
|
||||||
'Cookie' => $this->cookie ?? ''
|
|
||||||
];
|
|
||||||
$options['headers'] = $header;
|
|
||||||
|
|
||||||
return $this->getClient()->request($method, $uri, $options);
|
return $this->getClient()->request($method, $uri, $options);
|
||||||
} catch (RequestException $e) {
|
} catch (RequestException $e) {
|
||||||
if ($e->hasResponse()) {
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,10 +24,6 @@ class Account extends Model
|
|||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
$this->connection->setCookie(
|
|
||||||
current($response->getHeader('set-cookie'))
|
|
||||||
);
|
|
||||||
|
|
||||||
return $this->connection->parseResponse($response);
|
return $this->connection->parseResponse($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -42,10 +38,6 @@ class Account extends Model
|
|||||||
['query' => $this->getQuery()]
|
['query' => $this->getQuery()]
|
||||||
);
|
);
|
||||||
|
|
||||||
if (current($response)) {
|
|
||||||
$this->connection->setCookie(null);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (bool) current($response);
|
return (bool) current($response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
namespace D3\KlicktippPhpClient\Resources;
|
namespace D3\KlicktippPhpClient\Resources;
|
||||||
|
|
||||||
use D3\KlicktippPhpClient\Connection;
|
use D3\KlicktippPhpClient\Connection;
|
||||||
|
use GuzzleHttp\RequestOptions;
|
||||||
|
|
||||||
abstract class Model
|
abstract class Model
|
||||||
{
|
{
|
||||||
@ -59,7 +60,10 @@ abstract class Model
|
|||||||
return array_filter(array_merge(
|
return array_filter(array_merge(
|
||||||
$this->query,
|
$this->query,
|
||||||
$this->filters,
|
$this->filters,
|
||||||
[ 'limit' => $this->getLimit(), 'offset' => $this->getOffset() ]
|
[
|
||||||
|
'limit' => $this->getLimit(),
|
||||||
|
'offset' => $this->getOffset()
|
||||||
|
]
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -5,6 +5,7 @@ namespace D3\KlicktippPhpClient\Resources;
|
|||||||
use D3\KlicktippPhpClient\Entities\Tag as TagEntity;
|
use D3\KlicktippPhpClient\Entities\Tag as TagEntity;
|
||||||
use D3\KlicktippPhpClient\Exceptions\BaseException;
|
use D3\KlicktippPhpClient\Exceptions\BaseException;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
|
use GuzzleHttp\RequestOptions;
|
||||||
|
|
||||||
class Tag extends Model
|
class Tag extends Model
|
||||||
{
|
{
|
||||||
@ -31,7 +32,7 @@ class Tag extends Model
|
|||||||
'GET',
|
'GET',
|
||||||
'tag/'.urlencode(trim($tagId)),
|
'tag/'.urlencode(trim($tagId)),
|
||||||
[
|
[
|
||||||
'query' => $this->getQuery()
|
RequestOptions::QUERY => $this->getQuery()
|
||||||
]
|
]
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user