Skip to content

Commit bd1f136

Browse files
authored
Merge pull request #8 from journy-io/user-agent-version
Add user-agent to header with sdk version
2 parents 428522f + ff54d70 commit bd1f136

File tree

3 files changed

+74
-48
lines changed

3 files changed

+74
-48
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
],
1919
"require": {
2020
"php": "^7.0 || ^8.0",
21+
"composer-runtime-api": "^2.1",
2122
"psr/http-client": "^1.0",
2223
"psr/http-factory": "^1.0",
2324
"psr/http-message": "^1.0"

src/Client.php

Lines changed: 71 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ final class Client
2222
private $streamFactory;
2323
private $apiKey;
2424
private $rootUrl;
25+
private $version;
2526

2627
public function __construct(
2728
ClientInterface $http,
@@ -46,6 +47,7 @@ public function __construct(
4647
$this->streamFactory = $streamFactory;
4748
$this->apiKey = $config["apiKey"];
4849
$this->rootUrl = $config["rootUrl"] ?? "https://api.journy.io";
50+
$this->version = \Composer\InstalledVersions::getVersion('journy-io/sdk');
4951
}
5052

5153
public static function withDefaults(string $apiKey): Client
@@ -56,6 +58,11 @@ public static function withDefaults(string $apiKey): Client
5658
return new Client($http, $factory, $factory, ["apiKey" => $apiKey]);
5759
}
5860

61+
private function withUserAgent(RequestInterface $request): RequestInterface
62+
{
63+
return $request->withAddedHeader("user-agent", "php-sdk/" . $this->version);
64+
}
65+
5966
private function withAuthentication(RequestInterface $request): RequestInterface
6067
{
6168
return $request->withAddedHeader("x-api-key", $this->apiKey);
@@ -125,9 +132,11 @@ public function getApiKeyDetails(): CallResult
125132
{
126133
$response = $this->http->sendRequest(
127134
$this->withAuthentication(
128-
$this->requestFactory->createRequest(
129-
"GET",
130-
new Uri("{$this->rootUrl}/validate")
135+
$this->withUserAgent(
136+
$this->requestFactory->createRequest(
137+
"GET",
138+
new Uri("{$this->rootUrl}/validate")
139+
)
131140
)
132141
)
133142
);
@@ -171,9 +180,11 @@ public function getTrackingSnippet(string $domain): CallResult
171180
$encodedDomain = urlencode($domain);
172181
$response = $this->http->sendRequest(
173182
$this->withAuthentication(
174-
$this->requestFactory->createRequest(
175-
"GET",
176-
new Uri("{$this->rootUrl}/tracking/snippet?domain={$encodedDomain}")
183+
$this->withUserAgent(
184+
$this->requestFactory->createRequest(
185+
"GET",
186+
new Uri("{$this->rootUrl}/tracking/snippet?domain={$encodedDomain}")
187+
)
177188
)
178189
)
179190
);
@@ -287,13 +298,15 @@ public function addEvent(Event $event): CallResult
287298

288299
$response = $this->http->sendRequest(
289300
$this->withAuthentication(
290-
$this->requestFactory
291-
->createRequest(
292-
"POST",
293-
new Uri("{$this->rootUrl}/events")
294-
)
295-
->withHeader("content-type", "application/json")
296-
->withBody($body)
301+
$this->withUserAgent(
302+
$this->requestFactory
303+
->createRequest(
304+
"POST",
305+
new Uri("{$this->rootUrl}/events")
306+
)
307+
->withHeader("content-type", "application/json")
308+
->withBody($body)
309+
)
297310
)
298311
);
299312

@@ -347,13 +360,15 @@ public function link(array $arguments): CallResult
347360

348361
$response = $this->http->sendRequest(
349362
$this->withAuthentication(
350-
$this->requestFactory
351-
->createRequest(
352-
"POST",
353-
new Uri("{$this->rootUrl}/link")
354-
)
355-
->withHeader("content-type", "application/json")
356-
->withBody($body)
363+
$this->withUserAgent(
364+
$this->requestFactory
365+
->createRequest(
366+
"POST",
367+
new Uri("{$this->rootUrl}/link")
368+
)
369+
->withHeader("content-type", "application/json")
370+
->withBody($body)
371+
)
357372
)
358373
);
359374

@@ -455,13 +470,15 @@ public function upsertUser(array $user): CallResult
455470

456471
$response = $this->http->sendRequest(
457472
$this->withAuthentication(
458-
$this->requestFactory
459-
->createRequest(
460-
"POST",
461-
new Uri("{$this->rootUrl}/users/upsert")
462-
)
463-
->withHeader("content-type", "application/json")
464-
->withBody($body)
473+
$this->withUserAgent(
474+
$this->requestFactory
475+
->createRequest(
476+
"POST",
477+
new Uri("{$this->rootUrl}/users/upsert")
478+
)
479+
->withHeader("content-type", "application/json")
480+
->withBody($body)
481+
)
465482
)
466483
);
467484

@@ -514,13 +531,15 @@ public function upsertAccount(array $account): CallResult
514531

515532
$response = $this->http->sendRequest(
516533
$this->withAuthentication(
517-
$this->requestFactory
518-
->createRequest(
519-
"POST",
520-
new Uri("{$this->rootUrl}/accounts/upsert")
521-
)
522-
->withHeader("content-type", "application/json")
523-
->withBody($body)
534+
$this->withUserAgent(
535+
$this->requestFactory
536+
->createRequest(
537+
"POST",
538+
new Uri("{$this->rootUrl}/accounts/upsert")
539+
)
540+
->withHeader("content-type", "application/json")
541+
->withBody($body)
542+
)
524543
)
525544
);
526545

@@ -587,13 +606,15 @@ function (array $user) {
587606

588607
$response = $this->http->sendRequest(
589608
$this->withAuthentication(
590-
$this->requestFactory
591-
->createRequest(
592-
"POST",
593-
new Uri("{$this->rootUrl}/accounts/users/add")
594-
)
595-
->withHeader("content-type", "application/json")
596-
->withBody($body)
609+
$this->withUserAgent(
610+
$this->requestFactory
611+
->createRequest(
612+
"POST",
613+
new Uri("{$this->rootUrl}/accounts/users/add")
614+
)
615+
->withHeader("content-type", "application/json")
616+
->withBody($body)
617+
)
597618
)
598619
);
599620

@@ -660,13 +681,15 @@ function (array $user) {
660681

661682
$response = $this->http->sendRequest(
662683
$this->withAuthentication(
663-
$this->requestFactory
664-
->createRequest(
665-
"POST",
666-
new Uri("{$this->rootUrl}/accounts/users/remove")
667-
)
668-
->withHeader("content-type", "application/json")
669-
->withBody($body)
684+
$this->withUserAgent(
685+
$this->requestFactory
686+
->createRequest(
687+
"POST",
688+
new Uri("{$this->rootUrl}/accounts/users/remove")
689+
)
690+
->withHeader("content-type", "application/json")
691+
->withBody($body)
692+
)
670693
)
671694
);
672695

tests/ClientTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,8 @@ public function test_it_upserts_account()
267267
],
268268
$payload
269269
);
270+
$this->assertNotEmpty($request->getHeader("user-agent"));
271+
$this->assertEquals("php-sdk", explode("/", $request->getHeader("user-agent")[0])[0]);
270272
}
271273
}
272274

0 commit comments

Comments
 (0)