Skip to content

Commit 11909fd

Browse files
committed
new Microsoft\Kiota\Http\Promise interface extending Http\Promise\Promise
1 parent 4008f38 commit 11909fd

19 files changed

+187
-22
lines changed

packages/abstractions/src/Authentication/AccessTokenProvider.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88

99
namespace Microsoft\Kiota\Abstractions\Authentication;
1010

11-
use Http\Promise\Promise;
11+
use Microsoft\Kiota\Http\Promise;
12+
use Microsoft\Kiota\Http\RejectedPromise;
1213

1314
/**
1415
* Interface AccessTokenProvider

packages/abstractions/src/Authentication/AnonymousAuthenticationProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Microsoft\Kiota\Abstractions\Authentication;
44

5-
use Http\Promise\FulfilledPromise;
6-
use Http\Promise\Promise;
5+
use Microsoft\Kiota\Http\FulfilledPromise;
6+
use Microsoft\Kiota\Http\Promise;
77
use Microsoft\Kiota\Abstractions\RequestInformation;
88

99
class AnonymousAuthenticationProvider implements AuthenticationProvider {

packages/abstractions/src/Authentication/AuthenticationProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Microsoft\Kiota\Abstractions\Authentication;
33

4-
use Http\Promise\Promise;
4+
use Microsoft\Kiota\Http\Promise;
55
use Microsoft\Kiota\Abstractions\RequestInformation;
66

77
/**

packages/abstractions/src/Authentication/BaseBearerTokenAuthenticationProvider.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Microsoft\Kiota\Abstractions\Authentication;
44

5-
use Http\Promise\FulfilledPromise;
6-
use Http\Promise\Promise;
5+
use Microsoft\Kiota\Http\FulfilledPromise;
6+
use Microsoft\Kiota\Http\Promise;
77
use Microsoft\Kiota\Abstractions\RequestInformation;
88

99
/**

packages/abstractions/src/NativeResponseHandler.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace Microsoft\Kiota\Abstractions;
44

5-
use Http\Promise\FulfilledPromise;
5+
use Microsoft\Kiota\Http\FulfilledPromise;
66
use Psr\Http\Message\ResponseInterface;
7-
use Http\Promise\Promise;
7+
use Microsoft\Kiota\Http\Promise;
88

99
/**
1010
* Default response handler that returns the PSR-7 Response

packages/abstractions/src/RequestAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Microsoft\Kiota\Abstractions;
33

4-
use Http\Promise\Promise;
4+
use Microsoft\Kiota\Http\Promise;
55
use Microsoft\Kiota\Abstractions\Serialization\ParseNodeFactory;
66
use Microsoft\Kiota\Abstractions\Serialization\SerializationWriterFactory;
77
use Microsoft\Kiota\Abstractions\Store\BackingStoreFactory;

packages/abstractions/src/ResponseHandler.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
namespace Microsoft\Kiota\Abstractions;
33

4-
use Http\Promise\Promise;
4+
use Microsoft\Kiota\Http\Promise;
55
use Psr\Http\Message\ResponseInterface;
66
use Microsoft\Kiota\Abstractions\Serialization\Parsable;
77

packages/abstractions/tests/Authentication/BaseBearerTokenAuthenticationProviderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Microsoft\Kiota\Abstractions\Tests\Authentication;
44

5-
use Http\Promise\FulfilledPromise;
5+
use Microsoft\Kiota\Http\FulfilledPromise;
66
use Microsoft\Kiota\Abstractions\Authentication\AccessTokenProvider;
77
use Microsoft\Kiota\Abstractions\Authentication\BaseBearerTokenAuthenticationProvider;
88
use Microsoft\Kiota\Abstractions\RequestInformation;

packages/abstractions/tests/TestFiles/TestRequestAdapter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Microsoft\Kiota\Abstractions\Tests\TestFiles;
44

5-
use Http\Promise\FulfilledPromise;
6-
use Http\Promise\Promise;
5+
use Microsoft\Kiota\Http\FulfilledPromise;
6+
use Microsoft\Kiota\Http\Promise;
77
use Microsoft\Kiota\Abstractions\RequestAdapter;
88
use Microsoft\Kiota\Abstractions\RequestInformation;
99
use Microsoft\Kiota\Abstractions\Serialization\ParseNodeFactory;

packages/authentication/phpleague/src/Oauth/CAEConfigurationTrait.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
namespace Microsoft\Kiota\Authentication\Oauth;
1010

11-
use Http\Promise\Promise;
11+
use Microsoft\Kiota\Http\Promise;
1212

1313
trait CAEConfigurationTrait
1414
{

packages/authentication/phpleague/src/Oauth/TokenRequestContext.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace Microsoft\Kiota\Authentication\Oauth;
1010

1111
use League\OAuth2\Client\Token\AccessToken;
12-
use Http\Promise\Promise;
12+
use Microsoft\Kiota\Http\Promise;
1313

1414
/**
1515
* Interface TokenRequestContext

packages/authentication/phpleague/src/PhpLeagueAccessTokenProvider.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111

1212
use Exception;
13-
use Http\Promise\FulfilledPromise;
14-
use Http\Promise\Promise;
15-
use Http\Promise\RejectedPromise;
13+
use Microsoft\Kiota\Http\FulfilledPromise;
14+
use Microsoft\Kiota\Http\Promise;
15+
use Microsoft\Kiota\Http\RejectedPromise;
1616
use InvalidArgumentException;
1717
use League\OAuth2\Client\Provider\AbstractProvider;
1818
use League\OAuth2\Client\Provider\Exception\IdentityProviderException;

packages/authentication/phpleague/tests/PhpLeagueAccessTokenProviderTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use GuzzleHttp\Handler\MockHandler;
88
use GuzzleHttp\Psr7\Request;
99
use GuzzleHttp\Psr7\Response;
10-
use Http\Promise\FulfilledPromise;
10+
use Microsoft\Kiota\Http\FulfilledPromise;
1111
use InvalidArgumentException;
1212
use League\OAuth2\Client\Token\AccessToken;
1313
use Microsoft\Kiota\Authentication\Cache\InMemoryAccessTokenCache;

packages/http/guzzle/src/GuzzleRequestAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Exception;
1515
use GuzzleHttp\ClientInterface;
1616
use GuzzleHttp\Psr7\Request;
17-
use Http\Promise\Promise;
17+
use Microsoft\Kiota\Http\Promise;
1818
use InvalidArgumentException;
1919
use Microsoft\Kiota\Abstractions\ApiClientBuilder;
2020
use Microsoft\Kiota\Abstractions\ApiException;

packages/http/guzzle/tests/GuzzleRequestAdapterTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
use GuzzleHttp\Psr7\Request;
88
use GuzzleHttp\Psr7\Response;
99
use GuzzleHttp\Psr7\Utils;
10-
use Http\Promise\FulfilledPromise;
11-
use Http\Promise\Promise;
10+
use Microsoft\Kiota\Http\FulfilledPromise;
11+
use Microsoft\Kiota\Http\Promise;
1212
use Microsoft\Kiota\Abstractions\ApiException;
1313
use Microsoft\Kiota\Abstractions\Authentication\AuthenticationProvider;
1414
use Microsoft\Kiota\Abstractions\Enum;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Microsoft\Kiota\Http;
5+
6+
/**
7+
* A promise already fulfilled.
8+
*
9+
* @author Joel Wurtz <[email protected]>
10+
*
11+
* @template-covariant T
12+
*
13+
* @implements Promise<T>
14+
*/
15+
final class FulfilledPromise implements Promise
16+
{
17+
/**
18+
* @var T
19+
*/
20+
private $result;
21+
22+
/**
23+
* @param T $result
24+
*/
25+
public function __construct($result)
26+
{
27+
$this->result = $result;
28+
}
29+
30+
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
31+
{
32+
if (null === $onFulfilled) {
33+
return $this;
34+
}
35+
36+
try {
37+
return new self($onFulfilled($this->result));
38+
} catch (\Exception $e) {
39+
return new RejectedPromise($e);
40+
}
41+
}
42+
43+
public function getState()
44+
{
45+
return Promise::FULFILLED;
46+
}
47+
48+
public function wait($unwrap = true)
49+
{
50+
if ($unwrap) {
51+
return $this->result;
52+
}
53+
54+
return NULL;
55+
}
56+
}

