Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set correct base URL when custom request adapter is used #1264

Merged
merged 3 commits into from
Jun 18, 2023
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions docs/Examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Iteration can be resumed by calling `iterate()` again.

$messages = $graphServiceClient->users()->byUserId(USER_ID)->messages()->get()->wait();

$pageIterator = new PageIterator($messages, $requestAdapter, [MessageCollectionResponse::class, 'createFromDiscriminatorValue']);
$pageIterator = new PageIterator($messages, $graphServiceClient->getRequestAdapter());

$callback = function (Message $message) {
echo "Message ID: {$message->getId()}";
Expand Down Expand Up @@ -316,7 +316,7 @@ $uploadSessionRequestBody->setAttachmentItem($attachmentItem);
$uploadSession = $graphServiceClient->users()->byUserId(USER_ID)->messages()->byMessageId('[id]')->attachments()->createUploadSession()->post($uploadSessionRequestBody)->wait();

// upload
$largeFileUpload = new LargeFileUploadTask($uploadSession, $requestAdapter, $file);
$largeFileUpload = new LargeFileUploadTask($uploadSession, $graphServiceClient->getRequestAdapter(), $file);
try{
$uploadSession = $largeFileUpload->upload()->wait();
} catch (\Psr\Http\Client\NetworkExceptionInterface $ex) {
Expand Down Expand Up @@ -451,7 +451,7 @@ You can also add requests to the `BatchRequestContent` via `addPsrRequest()`, `a
use Microsoft\Graph\BatchRequestBuilder;
use Microsoft\Graph\Core\Requests\BatchResponseItem;

$requestBuilder = new BatchRequestBuilder($requestAdapter);
$requestBuilder = new BatchRequestBuilder($graphServiceClient->getRequestAdapter());
/** @var BatchResponseContent $batchResponse */
$batchResponse = $requestBuilder->postAsync($batchRequestContent)->wait();

Expand Down
20 changes: 16 additions & 4 deletions src/GraphServiceClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,30 @@ public function __construct(
* Get an instance of GraphServiceClient that uses $requestAdapter
*
* @param RequestAdapter $requestAdapter
* @param string $nationalCloud Defaults to https://graph.microsoft.com. See
* https://learn.microsoft.com/en-us/graph/deployments
* @param string $nationalCloud Used to build base URL of $requestAdapter if none has been specified
* Defaults to https://graph.microsoft.com. See https://learn.microsoft.com/en-us/graph/deployments
* @return GraphServiceClient
*/
public static function createWithRequestAdapter(
RequestAdapter $requestAdapter,
string $nationalCloud = NationalCloud::GLOBAL
): GraphServiceClient
{
$requestAdapter->setBaseUrl($nationalCloud);
if (!$requestAdapter->getBaseUrl()) {
$requestAdapter->setBaseUrl("$nationalCloud/v1.0");
}
$placeholder = new ClientCredentialContext('tenant', 'client', 'secret');
return new GraphServiceClient($placeholder, [], $nationalCloud, $requestAdapter);
return new GraphServiceClient($placeholder, [], 'placeholder', $requestAdapter);
}

/**
* Returns the request adapter instance in use
*
* @return RequestAdapter
*/
public function getRequestAdapter(): RequestAdapter
{
return $this->requestAdapter;
}

/**
Expand Down
22 changes: 20 additions & 2 deletions tests/GraphServiceClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Microsoft\Graph\Test;

use Microsoft\Graph\Core\BaseGraphRequestAdapter;
use Microsoft\Graph\GraphRequestAdapter;
use Microsoft\Graph\Core\NationalCloud;
use Microsoft\Graph\GraphServiceClient;
use Microsoft\Kiota\Abstractions\Authentication\AnonymousAuthenticationProvider;
use Microsoft\Kiota\Authentication\Oauth\ClientCredentialContext;
Expand All @@ -22,4 +21,23 @@ public function testsInit(): void
$client = new GraphServiceClient($testContext, ['Users.Read']);
$this->assertInstanceOf(GraphServiceClient::class, $client);
}

public function testCorrectDefaultBaseUrlIsSetWhenRequestAdapterHasEmptyBaseUrl(): void
{
$requestAdapter = new GuzzleRequestAdapter(new AnonymousAuthenticationProvider());
$client = GraphServiceClient::createWithRequestAdapter($requestAdapter);
$this->assertEquals(NationalCloud::GLOBAL.'/v1.0', $client->getRequestAdapter()->getBaseUrl());

$requestAdapter = new GuzzleRequestAdapter(new AnonymousAuthenticationProvider());
$client = GraphServiceClient::createWithRequestAdapter($requestAdapter, NationalCloud::CHINA);
$this->assertEquals(NationalCloud::CHINA.'/v1.0', $client->getRequestAdapter()->getBaseUrl());
}

public function testCustomBaseUrlIsNotOverriden(): void
{
$requestAdapter = new GuzzleRequestAdapter(new AnonymousAuthenticationProvider());
$requestAdapter->setBaseUrl(NationalCloud::CHINA);
$client = GraphServiceClient::createWithRequestAdapter($requestAdapter, NationalCloud::US_DOD);
$this->assertEquals(NationalCloud::CHINA, $client->getRequestAdapter()->getBaseUrl());
}
}