Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
b23cc04
Add Http client factory
Ndiritu Jun 27, 2021
4c3fa35
Update Graph class to use HttpClientFactory
Ndiritu Jun 28, 2021
0293b63
Remove proxy configuration from Graph, GraphRequest and GraphCollecti…
Ndiritu Jun 28, 2021
7231294
Update copyright statement, class and interface php docs
Ndiritu Jun 29, 2021
b6cdb1a
Remove TODO comments
Ndiritu Jun 29, 2021
3665dd2
Set the httpClient on a Graph client only through the constructor
Ndiritu Jun 29, 2021
8762088
Make HttpClientFactory methods static
Ndiritu Jun 29, 2021
719d50c
Undo Windows-style CRLF newlines added by editor
Ndiritu Jun 29, 2021
4dc0e87
Update editorConfig to use Unix-style line endings (LF)
Ndiritu Jun 29, 2021
b95fc51
Restore GraphTest file to LF line endings
Ndiritu Jun 29, 2021
2f161e0
Improve validation of national cloud urls
Ndiritu Jun 30, 2021
b044859
Fix GraphException toString() to use child exception class names if n…
Ndiritu Jun 30, 2021
bca9780
Add reference to Guzzle client configuration documentation
Ndiritu Jun 30, 2021
acd2855
Set default apiVersion and nationalCloud on Graph client if empty str…
Ndiritu Jun 30, 2021
17bb389
Allow non-national cloud but valid hosts to be set as base URL of Gra…
Ndiritu Jun 30, 2021
f11850d
Improve base url and national cloud host validation
Ndiritu Jul 1, 2021
20186f0
Change ClientInitialisationException to GraphClientException
Ndiritu Jul 1, 2021
607d4e8
Renamed Graph to BaseClient
Ndiritu Jul 5, 2021
13112b4
Refactor GraphRequest and GraphCollectionRequest constructors
Ndiritu Jul 7, 2021
bdd2ed5
Make base graph client abstract
Ndiritu Jul 7, 2021
efb8bc8
Fix initialisation of default graph request headers
Ndiritu Jul 7, 2021
bed36da
Update PSR-7 request object when headers or body is updated
Ndiritu Jul 7, 2021
35ce3ff
Support PSR-18 and HttPlug async clients
Ndiritu Jul 8, 2021
8a92a86
GraphCollectionRequest updates
Ndiritu Jul 8, 2021
8583d17
Initialise http request object using init method in constructor
Ndiritu Jul 8, 2021
f1bb80f
Make national cloud host check case-insensitive
Ndiritu Jul 8, 2021
bfb37ca
Fix method access modifiers in AbstractGraphClient
Ndiritu Jul 12, 2021
7606355
Merge pull request #4 from microsoftgraph/philip/feat/graph-request-c…
Ndiritu Aug 18, 2021
018c427
Resolve merge conflicts
Ndiritu Aug 18, 2021
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
54 changes: 27 additions & 27 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
# top-most EditorConfig file
root = true

# All PHP files MUST use the Unix LF (linefeed) line ending.
# Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting.
# All PHP files MUST end with a single blank line.
# There MUST NOT be trailing whitespace at the end of non-blank lines.
[*]
charset = utf-8
end_of_line = crlf
insert_final_newline = true
trim_trailing_whitespace = true

# PHP-Files, Composer.json, MD-Files
[{*.php,composer.json,*.md}]
indent_style = space
indent_size = 4

# HTML-Files LESS-Files SASS-Files CSS-Files JS-Files JSON-Files
[{*.html,*.less,*.sass,*.css,*.js,*.json}]
indent_style = tab
indent_size = 4

