Skip to content

Commit

Permalink
custom user agent
Browse files Browse the repository at this point in the history
  • Loading branch information
justincy committed Nov 10, 2016
1 parent 9c10b99 commit a1333f0
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ $fs = new FamilySearch([
'maxThrottledRetries' => 5,

// Activate pending modifications
'pendingModifications' => ['consolidate-redundant-resources', 'current-person-401']
'pendingModifications' => ['consolidate-redundant-resources', 'current-person-401'],

// Modify the default user agent by appending this value
'userAgent' => 'myApp/1.2.3'
]);

// OAuth step 1: Redirect
Expand Down
26 changes: 26 additions & 0 deletions src/FamilySearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
class FamilySearch
{

const VERSION = '1.2.0';

/**
* The FamilySearch reference or environment to target. Valid values are
* 'integration', 'beta', and 'production'.
Expand Down Expand Up @@ -65,6 +67,13 @@ class FamilySearch
*/
private $pendingModifications;

/**
* User agent
*
* @var string
*/
private $userAgent;

/**
* Construct a new FamilySearch Client
*
Expand Down Expand Up @@ -96,6 +105,11 @@ public function __construct($options = array())
$this->pendingModifications = implode(',', $options['pendingModifications']);
}

$this->userAgent = self::defaultUseragent();
if (isset($options['userAgent'])) {
$this->userAgent .= ' ' . $options['userAgent'];
}

// Load the access token from the session first so that it can be
// overwritten by the accessToken option
if ($this->sessions && isset($_SESSION[$this->sessionVariable])) {
Expand Down Expand Up @@ -321,6 +335,8 @@ private function request($url, $options = array())
$options['headers']['X-FS-Feature-Tag'] = $this->pendingModifications;
}

$options['headers']['User-Agent'] = $this->userAgent;

// Set the body
$body = null;
if ($options['body'] && ($options['method'] === 'POST' || $options['method'] === 'PUT')) {
Expand Down Expand Up @@ -522,4 +538,14 @@ private function platformHost()
}
}

/**
* Calculate the default user agent
*
* @return string
*/
private static function defaultUseragent()
{
return 'FS-PHP-Lite/' . self::VERSION . ' curl/' . \curl_version()['version'] . ' PHP/' . PHP_VERSION;
}

}
19 changes: 19 additions & 0 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,23 @@ public function testPendingModification()
$this->assertTrue($response->redirected);
}

/**
* @vcr testUserAgent.json
*/
public function testUserAgent()
{
$this->client = new \FamilySearch([
'appKey' => SandboxCredentials::API_KEY,
'userAgent' => 'myApp/1.2.3'
]);
$this->assertResponseOK($this->login());
$response = $this->client->get('https://httpbin.org/user-agent');
$this->assertResponseOK($response);
$this->assertResponseData($response);
$this->assertTrue(strpos($response->requestHeaders['User-Agent'], 'FS-PHP-Lite') === 0);
$this->assertTrue(strpos($response->requestHeaders['User-Agent'], 'curl') !== false);
$this->assertTrue(strpos($response->requestHeaders['User-Agent'], 'PHP') !== false);
$this->assertTrue(strpos($response->requestHeaders['User-Agent'], 'myApp/1.2.3') !== false);
}

}
59 changes: 59 additions & 0 deletions test/fixtures/testUserAgent.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
[{
"request": {
"method": "POST",
"url": "https:\/\/integration.familysearch.org\/cis-web\/oauth2\/v3\/token",
"headers": {
"Host": "integration.familysearch.org",
"Content-Type": "application\/x-www-form-urlencoded",
"User-Agent": "FS-PHP-Lite\/1.1.0 curl\/7.35.0 PHP\/5.5.9-1ubuntu4.17 myApp\/1.2.3"
},
"body": "grant_type=password&client_id=a02j000000CBv4gAAD&username=sdktester&password=1234sdkpass"
},
"response": {
"status": {
"http_version": "1.1",
"code": "200",
"message": "OK"
},
"headers": {
"Server": "Apache-Coyote\/1.1",
"Expires": "Tue, 03 Jul 2001 06:00:00 GMT",
"Last-Modified": "Thu Nov 10 21:37:57 GMT+00:00 2016",
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0",
"Pragma": "no-cache",
"Content-Type": "application\/json;charset=ISO-8859-1",
"Content-Language": "en-US",
"Content-Length": "185",
"Date": "Thu, 10 Nov 2016 21:37:56 GMT",
"Connection": "close"
},
"body": "{\"token\":\"USYS4577D98D73A5A3536EDFEBA47D962F30_idses-int02.a.fsglobal.net\",\"token_type\":\"family_search\",\"access_token\":\"USYS4577D98D73A5A3536EDFEBA47D962F30_idses-int02.a.fsglobal.net\"}"
}
},{
"request": {
"method": "GET",
"url": "https:\/\/httpbin.org\/user-agent",
"headers": {
"Host": "httpbin.org",
"Authorization": "Bearer USYS4577D98D73A5A3536EDFEBA47D962F30_idses-int02.a.fsglobal.net",
"User-Agent": "FS-PHP-Lite\/1.1.0 curl\/7.35.0 PHP\/5.5.9-1ubuntu4.17 myApp\/1.2.3"
}
},
"response": {
"status": {
"http_version": "1.1",
"code": "200",
"message": "OK"
},
"headers": {
"Server": "nginx",
"Date": "Thu, 10 Nov 2016 21:36:56 GMT",
"Content-Type": "application\/json",
"Content-Length": "86",
"Connection": "keep-alive",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Credentials": "true"
},
"body": "{\n \"user-agent\": \"FS-PHP-Lite\/1.1.0 curl\/7.35.0 PHP\/5.5.9-1ubuntu4.17 myApp\/1.2.3\"\n}\n"
}
}]

0 comments on commit a1333f0

Please sign in to comment.