Skip to content

Commit 28c0147

Browse files
committed
Add documentation to set user-agent for all requests (and also a test)
1 parent 94c1f6b commit 28c0147

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ The library provides a single client and multiple request classes to send reques
2323

2424
// Retrieve user with uid 314031.
2525
$client = new Client();
26-
$user_request = new UserRequest(314031);
26+
$user_request = new UserRequest('314031');
2727
$user = $client->getEntity($user_request);
2828

2929
There are various request classes to retrieve different types of entities and entity listings. Many of the entity request classes have a corresponding list request class as well, e.g. `CommentRequest` and `CommentCollectionRequest`.
30+
31+
### User Agent
32+
33+
In accordance with responsible usage of Drupal.org API, it is important to set the user-agent header to indicate your application. You may set this request once globally using the static property on the `Request` class.
34+
35+
Request::$userAgent = 'Drupal Statistics Collector';
36+
37+
You have to do this only once as this user-agent is applied to all child requests as well. See the test in `\Hussainweb\DrupalApi\Tests\Request\RequestTest::testRequestUserAgent` for verifying the behaviour.

tests/Request/RequestTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Hussainweb\DrupalApi\Tests\Request;
44

55
use Hussainweb\DrupalApi\Request\Request;
6+
use Hussainweb\DrupalApi\Request\UserRequest;
67

78
class RequestTest extends \PHPUnit_Framework_TestCase
89
{
@@ -29,7 +30,9 @@ public function testRequestWithHeaders()
2930
public function testRequestUserAgent()
3031
{
3132
Request::$userAgent = 'Drupal.org API client Test Suite';
32-
$req = new Request("test.json");
33+
// Test with a child request class to ensure the user-agent is used
34+
// globally.
35+
$req = new UserRequest('314031');
3336
$this->assertEquals('Drupal.org API client Test Suite', $req->getHeaderLine('User-Agent'));
3437
}
3538
}

0 commit comments

Comments
 (0)