# Gitlab-CI, Travis-CI
[*.yml]
indent_style = space
indent_size = 2
# top-most EditorConfig file
root = true
# All PHP files MUST use the Unix LF (linefeed) line ending.
# Code MUST use an indent of 4 spaces, and MUST NOT use tabs for indenting.
# All PHP files MUST end with a single blank line.
# There MUST NOT be trailing whitespace at the end of non-blank lines.
[*]
charset = utf-8
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
# PHP-Files, Composer.json, MD-Files
[{*.php,composer.json,*.md}]
indent_style = space
indent_size = 4
# HTML-Files LESS-Files SASS-Files CSS-Files JS-Files JSON-Files
[{*.html,*.less,*.sass,*.css,*.js,*.json}]
indent_style = tab
indent_size = 4
# Gitlab-CI, Travis-CI
[*.yml]
indent_style = space
indent_size = 2
74 changes: 38 additions & 36 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,38 @@
{
"name": "microsoft/microsoft-graph-core",
"type": "library",
"description": "The Microsoft Graph SDK for PHP",
"homepage": "https://developer.microsoft.com/en-us/graph",
"license": "MIT",
"authors": [
{
"name": "Microsoft Graph Client Tooling",
"email": "graphtooling@service.microsoft.com",
"role": "Developer"
}
],
"require": {
"php": "^8.0 || ^7.3",
"guzzlehttp/guzzle": "^6.0 || ^7.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0 || ^9.0",
"mikey179/vfsstream": "^1.2",
"vimeo/psalm": "^4.8",
"microsoft/microsoft-graph": "^1.34"
},
"autoload": {
"psr-4": {
"Microsoft\\Graph\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Microsoft\\Graph\\Test\\": "tests/Functional/",
"Microsoft\\Graph\\Http\\Test\\": "tests/Http/"
}
}
}
{
"name": "microsoft/microsoft-graph-core",
"type": "library",
"description": "The Microsoft Graph SDK for PHP",
"homepage": "https://developer.microsoft.com/en-us/graph",
"license": "MIT",
"authors": [
{
"name": "Microsoft Graph Client Tooling",
"email": "graphtooling@service.microsoft.com",
"role": "Developer"
}
],
"require": {
"php": "^8.0 || ^7.3",
"guzzlehttp/guzzle": "^7.0",
"php-http/httplug": "^2.2.0",
"php-http/guzzle7-adapter": "^1.0.0",
"ext-json": "*"
},
"require-dev": {
"phpunit/phpunit": "^8.0 || ^9.0",
"mikey179/vfsstream": "^1.2",
"vimeo/psalm": "^4.8",
"microsoft/microsoft-graph": "^1.34"
},
"autoload": {
"psr-4": {
"Microsoft\\Graph\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Microsoft\\Graph\\Test\\": "tests/Functional/",
"Microsoft\\Graph\\Http\\Test\\": "tests/Http/"
}
}
}
2 changes: 1 addition & 1 deletion src/Core/Enum.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,4 @@ public function value()
{
return $this->_value;
}
}
}
48 changes: 48 additions & 0 deletions src/Core/NationalCloud.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*/

namespace Microsoft\Graph\Core;

/**
* Class NationalCloud
*
* Defines Graph hosts for the various national clouds
*
* @package Microsoft\Graph\Core
* @copyright 2021 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://developer.microsoft.com/graph
*/
final class NationalCloud
{
const GLOBAL = "https://graph.microsoft.com";
const US_GOV = "https://graph.microsoft.us";
const US_DOD = "https://dod-graph.microsoft.us";
const GERMANY = "https://graph.microsoft.de";
const CHINA = "https://microsoftgraph.chinacloudapi.cn";

/**
* Stores all enum values as list
* Prevents having to do reflection for each call to getValues()
*
* @var array
*/
private static $values = [];

/**
* Returns a list of the constant values
*
* @return array
*/
public static function getValues(): array {
if (!self::$values) {
$reflectedClass = new \ReflectionClass(__CLASS__);
self::$values = array_values($reflectedClass->getConstants());
}
return self::$values;
}
}
21 changes: 21 additions & 0 deletions src/Exception/ClientInitialisationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Copyright (c) Microsoft Corporation. All Rights Reserved.
* Licensed under the MIT License. See License in the project root
* for license information.
*/

namespace Microsoft\Graph\Exception;

/**
* Class ClientInitialisationException
*
* @package Microsoft\Graph\Exception
* @copyright 2021 Microsoft Corporation
* @license https://opensource.org/licenses/MIT MIT License
* @link https://developer.microsoft.com/graph
*/
class ClientInitialisationException extends GraphException
{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It extends all functionality of the GraphException with nothing custom to add.

Thanks for pointing out a bug though, I need to change GraphException's __toString() to print the child class name where needed

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the purpose of GraphException class?

For the JS SDK, @MIchaelMainer had suggested creating a GraphCientError class which handles the errors such as ClientInitialisationException and a GraphError class which handles errors from the API.

I would suggest using something similar because:

  • There is a sync in the SDKs
  • Avoid creating an empty class if you are not extending or adding more functionalities.
  • A generic GraphClientError class with clear messages reduces the need to create multiple class depending on the feature. For example, ClientInitialization, an error class for PageIteration.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for catching this! We still need to better define our error classification. At a high-level, we have two primary exceptions: ServiceException that represents errors returned in an API response and ClientException for issues raised in the client. Both of these can extend GraphException.

I just created a quick PR to help us be more consistent.

microsoftgraph/msgraph-sdk-design#47

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated ClientInitialisationException to GraphClientException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Ndiritu Graph Exception that is the parent class is it the service exception class?
What is the class which captures the Graph API error response?

If GraphException is the service error response class then I recommend you to keep them separate as mentioned by @MIchaelMainer in the PR microsoftgraph/msgraph-sdk-design#47

I suppose you can address this in a separate PR focusing on the error classes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nikithauc Yes, i'll need to make these changes in a separate PR.


}
107 changes: 37 additions & 70 deletions src/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@
namespace Microsoft\Graph;

