Skip to content

Commit

Permalink
Merge pull request #20 from bookboon/feature/override-uri
Browse files Browse the repository at this point in the history
Feature/override uri
  • Loading branch information
lkm authored Oct 30, 2018
2 parents 9a89504 + 0da5fa7 commit 65ba475
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 68 deletions.
32 changes: 22 additions & 10 deletions src/Bookboon.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,30 +41,42 @@ public function __construct(Client $client)
}

/**
* @param $appId
* @param $appSecret
* @param string $appId
* @param string $appSecret
* @param array $scopes
* @param array $headers
* @param null $appUserId
* @param null $redirectUri
* @param string|null $appUserId
* @param string|null $redirectUri
* @param Cache|null $cache
* @return Bookboon
* @throws Exception\UsageException
*/
public static function create($appId, $appSecret, array $scopes, array $headers = [], $appUserId = null, $redirectUri = null, Cache $cache = null)
{
public static function create(
$appId,
$appSecret,
array $scopes,
array $headers = [],
$appUserId = null,
$redirectUri = null,
Cache $cache = null
) {
$headersObject = new Headers();
foreach ($headers as $key => $value) {
$headersObject->set($key, $value);
}

return new Bookboon(new OauthClient($appId, $appSecret, $headersObject, $scopes, $cache, $redirectUri, $appUserId));
return new Bookboon(
new OauthClient($appId, $appSecret, $headersObject, $scopes, $cache, $redirectUri, $appUserId)
);
}

/**
* @param $url
* @param string $url
* @param array $variables
* @param $httpMethod
* @param bool $shouldCache
* @param string $httpMethod
* @param boolean $shouldCache
* @return BookboonResponse
* @throws Exception\UsageException
*/
public function rawRequest($url, array $variables = [], $httpMethod = Client::HTTP_GET, $shouldCache = true)
{
Expand Down
62 changes: 46 additions & 16 deletions src/Client/BasicAuthClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class BasicAuthClient implements Client
{
use ClientTrait, ResponseTrait, RequestTrait;

const C_VERSION = '2.0';
const C_VERSION = '2.1';

protected static $CURL_REQUESTS;

Expand All @@ -28,7 +28,18 @@ class BasicAuthClient implements Client
CURLOPT_SSL_VERIFYHOST => 2,
);

public function __construct($apiId, $apiSecret, Headers $headers, Cache $cache = null)
protected $_apiUri;

/**
* BasicAuthClient constructor.
* @param string $apiId
* @param string $apiSecret
* @param Headers $headers
* @param Cache|null $cache
* @param string|null $apiUri
* @throws UsageException
*/
public function __construct($apiId, $apiSecret, Headers $headers, Cache $cache = null, $apiUri = null)
{
if (empty($apiId) || empty($apiSecret)) {
throw new UsageException("Key and secret are required");
Expand All @@ -38,23 +49,32 @@ public function __construct($apiId, $apiSecret, Headers $headers, Cache $cache =
$this->setApiSecret($apiSecret);
$this->setCache($cache);
$this->setHeaders($headers);

$this->_apiUri = $this->parseUriOrDefault($apiUri);
}

/**
* Makes the actual query call to the remote api.
*
* @param string $url The url relative to the address
* @param string $uri The url relative to the address
* @param string $type Bookboon::HTTP_GET or Bookboon::HTTP_POST
* @param array $variables array of post variables (key => value)
* @oaram string $contentType
*
* @param string $contentType
* @return BookboonResponse
* @throws ApiAuthenticationException
* @throws ApiGeneralException
* @throws ApiTimeoutException
* @throws \Bookboon\Api\Exception\ApiNotFoundException
* @throws \Bookboon\Api\Exception\ApiSyntaxException
* @oaram string $contentType
*
*/
protected function executeQuery($url, $type = self::HTTP_GET, $variables = array(), $contentType = self::CONTENT_TYPE_FORM)
{
protected function executeQuery(
$uri,
$type = self::HTTP_GET,
$variables = array(),
$contentType = self::CONTENT_TYPE_FORM
) {
$http = curl_init();
$headers = $this->getHeaders()->getAll();

Expand All @@ -70,7 +90,7 @@ protected function executeQuery($url, $type = self::HTTP_GET, $variables = array
}

curl_setopt($http, CURLOPT_USERAGENT, $this->getUserAgentString());
curl_setopt($http, CURLOPT_URL, Client::API_PROTOCOL . "://$url");
curl_setopt($http, CURLOPT_URL, $uri);
curl_setopt($http, CURLOPT_USERPWD, $this->getApiId() . ':' . $this->getApiSecret());
curl_setopt($http, CURLOPT_HTTPHEADER, $headers);

Expand All @@ -97,7 +117,7 @@ protected function executeQuery($url, $type = self::HTTP_GET, $variables = array
substr($response, $headersSize),
substr($response, 0, $headersSize),
$httpStatus,
$url
$uri
);

return new BookboonResponse($responseArray, $headers);
Expand Down Expand Up @@ -153,8 +173,8 @@ public function requestAccessToken(array $options = array(), $type = OauthGrants
}

/**
* @param $variables
* @param $contentType
* @param array $variables
* @param string $contentType
* @return string
*/
protected function encodeByContentType(array $variables, $contentType)
Expand All @@ -164,7 +184,7 @@ protected function encodeByContentType(array $variables, $contentType)


/**
* @param $appUserId
* @param string $appUserId
* @throws UsageException
*/
public function setAct($appUserId)
Expand Down Expand Up @@ -202,19 +222,29 @@ public function generateState()
}

/**
* @param $stateParameter
* @param $stateSession
* @return bool
* @throws ApiInvalidStateException
* @param string $stateParameter
* @param string $stateSession
* @return boolean
* @throws UsageException
*/
public function isCorrectState($stateParameter, $stateSession)
{
throw new UsageException("Not Supported");
}

/**
* @return string
*/
protected function getComponentVersion()
{
return self::C_VERSION;
}

/**
* @return string
*/
protected function getBaseApiUri()
{
return $this->_apiUri;
}
}
25 changes: 13 additions & 12 deletions src/Client/BookboonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,55 +9,56 @@ class BookboonResponse
/**
* @var array
*/
protected $returnArray;
protected $responseArray;

/**
* @var array
*/
protected $headers;
protected $responseHeaders;

/**
* @var array
* @var EntityStore
*/
protected $entityStore;

/**
* BookboonResponse constructor.
* @param $returnArray
* @param $headers
* @param array $responseArray
* @param array $responseHeaders
*/
public function __construct($returnArray, $headers)
public function __construct(array $responseArray, array $responseHeaders)
{
$this->returnArray = $returnArray;
$this->headers = $headers;
$this->responseArray = $responseArray;
$this->responseHeaders = $responseHeaders;
}

/**
* @return array
*/
public function getHeaders()
{
return $this->headers;

return $this->responseHeaders;
}

/**
* @return array
*/
public function getReturnArray()
{
return $this->returnArray;
return $this->responseArray;
}

/**
* @return array
* @return EntityStore
*/
public function getEntityStore()
{
return $this->entityStore;
}

/**
* @param $entityStore
* @param EntityStore $entityStore
*/
public function setEntityStore(EntityStore $entityStore)
{
Expand Down
5 changes: 3 additions & 2 deletions src/Client/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ interface Client
const CONTENT_TYPE_FORM = 'application/x-www-form-urlencoded';

const API_PROTOCOL = 'https';
const API_URL = 'bookboon.com/api';
const API_HOST = 'bookboon.com';
const API_PATH = '/api';

const VERSION = 'Bookboon-PHP/3.0';
const VERSION = 'Bookboon-PHP/3.1';

/**
* Prepares the call to the api and if enabled tries cache provider first for GET calls.
Expand Down
2 changes: 1 addition & 1 deletion src/Client/Oauth/BookboonProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BookboonProvider extends AbstractProvider

public function __construct(array $options = [], array $collaborators = [])
{
if (isset($options['baseUri'])) {
if (isset($options['baseUri']) && $options['baseUri'] != "") {
$parts = explode('://', $options['baseUri']);
$this->protocol = $parts[0];
$this->host = $parts[1];
Expand Down
Loading

0 comments on commit 65ba475

Please sign in to comment.