Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
ability to set siteUrl in httpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
znarf committed Jun 2, 2016
1 parent c63a1e9 commit 16915a9
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 4 deletions.
64 changes: 60 additions & 4 deletions src/Http/SimpleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,87 @@
class SimpleClient
{

/**
* @var string
*/
protected $apiKey;

/**
* @var string
*/
protected $siteUrl;

/**
* @var int
*/
protected $timeout = 2;

public function __construct($apiKey = null)
/**
* @var string
*/
protected $baseUserAgent = 'Bouncer Http';

/**
* Constructor.
*
* @param array|string $options
*/
public function __construct($options = array())
{
if ($apiKey) {
$this->setApiKey($apiKey);
// Compatibility with previous API
if (is_string($options)) {
$options = array('apiKey' => $options);
}

if (isset($options['apiKey']) && is_string($options['apiKey'])) {
$this->setApiKey($options['apiKey']);
}
if (isset($options['siteUrl']) && is_string($options['siteUrl'])) {
$this->setSiteUrl($options['siteUrl']);
}
}

/**
* @return SimpleClient
*/
public function setApiKey($apiKey)
{
$this->apiKey = $apiKey;

return $this;
}

/**
* @return SimpleClient
*/
public function setSiteUrl($siteUrl)
{
$this->siteUrl = $siteUrl;

return $this;
}

/**
* @return string
*/
public function getUserAgent()
{
if ($this->siteUrl) {
return "{$this->baseUserAgent}; {$this->siteUrl}";
}
else {
return $this->baseUserAgent;
}
}

public function request($method, $url, $data = null)
{
$userAgent = $this->getUserAgent();
$options = array(
'http' => array(
'timeout' => $this->timeout,
'method' => $method,
'header' => "User-Agent: Bouncer Http\r\n"
'header' => "User-Agent: {$userAgent}\r\n"
)
);
if ($this->apiKey) {
Expand Down
29 changes: 29 additions & 0 deletions tests/HttpClientTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

/*
* This file is part of the Bouncer package.
*
* (c) François Hodierne <francois@hodierne.net>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Bouncer;

class HttpClientTest extends \PHPUnit_Framework_TestCase
{

public function testGetUserAgent()
{
$options = array(
'apiKey' => 'b3bb90d61e80e96259bf354fd7cb03d7',
'siteUrl' => 'https://github.com/znarf/bouncer',
);

$client = new \Bouncer\Http\SimpleClient($options);

$this->assertEquals('Bouncer Http; https://github.com/znarf/bouncer', $client->getUserAgent());
}

}

0 comments on commit 16915a9

Please sign in to comment.