Skip to content

Commit e4be8aa

Browse files
committed
SearchKit updated.
1 parent d2e2516 commit e4be8aa

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

src/SearchKit/SearchKit.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
<?php
22
namespace HMS\SearchKit;
33

4+
use HMS\AccountKit\AccountKit;
45
use HMS\Core\Wrapper;
56
use InvalidArgumentException;
67
use stdClass;
78

89
/**
910
* Class HMS SearchKit Wrapper
1011
*
12+
* Note: This API requires an "App-level client ID" & "Enable app-level client API".
13+
*
14+
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/app-gallery-connect-0000001057712438">Configuring App Information in AppGallery Connect</a>
1115
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-References/overview-0000001057087079">SearchKit</a>
12-
* @see <a href="https://developer.huawei.com/consumer/en/doc/development/HMSCore-Guides/open-platform-oauth-0000001053629189#section1022911426469">Authorization Code</a>
1316
* @author Martin Zeitler
1417
*/
1518
class SearchKit extends Wrapper {
@@ -47,11 +50,10 @@ class SearchKit extends Wrapper {
4750

4851
public function __construct( array|string $config ) {
4952
parent::__construct( $config );
50-
if (is_array($config) && isset($config['access_token'])) {
51-
$this->access_token = $config['access_token'];
52-
} else {
53-
throw new InvalidArgumentException('SearchKit requires an user access token.');
54-
}
53+
54+
/* Obtain an access-token. */
55+
$account_kit = new AccountKit( $config );
56+
$this->access_token = $account_kit->get_access_token();
5557

5658
// $this->base_url = Constants::SEARCH_KIT_BASE_URL;
5759
$this->base_url = Constants::SEARCH_KIT_BASE_URL_EU;
@@ -65,7 +67,7 @@ protected function post_init(): void {
6567
if (! in_array($this->base_url, $urls)) {
6668
throw new InvalidArgumentException('SearchKit permits these base_url values: '. implode(', ', $urls));
6769
}
68-
unset($this->oauth2_client_secret, $this->oauth2_api_scope, $this->oauth2_api_scope, $this->oauth2_redirect_url);
70+
unset($this->oauth2_client_secret, $this->oauth2_api_scope, $this->oauth2_redirect_url);
6971
unset($this->token_expiry, $this->refresh_token, $this->id_token, $this->package_name, $this->product_id);
7072
unset($this->agc_client_id, $this->agc_client_secret);
7173
unset($this->developer_id, $this->project_id);
@@ -79,7 +81,7 @@ protected function auth_header(): array {
7981
"Content-Type" => "application/json; charset=utf-8",
8082
"Authorization" => "Bearer $this->access_token",
8183
"X-Kit-AppID" => $this->oauth2_client_id,
82-
// "X-Kit-ClientIP" => $this->client_ip,
84+
"X-Kit-ClientIP" => $this->client_ip,
8385
"X-Kit-RequestID" => $this->request_id
8486
];
8587
}

tests/BaseTestCase.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ abstract class BaseTestCase extends TestCase {
3535
protected static ?string $agc_gateway = 'https://connect-drcn.dbankcloud.cn/';
3636
private const ENV_VAR_OAUTH2_CLIENT_ID = 'Variable HUAWEI_OAUTH2_CLIENT_ID is not set.';
3737
private const ENV_VAR_OAUTH2_CLIENT_SECRET = 'Variable HUAWEI_OAUTH2_CLIENT_SECRET is not set.';
38+
private const ENV_VAR_APP_LEVEL_CLIENT_ID = 'Variable HUAWEI_APP_LEVEL_CLIENT_ID is not set.'; // TODO
39+
private const ENV_VAR_APP_LEVEL_CLIENT_SECRET = 'Variable HUAWEI_APP_LEVEL_CLIENT_SECRET is not set.'; // TODO
3840
protected const ENV_VAR_CONNECT_API_CLIENT_ID = 'Variable HUAWEI_CONNECT_API_CLIENT_ID is not set.';
3941
protected const ENV_VAR_CONNECT_API_CLIENT_SECRET = 'Variable HUAWEI_CONNECT_API_CLIENT_SECRET is not set.';
4042
protected const ENV_VAR_CONNECT_PRODUCT_ID = 'Variable HUAWEI_CONNECT_PRODUCT_ID is not set.';
@@ -118,12 +120,12 @@ protected static function load_user_access_token() {
118120
file_put_contents(self::$oauth2_token_path, json_encode($token_response));
119121
}
120122
$remaining = $token_response->token_expiry - time();
121-
echo sprintf('The cached token expires in %02d:%02d:%02d.', ($remaining / 3600), ($remaining / 60 % 60), $remaining % 60);
123+
echo sprintf('The cached user access token expires in %02d:%02d:%02d.', ($remaining / 3600), ($remaining / 60 % 60), $remaining % 60);
122124
}
123125
}
124126
self::$user_access_token = $token_response->access_token;
125127
} else {
126-
self::markTestSkipped( "Cannot read cached token: " . self::$oauth2_token_path);
128+
self::markTestSkipped( "Cannot read cached user access token: " . self::$oauth2_token_path);
127129
}
128130
}
129131
protected static function format_filesize(int $bytes, int $decimals=2): string {

tests/SearchKitTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ class SearchKitTest extends BaseTestCase {
2121
/** This method is called before the first test of this test class is run. */
2222
public static function setUpBeforeClass(): void {
2323
parent::setUpBeforeClass();
24+
self::$oauth2_client_id = getenv('HUAWEI_APP_LEVEL_CLIENT_ID');
25+
self::$oauth2_client_secret = getenv('HUAWEI_APP_LEVEL_CLIENT_SECRET');
2426
self::$client = new SearchKit( array_merge(self::get_user_config(), [
2527
'oauth2_client_id' => self::$oauth2_client_id,
28+
'oauth2_client_secret' => self::$oauth2_client_secret,
2629
'debug_mode' => false
2730
] ));
2831
self::assertTrue( self::$client->is_ready(), self::CLIENT_NOT_READY );

0 commit comments

Comments
 (0)