packages/http/promise/src/Promise.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Microsoft\Kiota\Http;
5+
6+
/**
7+
* Promise represents a value that may not be available yet, but will be resolved at some point in future.
8+
* It acts like a proxy to the actual value.
9+
*
10+
* This interface is an extension of the promises/a+ specification.
11+
*
12+
* @see https://promisesaplus.com/
13+
*
14+
* @author Joel Wurtz <[email protected]>
15+
* @author Márk Sági-Kazár <[email protected]>
16+
*
17+
* @template-covariant T
18+
*/
19+
interface Promise extends \Http\Promise\Promise
20+
{
21+
22+
/**
23+
* Adds behavior for when the promise is resolved or rejected (response will be available, or error happens).
24+
*
25+
* If you do not care about one of the cases, you can set the corresponding callable to null
26+
* The callback will be called when the value arrived and never more than once.
27+
*
28+
* @param callable(T): V|null $onFulfilled called when a response will be available
29+
* @param callable(\Exception): V|null $onRejected called when an exception occurs
30+
*
31+
* @return Promise<V> a new resolved promise with value of the executed callback (onFulfilled / onRejected)
32+
*
33+
* @template V
34+
*/
35+
public function then(?callable $onFulfilled = null, ?callable $onRejected = null);
36+
37+
/**
38+
* Wait for the promise to be fulfilled or rejected.
39+
*
40+
* When this method returns, the request has been resolved and if callables have been
41+
* specified, the appropriate one has terminated.
42+
*
43+
* When $unwrap is true (the default), the response is returned, or the exception thrown
44+
* on failure. Otherwise, nothing is returned or thrown.
45+
*
46+
* @param bool $unwrap Whether to return resolved value / throw reason or not
47+
*
48+
* @return ($unwrap is true ? T : null) Resolved value, null if $unwrap is set to false
49+
*
50+
* @throws \Exception the rejection reason if $unwrap is set to true and the request failed
51+
*/
52+
public function wait($unwrap = true);
53+
54+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Microsoft\Kiota\Http;
5+
6+
/**
7+
* A rejected promise.
8+
*
9+
* @author Joel Wurtz <[email protected]>
10+
*
11+
* @template-covariant T
12+
*
13+
* @implements Promise<T>
14+
*/
15+
final class RejectedPromise implements Promise
16+
{
17+
/**
18+
* @var \Exception
19+
*/
20+
private $exception;
21+
22+
public function __construct(\Exception $exception)
23+
{
24+
$this->exception = $exception;
25+
}
26+
27+
public function then(?callable $onFulfilled = null, ?callable $onRejected = null)
28+
{
29+
if (null === $onRejected) {
30+
return $this;
31+
}
32+
33+
try {
34+
return new FulfilledPromise($onRejected($this->exception));
35+
} catch (\Exception $e) {
36+
return new self($e);
37+
}
38+
}
39+
40+
public function getState()
41+
{
42+
return Promise::REJECTED;
43+
}
44+
45+
public function wait($unwrap = true)
46+
{
47+
if ($unwrap) {
48+
throw $this->exception;
49+
}
50+
51+
return NULL;
52+
}
53+
}

phpstan.neon

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ parameters:
66
- packages/abstractions/src
77
- packages/authentication/phpleague/src
88
- packages/http/guzzle/src
9+
- packages/http/promise/src
910
- packages/bundle/src
1011
- packages/serialization/json/src
1112
- packages/serialization/form/src

0 commit comments

Comments
 (0)