Skip to content

Commit

Permalink
Cache http client (#116)
Browse files Browse the repository at this point in the history
  • Loading branch information
andig authored Feb 8, 2019
1 parent 22c4512 commit 46cae0e
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions src/CardDav/Backend.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Andig\Http\ClientTrait;
use Andig\Vcard\Parser;
use GuzzleHttp\Client;
use \stdClass;

/**
Expand All @@ -19,28 +20,34 @@ class Backend

/**
* CardDAV server url
*
* @var string
* @var string
*/
private $url;

/**
* VCard File URL Extension
*
* @var string
*/
private $vcard_extension = '.vcf';

/**
* Progress callback
* @var callable
*/
private $callback;

/**
* Set substitutions of links to embedded data
* @var array
*/
private $substitutes = [];

/**
* Cached http client
* @var Client|null
*/
private $client;

/**
* Constructor
* Sets the CardDAV server url
Expand Down Expand Up @@ -96,14 +103,27 @@ public function setProgress(callable $callback=null)
$this->callback = $callback;
}

/**
* Get initialized http client. Improves download performance by up to x7
*
* @return Client
*/
private function getCachedClient(): Client
{
if (!$this->client) {
$this->client = $this->getClient();
}
return $this->client;
}

/**
* Gets all vCards including additional information from the CardDAV server
*
* @return stdClass[] All parsed Vcards from backend
*/
public function getVcards(): array
{
$response = $this->getClient()->request('PROPFIND', $this->url);
$response = $this->getCachedClient()->request('PROPFIND', $this->url);
$body = (string)$response->getBody();
return $this->processPropFindResponse($body);
}
Expand Down Expand Up @@ -166,7 +186,7 @@ private function embedBase64(stdClass $vcard, string $substituteID): stdClass
*/
public function getLinkedData(string $uri): array
{
$response = $this->getClient()->request('GET', $uri);
$response = $this->getCachedClient()->request('GET', $uri);
$contentType = $response->getHeader('Content-Type');

@list($mimeType, $parameters) = explode(';', $contentType[0], 2);
Expand All @@ -191,7 +211,7 @@ public function getLinkedData(string $uri): array
public function getVcard(string $vcard_id): stdClass
{
$vcard_id = str_replace($this->vcard_extension, '', $vcard_id) . $this->vcard_extension;
$response = $this->getClient()->request('GET', $this->url . $vcard_id);
$response = $this->getCachedClient()->request('GET', $this->url . $vcard_id);

$body = (string)$response->getBody();

Expand Down

0 comments on commit 46cae0e

Please sign in to comment.