Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions src/Query/CurlQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class CurlQuery
private $expectJSON_ignoreResContentType;
/** @var bool */
private $debug;
/** @var string */
private string $responseType;

/**
* CurlQuery constructor.
Expand Down Expand Up @@ -85,19 +87,6 @@ public function auth(): Authentication
return $this->auth;
}

/**
* @return SSL
* @throws \Comely\Http\Exception\SSL_Exception
*/
public function ssl(): SSL
{
if (!$this->ssl) {
$this->ssl = new SSL();
}

return $this->ssl;
}

/**
* @param int $version
* @return CurlQuery
Expand All @@ -112,6 +101,16 @@ public function useHttpVersion(int $version): self
return $this;
}

/**
* @param string|null $responseType
* @return $this
*/
public function forceResponseType(?string $responseType = null): self
{
$this->responseType = $responseType ?? "application/json; charset=utf-8";
return $this;
}

/**
* @param string|null $agent
* @return CurlQuery
Expand Down Expand Up @@ -271,16 +270,21 @@ public function send(): CurlResponse
$responseIsJSON = is_string($responseType) && preg_match('/json/', $responseType) ? true : $this->expectJSON;
if ($responseIsJSON) {
if (!$this->expectJSON_ignoreResContentType) {
if ($this->responseType) {
$responseType = $this->responseType;
}
if (!is_string($responseType)) {
throw new HttpResponseException('Invalid "Content-type" header received, expecting JSON', $responseCode);
}

if (strtolower(trim(explode(";", $responseType)[0])) !== "application/json") {

throw new HttpResponseException(
sprintf('Expected "application/json", got "%s"', $responseType),
$responseCode
);
}

}

// Decode JSON body
Expand All @@ -296,11 +300,24 @@ public function send(): CurlResponse
throw new HttpResponseException('An error occurred while decoding JSON body');
}

if(is_array($json)) {
if (is_array($json)) {
$response->payload()->use($json);
}
}

return $response;
}

/**
* @return SSL
* @throws \Comely\Http\Exception\SSL_Exception
*/
public function ssl(): SSL
{
if (!$this->ssl) {
$this->ssl = new SSL();
}

return $this->ssl;
}
}