Skip to content

Commit

Permalink
Added possibility to alter headers for Asset Service.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuba Płaskonka committed May 7, 2020
1 parent f8dbda2 commit 13fdf5d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 17 additions & 5 deletions src/PimcoreDevkitBundle/Service/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*/
class AssetService
{
private const DEFAULT_FILE_TYPE = 'txt';
public const DEFAULT_FILE_TYPE = 'txt';

/** @var ClientInterface */
protected $httpClient;
Expand Down Expand Up @@ -78,28 +78,39 @@ protected function getHttpClient(): ClientInterface
return $this->httpClient;
}

/**
* @param ClientInterface $httpClient
*/
public function setHttpClient(ClientInterface $httpClient): void
{
$this->httpClient = $httpClient;
}

/**
* @param string $url
* @param Folder $targetFolder
* @param null $filename
* @param string $defaultFileType
* @param array $headers Headers to append
* @return HttpAssetInterface
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function prepareHttpAsset(
string $url,
Folder $targetFolder,
$filename = null,
string $defaultFileType = self::DEFAULT_FILE_TYPE
string $defaultFileType = self::DEFAULT_FILE_TYPE,
array $headers = []
): HttpAssetInterface {
return new HttpAsset($this->getHttpClient(), $url, $targetFolder, $filename, $defaultFileType);
return new HttpAsset($this->getHttpClient(), $url, $targetFolder, $filename, $defaultFileType, $headers);
}

/**
* @param string $url
* @param Folder $targetFolder
* @param null $filename
* @param string $defaultFileType
* @param array $headers Headers to append
* @return Asset
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Exception
Expand All @@ -108,9 +119,10 @@ public function getHttpAsset(
string $url,
Folder $targetFolder,
$filename = null,
string $defaultFileType = self::DEFAULT_FILE_TYPE
string $defaultFileType = self::DEFAULT_FILE_TYPE,
array $headers = []
): Asset {
$httpAsset = $this->prepareHttpAsset($url, $targetFolder, $filename, $defaultFileType);
$httpAsset = $this->prepareHttpAsset($url, $targetFolder, $filename, $defaultFileType, $headers);

return $httpAsset->getAsset();
}
Expand Down
5 changes: 3 additions & 2 deletions src/PimcoreDevkitBundle/Wrapper/HttpAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,10 @@ public function __construct(
string $url,
Folder $targetFolder,
string $filename = null,
string $defaultFileType = 'txt'
string $defaultFileType = 'txt',
array $headers = []
) {
$this->response = $httpClient->request('GET', $url, ['timeout' => 5, 'connect_timeout' => 2]);
$this->response = $httpClient->request('GET', $url, ['headers' => $headers, 'connect_timeout' => 1]);
$statusCode = $this->response->getStatusCode();

if ($statusCode != 200) {
Expand Down

0 comments on commit 13fdf5d

Please sign in to comment.