Skip to content

Commit

Permalink
feat: add support for setting additional HTTP request parameters (#14)
Browse files Browse the repository at this point in the history
Co-authored-by: XueSi <[email protected]>
  • Loading branch information
XueSiLf and XueSiLf authored Oct 17, 2024
1 parent 7f5a844 commit e815211
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/BaseFunc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class BaseFunc
protected $config;
protected $route;
protected $keywords;
protected $onceHeaders = [];

/**
* BaseFunc constructor.
Expand Down Expand Up @@ -43,6 +44,12 @@ protected function putJSON(BaseCommand $bean, $isJsonParams = true)
$http = new HttpClient($url);
if ($http) {
try {
if (!empty($this->onceHeaders)) {
foreach ($this->onceHeaders as $headerKey => $headerVal) {
$http->setHeader($headerKey, $headerVal, false);
}
$this->onceHeaders = [];
}
return $this->checkResponse($http->put($data));
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
Expand Down Expand Up @@ -70,8 +77,14 @@ protected function getJson(BaseCommand $bean, array $headers = [])
try {
if (isset($headers) && !empty($headers)) {
foreach ($headers as $headerKey => $headerVal) {
$http->setHeader($headerKey, $headerVal);
$http->setHeader($headerKey, $headerVal, false);
}
}
if (!empty($this->onceHeaders)) {
foreach ($this->onceHeaders as $headerKey => $headerVal) {
$http->setHeader($headerKey, $headerVal, false);
}
$this->onceHeaders = [];
}
return $this->checkResponse($http->get());
} catch (\Exception $exception) {
Expand Down Expand Up @@ -99,8 +112,14 @@ protected function postJson(BaseCommand $bean, array $headers = [])
try {
if (isset($headers) && !empty($headers)) {
foreach ($headers as $headerKey => $headerVal) {
$http->setHeader($headerKey, $headerVal);
$http->setHeader($headerKey, $headerVal, false);
}
}
if (!empty($this->onceHeaders)) {
foreach ($this->onceHeaders as $headerKey => $headerVal) {
$http->setHeader($headerKey, $headerVal, false);
}
$this->onceHeaders = [];
}
return $this->checkResponse($http->postJson($data));
} catch (\Exception $exception) {
Expand Down Expand Up @@ -128,9 +147,15 @@ protected function deleteJson(BaseCommand $bean, array $headers = [])
try {
if (isset($headers) && !empty($headers)) {
foreach ($headers as $headerKey => $headerVal) {
$http->setHeader($headerKey, $headerVal);
$http->setHeader($headerKey, $headerVal, false);
}
}
if (!empty($this->onceHeaders)) {
foreach ($this->onceHeaders as $headerKey => $headerVal) {
$http->setHeader($headerKey, $headerVal, false);
}
$this->onceHeaders = [];
}
return $this->checkResponse($http->delete());
} catch (\Exception $exception) {
throw new \Exception($exception->getMessage());
Expand Down Expand Up @@ -206,4 +231,9 @@ private function toRequestJson(BaseCommand $request): ?string

return $data ? json_encode($data) : null;
}

public function setOnceHeaders(array $onceHeaders)
{
$this->onceHeaders = $onceHeaders;
}
}

0 comments on commit e815211

Please sign in to comment.