Skip to content

Commit c02a9ce

Browse files
committed
add cookie support to requests
1 parent 56b45ac commit c02a9ce

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

src/Client.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public function send(RequestInterface $request): ResponseInterface
3737
ContentType::MULTIPART => $client->attach($request->getFiles()),
3838
};
3939

40+
if ($request->getCookies()) {
41+
$client->withCookies($request->getCookies(), (string) parse_url($request->getUrl(), PHP_URL_HOST));
42+
}
43+
4044
$startTime = microtime(true);
4145

4246
$response = $client->{$request->getMethod()->value}($request->getUrl(), $request->getBody());

src/Request.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class Request implements RequestInterface
1010
{
1111
protected array $files = [];
1212

13+
protected array $cookies = [];
14+
1315
protected ContentType $contentType = ContentType::JSON;
1416

1517
public function __construct(
@@ -120,4 +122,22 @@ public function getContentType(): ContentType
120122
{
121123
return $this->contentType;
122124
}
125+
126+
/**
127+
* {@inheritDoc}
128+
*/
129+
public function addCookie(string $name, string $value): static
130+
{
131+
$this->cookies[$name] = $value;
132+
133+
return $this;
134+
}
135+
136+
/**
137+
* {@inheritDoc}
138+
*/
139+
public function getCookies(): array
140+
{
141+
return $this->cookies;
142+
}
123143
}

src/RequestInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,14 @@ public function getFiles(): array;
3939
* Get the content type of the request (e.g., JSON, FORM, etc.).
4040
*/
4141
public function getContentType(): ContentType;
42+
43+
/**
44+
* Add a cookie to the request.
45+
*/
46+
public function addCookie(string $name, string $value): static;
47+
48+
/**
49+
* Get all cookies for the request.
50+
*/
51+
public function getCookies(): array;
4252
}

0 commit comments

Comments
 (0)