diff --git a/src/Core/GraphConstants.php b/src/Core/GraphConstants.php index 39a79cdb..be83a353 100644 --- a/src/Core/GraphConstants.php +++ b/src/Core/GraphConstants.php @@ -18,6 +18,9 @@ final class GraphConstants { + const BETA_API_VERSION = "beta"; + const V1_API_VERSION = "v1.0"; + // These can be overwritten in setters in the Graph object const REST_ENDPOINT = "https://graph.microsoft.com/"; diff --git a/src/Http/GraphRequest.php b/src/Http/GraphRequest.php index cb4ea4a7..a6d4f349 100644 --- a/src/Http/GraphRequest.php +++ b/src/Http/GraphRequest.php @@ -344,7 +344,11 @@ public function upload(string $path, ?ClientInterface $client = null) private function initHeaders(string $baseUrl): void { $coreSdkVersion = "graph-php-core/".GraphConstants::SDK_VERSION; - $serviceLibSdkVersion = "Graph-php-".$this->graphClient->getSdkVersion(); + if ($this->graphClient->getApiVersion() === GraphConstants::BETA_API_VERSION) { + $serviceLibSdkVersion = "graph-php-beta/".$this->graphClient->getSdkVersion(); + } else { + $serviceLibSdkVersion = "graph-php/".$this->graphClient->getSdkVersion(); + } if (NationalCloud::containsNationalCloudHost($this->requestUri)) { $this->headers = [ 'Content-Type' => 'application/json', diff --git a/tests/Http/Request/GraphRequestTest.php b/tests/Http/Request/GraphRequestTest.php index 7786e651..dfd91e57 100644 --- a/tests/Http/Request/GraphRequestTest.php +++ b/tests/Http/Request/GraphRequestTest.php @@ -91,7 +91,7 @@ public function testConstructorGivenInvalidFullEndpointUriAppendsItToDefaultBase public function testConstructorSetsExpectedHeadersGivenValidGraphBaseUrl(): void { $expectedHeaders = [ 'Content-Type' => ['application/json'], - 'SdkVersion' => ["graph-php-core/".GraphConstants::SDK_VERSION.", Graph-php-".$this->mockGraphClient->getSdkVersion()], + 'SdkVersion' => ["graph-php-core/".GraphConstants::SDK_VERSION.", graph-php/".$this->mockGraphClient->getSdkVersion()], 'Authorization' => ['Bearer ' . $this->mockGraphClient->getAccessToken()], 'Host' => [substr($this->mockGraphClient->getNationalCloud(), strlen("https://"))] ]; @@ -99,6 +99,18 @@ public function testConstructorSetsExpectedHeadersGivenValidGraphBaseUrl(): void $this->assertEquals($expectedHeaders, $request->getHeaders()); } + public function testConstructorSetsExpectedBetaSdkVersionHeader(): void { + $graphClient = $this->createMock(AbstractGraphClient::class); + $graphClient->method('getAccessToken')->willReturn("abc"); + $graphClient->method('getNationalCloud')->willReturn(NationalCloud::GLOBAL); + $graphClient->method('getSdkVersion')->willReturn('2.0.0'); + $graphClient->method('getApiVersion')->willReturn(GraphConstants::BETA_API_VERSION); + + $request = new GraphRequest("GET", "/me", $graphClient); + $expected = ["graph-php-core/".GraphConstants::SDK_VERSION.", graph-php-beta/".$graphClient->getSdkVersion()]; + $this->assertEquals($expected, $request->getHeaders()["SdkVersion"]); + } + public function testConstructorSetsExpectedHeadersGivenValidCustomBaseUrl(): void { $baseUrl = "https://www.outlook.com"; $expectedHeaders = [ @@ -113,7 +125,7 @@ public function testConstructorSetsExpectedHeadersGivenGraphEndpointUrl(): void $endpoint = "https://graph.microsoft.com/v1.0/me/users\$skip=10&\$top=5"; $expectedHeaders = [ 'Content-Type' => ['application/json'], - 'SdkVersion' => ["graph-php-core/".GraphConstants::SDK_VERSION.", Graph-php-".$this->mockGraphClient->getSdkVersion()], + 'SdkVersion' => ["graph-php-core/".GraphConstants::SDK_VERSION.", graph-php/".$this->mockGraphClient->getSdkVersion()], 'Authorization' => ['Bearer ' . $this->mockGraphClient->getAccessToken()], 'Host' => [substr($this->mockGraphClient->getNationalCloud(), strlen("https://"))] ];