Skip to content

Commit

Permalink
Merge pull request #88 from microsoft/dev
Browse files Browse the repository at this point in the history
Release 0.9.0
  • Loading branch information
Ndiritu authored Oct 31, 2023
2 parents ed77781 + 39c7303 commit 90b8ba9
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 36 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr-validation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
php-versions: ['7.4', '8.0', '8.1', '8.2']
steps:
- name: Checkout
uses: actions/[email protected].0
uses: actions/[email protected].1
- name: Setup PHP and Xdebug for Code Coverage report
uses: shivammathur/setup-php@v2
with:
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [0.9.0] - 2023-10-30

### Added
- Adds generics to Promise types in PHPDocs

## [0.8.4] - 2023-10-11

### Added
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"require": {
"php": "^7.4 | ^8.0",
"guzzlehttp/guzzle": "^7.0",
"microsoft/kiota-abstractions": "^0.8.0",
"microsoft/kiota-abstractions": "^0.9.0",
"ext-zlib": "*",
"ext-json": "*"
},
Expand Down
2 changes: 1 addition & 1 deletion src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@
final class Constants
{
/** @var string The current version for this Library */
public const KIOTA_HTTP_CLIENT_VERSION = '0.8.4';
public const KIOTA_HTTP_CLIENT_VERSION = '0.9.0';
}
67 changes: 51 additions & 16 deletions src/GuzzleRequestAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Exception;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Request;
use Http\Promise\FulfilledPromise;
use Http\Promise\Promise;
use InvalidArgumentException;
use Microsoft\Kiota\Abstractions\ApiClientBuilder;
Expand Down Expand Up @@ -46,6 +45,7 @@
use Psr\Http\Message\StreamInterface;
use RuntimeException;
use Throwable;
use UnexpectedValueException;

