Skip to content

Commit

Permalink
allow argument to override guzzle client config
Browse files Browse the repository at this point in the history
  • Loading branch information
frets1700 committed Jan 31, 2024
1 parent adaa7d2 commit 06ec95f
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public function getSelect(): ?array;

public function getDeltaToken(): ?string;

/**
* @return array<string, mixed>
*/
public function getRequestOptions(): array;

/**
* @return array<string, string>|null
*/
Expand Down
17 changes: 17 additions & 0 deletions src/Utilities/CalendarView/CalendarViewParams.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class CalendarViewParams extends CalendarViewRequestBuilderGetQueryParameters im
private ?string $preferHeaders = null;
private ?string $timezone = 'Eastern Standard Time';

/** @var array<string, mixed> */
private array $requestOptions = [];

public function getStartDateTime(): ?string
{
return $this->startDateTime;
Expand Down Expand Up @@ -81,6 +84,11 @@ public function getPreferHeaders(): ?string
return $this->preferHeaders ?? 'odata.maxpagesize=50,odata.track-changes,outlook.timezone="' . $this->timezone . '"';
}

public function getRequestOptions(): array
{
return $this->requestOptions;
}

public function setStartDateTime(string $startDateTime): CalendarViewParams
{
$this->startDateTime = $startDateTime;
Expand Down Expand Up @@ -161,4 +169,13 @@ public function setTimezone(?string $timezone): CalendarViewParams
$this->timezone = $timezone;
return $this;
}

/**
* @param array<string, mixed> $requestOptions
*/
public function setRequestOptions(array $requestOptions): CalendarViewParams
{
$this->requestOptions = $requestOptions;
return $this;
}
}
8 changes: 6 additions & 2 deletions src/Utilities/CalendarView/GraphServiceCalendarView.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class GraphServiceCalendarView
public const BASE_URI = NationalCloud::GLOBAL;
public const ENABLE_HTTP_ERROR = false;
public const HTTP_VERIFY = false;
public const GUZZLE_HTTP_CONFIG_KEY = 'guzzleConfig';

protected ?RequestAdapter $requestAdapter;
protected ?Client $httpClient = null;
Expand All @@ -45,10 +46,13 @@ public function client(CalendarViewParamsInterface $params): MSGraphServiceClien
'deltaToken' => $params->getDeltaToken()
]));

$client = GraphClientFactory::createWithConfig(array_merge(
$guzzleConfig = array_merge(
static::getDefaultConfig(),
$params->getRequestOptions(),
['handler' => $handlerStack]
));
);

$client = GraphClientFactory::createWithConfig($guzzleConfig);
}

$this->requestAdapter = new GraphRequestAdapter(
Expand Down
6 changes: 4 additions & 2 deletions src/Utilities/EventView/GraphServiceEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ public function client(mixed $params = null): MSGraphServiceClient
$client = $params['client'];
} else {
$handlerStack = GraphClientFactory::getDefaultHandlerStack();
$client = GraphClientFactory::createWithConfig(array_merge(
$guzzleConfig = array_merge(
static::getDefaultConfig(),
$params[static::GUZZLE_HTTP_CONFIG_KEY] ?? [],
['handler' => $handlerStack]
));
);
$client = GraphClientFactory::createWithConfig($guzzleConfig);
}

$this->requestAdapter = new GraphRequestAdapter(
Expand Down
41 changes: 39 additions & 2 deletions tests/Utilities/CalendarView/GraphServiceCalendarViewTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

namespace Symplicity\Outlook\Tests\Utilities\CalendarView;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Abstractions\RequestAdapter;
use PHPUnit\Framework\TestCase;
use Symplicity\Outlook\Utilities\CalendarView\CalendarViewParams;
Expand All @@ -17,9 +20,43 @@ public function testClient()
$params->setOrderBy(['start', 'end']);
$params->setDeltaToken('foo-delta-token==');
$params->setFilter('Extensions(id eq \'symplicity.com\'');

$graphClient = new GraphServiceCalendarView('foo', 'bar', 'token_1');
$actualClient = $graphClient->client($params);
$this->assertInstanceOf(RequestAdapter::class, $actualClient->getRequestAdapter());
$this->assertSame('https://graph.microsoft.com/v1.0', $actualClient->getRequestAdapter()->getBaseUrl());
$requestAdapter = $actualClient->getRequestAdapter();
$this->assertInstanceOf(RequestAdapter::class, $requestAdapter);
$this->assertSame('https://graph.microsoft.com/v1.0', $requestAdapter->getBaseUrl());

$values = $this->getGuzzleConfig($requestAdapter, $actualClient);
$this->assertIsArray($values);
$this->assertSame(GraphServiceCalendarView::DEFAULT_CONNECT_TIMEOUT, $values['connect_timeout']);
$this->assertSame(GraphServiceCalendarView::DEFAULT_TIMEOUT, $values['timeout']);
$this->assertSame(GraphServiceCalendarView::HTTP_VERIFY, $values['verify']);

$params->setRequestOptions([
RequestOptions::CONNECT_TIMEOUT => 6.0,
RequestOptions::TIMEOUT => 8.0,
RequestOptions::VERIFY => true
]);

$actualClient = $graphClient->client($params);
$requestAdapter = $actualClient->getRequestAdapter();
$values = $this->getGuzzleConfig($requestAdapter, $actualClient);
$this->assertIsArray($values);
$this->assertSame(6.0, $values['connect_timeout']);
$this->assertSame(8.0, $values['timeout']);
$this->assertTrue($values['verify']);
}

private function getGuzzleConfig(RequestAdapter $requestAdapter, GraphServiceClient $client): mixed
{
$reflection = new \ReflectionObject($requestAdapter);
$reflectionProperty = $reflection->getParentClass()->getParentClass()->getProperty('guzzleClient');

/** @var Client $guzzleClient */
$guzzleClient = $reflectionProperty->getValue($client->getRequestAdapter());

$guzzleConfigProperty = new \ReflectionProperty(Client::class, 'config');
return $guzzleConfigProperty->getValue($guzzleClient);
}
}

0 comments on commit 06ec95f

Please sign in to comment.