use Microsoft\Graph\Core\GraphConstants;
use Microsoft\Graph\Core\NationalCloud;
use Microsoft\Graph\Exception\ClientInitialisationException;
use Microsoft\Graph\Http\GraphCollectionRequest;
use Microsoft\Graph\Http\GraphRequest;
use Microsoft\Graph\Http\HttpClientFactory;
use Microsoft\Graph\Http\HttpClientInterface;

/**
* Class Graph
Expand All @@ -38,68 +42,51 @@ class Graph
* @var string
*/
private $_accessToken;

/**
* The api version to use ("v1.0", "beta")
* Default is "v1.0"
*
* @var string
*/
private $_apiVersion;
/**
* The base url to call
* Default is "https://graph.microsoft.com"
*
* @var string
*/
private $_baseUrl;
/**
* The port to use for proxy requests
* Null disables port forwarding
*
* @var string
*/
private $_proxyPort;
private $_apiVersion = GraphConstants::API_VERSION;

/**
* Whether SSL verification should be used for proxy requests
*
* @var bool
* Host to use as the base URL and for authentication
* @var string
*/
private $_proxyVerifySSL;
private $_nationalCloud = NationalCloud::GLOBAL;

/**
* Creates a new Graph object, which is used to call the Graph API
*/
public function __construct()
{
$this->_apiVersion = GraphConstants::API_VERSION;
$this->_baseUrl = GraphConstants::REST_ENDPOINT;
}
* HttpClient to use for requests
* @var HttpClientInterface
*/
private $_httpClient;

/**
* Sets the Base URL to call (defaults to https://graph.microsoft.com)
*
* @param string $baseUrl The URL to call
*
* @return Graph object
*/
public function setBaseUrl($baseUrl)
{
$this->_baseUrl = $baseUrl;
return $this;
}

/**
* Sets the API version to use, e.g. "beta" (defaults to v1.0)
*
* @param string $apiVersion The API version to use
*
* @return Graph object
*/
public function setApiVersion($apiVersion)
* Graph constructor.
*
* Creates a Graph client object used to make requests to the Graph API
* Setting $httpClient to null will create a default Guzzle client.
* @param string $apiVersion
* @param string $nationalCloud
* @param HttpClientInterface|null $httpClient
* @throws ClientInitialisationException
*/
public function __construct(string $apiVersion = GraphConstants::API_VERSION,
string $nationalCloud = NationalCloud::GLOBAL,
HttpClientInterface $httpClient = null)
{
if (!$apiVersion) {
throw new \InvalidArgumentException("Api version string cannot be empty");
}
if (!in_array($nationalCloud, NationalCloud::getValues())) {
throw new \InvalidArgumentException("Invalid national cloud passed. See NationalCloud constants");
}
$this->_apiVersion = $apiVersion;
return $this;
$this->_nationalCloud = $nationalCloud;
$this->_httpClient = ($httpClient) ?: HttpClientFactory::nationalCloud($nationalCloud)::createAdapter();
}

/**
Expand All @@ -117,24 +104,6 @@ public function setAccessToken($accessToken)
return $this;
}

/**
* Sets the proxy port. This allows you
* to use tools such as Fiddler to view
* requests and responses made with Guzzle
*
* @param string port The port number to use
* @param bool $verifySSL Whether SSL verification should be enabled
*
* @return Graph object
*/
public function setProxyPort($port, $verifySSL = false)
{
$this->_proxyPort = $port;
$this->_proxyVerifySSL = $verifySSL;

return $this;
}

/**
* Creates a new request object with the given Graph information
*
Expand All @@ -151,10 +120,9 @@ public function createRequest($requestType, $endpoint)
$requestType,
$endpoint,
$this->_accessToken,
$this->_baseUrl,
$this->_nationalCloud,
$this->_apiVersion,
$this->_proxyPort,
$this->_proxyVerifySSL
$this->_httpClient
);
}

Expand All @@ -175,10 +143,9 @@ public function createCollectionRequest($requestType, $endpoint)
$requestType,
$endpoint,
$this->_accessToken,
$this->_baseUrl,
$this->_nationalCloud,
$this->_apiVersion,
$this->_proxyPort,
$this->_proxyVerifySSL
$this->_httpClient
);
}
}
Loading