Skip to content

Commit 2216362

Browse files
authored
Merge pull request #19 from magiclabs/dev-brian
Add additional parameters to HttpClient user-agent
2 parents 85cef52 + bc47421 commit 2216362

File tree

9 files changed

+54
-2
lines changed

9 files changed

+54
-2
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ composer.lock
1515

1616
# Ignore PHPUnit coverage file
1717
clover.xml
18+
.phpunit.result.cache
1819

1920
# Ignore IDE's configuration files
2021
.idea

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
- <PR-#ISSUE> ...
1414

15+
## `0.3.0` - 2/17/2023
16+
17+
#### Added
18+
19+
- PR-#19: Add additional parameters to HttpClient user-agent
20+
1521
## `0.2.0` - 11/29/2022
1622

1723
#### Added

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.0
1+
0.3.0

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"simplito/elliptic-php": "^1.0"
2727
},
2828
"require-dev": {
29-
"phpunit/phpunit": "^7.3"
29+
"phpunit/phpunit": "^8.0"
3030
},
3131
"autoload": {
3232
"psr-4": {

lib/HttpClient.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class HttpClient
2222
public $_backoff_factor;
2323
public $_base_url;
2424
public $_api_secret_key;
25+
public $_platform = 'php';
2526
public $ch;
2627

2728
public function __construct($api_secret_key, $timeout, $retries, $backoff_factor)
@@ -68,6 +69,11 @@ public function _get_user_agent()
6869
$user_agent[] = 'sdk_version: ' . $this->get_version();
6970
$user_agent[] = 'publisher: Magic Labs Inc.';
7071
$user_agent[] = 'http_lib: magic-admin-php';
72+
$user_agent[] = 'platform: '.$this->_platform;
73+
74+
if (isset($_SERVER['SERVER_NAME'])) {
75+
$user_agent[] = 'server_name: '.$_SERVER['SERVER_NAME'];
76+
}
7177

7278
return $user_agent;
7379
}
@@ -271,4 +277,9 @@ public function get_version()
271277
{
272278
return \file_get_contents(MAGIC_ADMIN_PHP_PATH . '/VERSION');
273279
}
280+
281+
public function _set_platform($platform)
282+
{
283+
$this->_platform = $platform;
284+
}
274285
}

lib/Magic.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,9 @@ public function __construct(
2828
$backoff_factor
2929
);
3030
}
31+
32+
public function _set_platform($platform)
33+
{
34+
$this->user->_set_platform($platform);
35+
}
3136
}

lib/Resource/User.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,9 @@ public function logout_by_token($did_token)
6565
{
6666
return $this->logout_by_issuer($this->token->get_issuer($did_token));
6767
}
68+
69+
public function _set_platform($platform)
70+
{
71+
$this->request_client->_set_platform($platform);
72+
}
6873
}

phpunit.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<phpunit
4+
bootstrap="vendor/autoload.php"
5+
colors="true"
6+
stopOnFailure="true"
7+
>
8+
<testsuites>
9+
<testsuite name="Test Suite">
10+
<directory>tests</directory>
11+
</testsuite>
12+
</testsuites>
13+
14+
<php>
15+
<server name="SERVER_NAME" value="local_phpunit"/>
16+
</php>
17+
</phpunit>

tests/HttpClientTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,16 @@ protected function setUp(): void
1717
$retries = 3;
1818
$backoff_factor = 0.02;
1919
$api_secret_key = 'magic_admin';
20+
$platform = 'test_platform';
2021

2122
$this->requestsClient = new \MagicAdmin\HttpClient(
2223
$api_secret_key,
2324
$timeout,
2425
$retries,
2526
$backoff_factor
2627
);
28+
29+
$this->requestsClient->_set_platform($platform);
2730
}
2831

2932
public function testRetrieves()
@@ -54,6 +57,8 @@ public function testGetUserAgent()
5457
$expected_array[] = 'sdk_version: ' . $this->requestsClient->get_version();
5558
$expected_array[] = 'publisher: Magic Labs Inc.';
5659
$expected_array[] = 'http_lib: magic-admin-php';
60+
$expected_array[] = 'platform: test_platform';
61+
$expected_array[] = 'server_name: local_phpunit';
5762

5863
static::assertSame($this->requestsClient->_get_user_agent(), $expected_array);
5964
}
@@ -69,6 +74,8 @@ public function testGetRequestHeaders()
6974
'sdk_version: ' . $this->requestsClient->get_version(),
7075
'publisher: Magic Labs Inc.',
7176
'http_lib: magic-admin-php',
77+
'platform: test_platform',
78+
'server_name: local_phpunit',
7279
]
7380
);
7481
$expected_headers = [];

0 commit comments

Comments
 (0)