Skip to content

Commit cdbd778

Browse files
author
Adam Soos
committed
ws-3314: update headers and user agent
1 parent 54bc788 commit cdbd778

2 files changed

Lines changed: 19 additions & 7 deletions

File tree

source/rosette/api/Api.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ public function __construct($user_key, $service_url = 'https://analytics.babelst
144144
'Content-Type' => 'application/json',
145145
'Accept-Encoding' => 'gzip',
146146
'User-Agent' => $this->getUserAgent(),
147+
'X-BabelStreetAPI-Binding' => 'php',
148+
'X-BabelStreetAPI-Binding-Version' => $this->getBindingVersion(),
149+
// TODO remove this in future release
147150
'X-RosetteApi-Binding' => 'php',
148151
'X-RosetteAPI-Binding-Version' => $this->getBindingVersion());
149152

@@ -170,7 +173,7 @@ public function getBindingVersion()
170173
*/
171174
public function getUserAgent()
172175
{
173-
return 'RosetteAPIPHP/' . $this->getBindingVersion() . '/' . phpversion();
176+
return 'Babel-Street-Analytics-API-PHP/' . $this->getBindingVersion() . '/' . phpversion();
174177
}
175178

176179
/**
@@ -379,10 +382,12 @@ public function getCustomHeaders()
379382
*/
380383
public function setCustomHeader($header, $value = null)
381384
{
382-
$headerPrefix = 'x-rosetteapi-';
383-
if (strlen($header) < strlen($headerPrefix) ||
384-
strcasecmp(substr($header, 0, strlen($headerPrefix)), $headerPrefix) != 0) {
385-
throw new RosetteException("Custom headers must start with \"$headerPrefix\"");
385+
$legacyHeaderPrefix = 'x-rosetteapi-';
386+
$headerPrefix = 'x-babelstreetapi-';
387+
if (strlen($header) < strlen($legacyHeaderPrefix) ||
388+
(strcasecmp(substr($header, 0, strlen($headerPrefix)), $headerPrefix) != 0 &&
389+
strcasecmp(substr($header, 0, strlen($legacyHeaderPrefix)), $legacyHeaderPrefix) != 0)) {
390+
throw new RosetteException("Custom headers must start with \"$headerPrefix\" or \"$legacyHeaderPrefix\"");
386391
}
387392
if (is_null($value) && array_key_exists($header, $this->customHeaders)) {
388393
unsset($this->customHeaders, $header);

spec/rosette/api/ApiSpec.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function it_sets_gets_response_code()
3434
public function it_constructs_user_agent()
3535
{
3636
$version = $this->getWrappedObject()->getBindingVersion();
37-
$uaString = 'RosetteAPIPHP/' . $version . '/' . phpversion();
37+
$uaString = 'Babel-Street-Analytics-API-PHP/' . $version . '/' . phpversion();
3838
$this->getUserAgent()->shouldBe($uaString);
3939
}
4040
public function it_sets_gets_clears_url_params()
@@ -333,13 +333,20 @@ public function it_fails_with_incorrectly_formatted_custom_header($params, $requ
333333
{
334334
$this->shouldThrow(RosetteConstants::$RosetteExceptionFullClassName)->duringSetCustomHeader("test");
335335
}
336-
public function it_sets_gets_clears_customHeaders()
336+
public function it_sets_gets_clears_customHeaders_rosette()
337337
{
338338
$this->setCustomHeader('X-RosetteAPI-test', 'true');
339339
$this->getCustomHeaders()->shouldBe(array('X-RosetteAPI-test' => 'true'));
340340
$this->clearCustomHeaders();
341341
$this->getCustomHeaders()->shouldBe(array());
342342
}
343+
public function it_sets_gets_clears_customHeaders_babelstreet()
344+
{
345+
$this->setCustomHeader('X-BabelStreetAPI-test', 'true');
346+
$this->getCustomHeaders()->shouldBe(array('X-BabelStreetAPI-test' => 'true'));
347+
$this->clearCustomHeaders();
348+
$this->getCustomHeaders()->shouldBe(array());
349+
}
343350
public function it_fails_with_409_response($params, $request)
344351
{
345352
$params->beADoubleOf(ApiSpec::$DocumentParametersFullClassName);

0 commit comments

Comments
 (0)