/**
* Class GuzzleRequestAdapter
Expand Down Expand Up @@ -107,22 +107,22 @@ public function __construct(AuthenticationProvider $authenticationProvider,
}

/**
* @template T of Parsable
* @param RequestInformation $requestInfo
* @param ResponseInterface $result
* @param array<string,array{string,string}>|null $errorMappings
* @param array<string, array{class-string<T>, string}>|null $errorMappings
* @param SpanInterface $span
* @return Promise|null
* @return Promise<mixed>|null
*/
private function tryHandleResponse(RequestInformation $requestInfo,
ResponseInterface $result,
?array $errorMappings,
SpanInterface $span): ?Promise
{
$responseHandlerOption = $requestInfo->getRequestOptions()[ResponseHandlerOption::class] ?? null;
if ($responseHandlerOption && is_a($responseHandlerOption, ResponseHandlerOption::class)) {
if ($responseHandlerOption && $responseHandlerOption instanceof ResponseHandlerOption) {
$responseHandler = $responseHandlerOption->getResponseHandler();
$span->addEvent('Event - com.microsoft.kiota.response_handler_invoked?');
/** @phpstan-ignore-next-line False alarm?*/
return $responseHandler->handleResponseAsync($result, $errorMappings);
}
return null;
Expand All @@ -140,11 +140,17 @@ public function sendAsync(RequestInformation $requestInfo, array $targetCallable
$responseMessage = $this->getHttpResponseMessage($requestInfo, '', $span);
$finalResponse = $responseMessage->then(
function (ResponseInterface $result) use ($targetCallable, $requestInfo, $errorMappings, &$span) {
$response = $this->tryHandleResponse($requestInfo, $result, $errorMappings, $span);
$this->setHttpResponseAttributesInSpan($span, $result);
$response = $this->tryHandleResponse($requestInfo, $result, $errorMappings, $span);
if ($response !== null) {
$span->addEvent(self::EVENT_RESPONSE_HANDLER_INVOKED_KEY);
return $response;
$customResponse = $response->wait();
if ($customResponse instanceof $targetCallable[0] || is_null($customResponse)) {
return $customResponse;
}
throw new UnexpectedValueException(
"Custom response handler failed to return object of expected type {$targetCallable[0]}|null"
);
}

$this->throwFailedResponse($result, $errorMappings, $span);
Expand Down Expand Up @@ -196,11 +202,28 @@ function (ResponseInterface $result) use ($targetCallable, $requestInfo, $errorM

if ($response !== null) {
$span->addEvent(self::EVENT_RESPONSE_HANDLER_INVOKED_KEY);
return $result;
$customResponse = $response->wait();
if (is_array($customResponse)) {
foreach ($customResponse as $item) {
if (!is_null($item) && !$item instanceof $targetCallable[0]) {
throw new UnexpectedValueException(
"Custom response handler returned array containing invalid type.
Expected type: {$targetCallable[0]}|null"
);
}
}
return $customResponse;
}
elseif (is_null($customResponse)) {
return $customResponse;
}
throw new UnexpectedValueException(
"Custom response handler failed to return array of expected type:{$targetCallable[0]}|null"
);
}
$this->throwFailedResponse($result, $errorMappings, $span);
if ($this->is204NoContentResponse($result)) {
return new FulfilledPromise(null);
return null;
}
$rootNode = $this->getRootParseNode($result, $span);
$spanForDeserialization = $this->tracer->spanBuilder('ParseNode.getCollectionOfObjectValues')
Expand All @@ -215,7 +238,7 @@ function (ResponseInterface $result) use ($targetCallable, $requestInfo, $errorM
$scope->detach();
$span->end();
}
return $finalResponse;
return $finalResponse; /** @phpstan-ignore-line */
}

/**
Expand All @@ -232,7 +255,7 @@ function (ResponseInterface $result) use ($primitiveType, $requestInfo, $errorMa
$response = $this->tryHandleResponse($requestInfo, $result, $errorMappings, $span);

if ($response !== null) {
return $result;
return $response->wait();
}
$this->throwFailedResponse($result, $errorMappings, $span);
$this->setResponseType($primitiveType, $span);
Expand Down Expand Up @@ -294,7 +317,13 @@ function (ResponseInterface $result) use ($primitiveType, $requestInfo, $errorMa
$response = $this->tryHandleResponse($requestInfo, $result, $errorMappings, $span);

if ($response !== null) {
return $result;
$customResponse = $response->wait();
if (is_array($customResponse) || is_null($customResponse)) {
return $customResponse;
}
throw new UnexpectedValueException(
"Custom response handler failed to return array of expected type: {$primitiveType}"
);
}
$this->throwFailedResponse($result, $errorMappings, $span);
if ($this->is204NoContentResponse($result)) {
Expand All @@ -321,10 +350,16 @@ public function sendNoContentAsync(RequestInformation $requestInfo, ?array $erro
$finalResponse = $this->getHttpResponseMessage($requestInfo, '', $span)->then(
function (ResponseInterface $result) use ($requestInfo, $errorMappings, &$span) {
$this->setHttpResponseAttributesInSpan($span, $result);
$response = $this->tryHandleResponse($requestInfo, $result, $errorMappings, $span);
$response = $this->tryHandleResponse($requestInfo, $result,$errorMappings, $span);

if ($response !== null) {
return $result;
$customResponse = $response->wait();
if (is_null($customResponse)) {
return $customResponse;
}
throw new UnexpectedValueException(
"Custom response handler failed to return value of expected return type: null"
);
}
$this->throwFailedResponse($result, $errorMappings, $span);
return null;
Expand Down Expand Up @@ -409,7 +444,7 @@ public function getPsrRequestFromRequestInformation(RequestInformation $requestI
* Converts RequestInformation object to an authenticated(containing auth header) PSR-7 Request Object.
*
* @param RequestInformation $requestInformationInformation
* @return Promise
* @return Promise<RequestInterface>
*/
public function convertToNative(RequestInformation $requestInformationInformation): Promise
{
Expand Down Expand Up @@ -461,7 +496,7 @@ private function getRootParseNode(ResponseInterface $response, SpanInterface $sp
* @param RequestInformation $requestInfo
* @param string $claims additional claims to request if CAE fails
* @param SpanInterface $span
* @return Promise
* @return Promise<ResponseInterface>
*/
private function getHttpResponseMessage(RequestInformation $requestInfo,
string $claims,
Expand Down
Loading

0 comments on commit 90b8ba9

Please sign in to comment.