Skip to content

Commit 214d5c7

Browse files
authored
Merge pull request #31 from elan-ev/issue-30
Allow passing of options to guzzle
2 parents 183619e + 3a39df7 commit 214d5c7

File tree

5 files changed

+111
-19
lines changed

5 files changed

+111
-19
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,6 @@
5050

5151
# 1.8.0
5252
- Fix for unauthorized access when extracting the Opencast API Version.
53+
54+
# 1.9.0
55+
- Allow passing additonal options to Guzzle #30

README.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ $config = [
2525
'timeout' => 0, // The API timeout. In seconds (default 0 to wait indefinitely). (optional)
2626
'connect_timeout' => 0, // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
2727
'version' => null, // The API Version. (Default null). (optional)
28-
'handler' => null // The callable Handler or HandlerStack. (Default null). (optional)
29-
'features' => null // A set of additional features [e.g. lucene search]. (Default null). (optional)
28+
'handler' => null, // The callable Handler or HandlerStack. (Default null). (optional)
29+
'features' => null, // A set of additional features [e.g. lucene search]. (Default null). (optional)
30+
'guzzle' => null, // Additional Guzzle Request Options. These options can overwrite some default options (Default null). (optional)
3031
];
3132

3233
$engageConfig = [
@@ -36,8 +37,9 @@ $engageConfig = [
3637
'timeout' => 0, // The API timeout. In seconds (default 0 to wait indefinitely). (optional)
3738
'connect_timeout' => 0, // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
3839
'version' => null, // The API version. (Default null). (optional)
39-
'handler' => null // The callable Handler or HandlerStack. (Default null). (optional)
40-
'features' => null // A set of additional features [e.g. lucene search]. (Default null). (optional)
40+
'handler' => null, // The callable Handler or HandlerStack. (Default null). (optional)
41+
'features' => null, // A set of additional features [e.g. lucene search]. (Default null). (optional)
42+
'guzzle' => null, // Additional Guzzle Request Options. These options can overwrite some default options (Default null). (optional)
4143
];
4244

4345
use OpencastApi\Opencast;
@@ -74,8 +76,9 @@ $config = [
7476
'timeout' => 0, // The API timeout. In seconds (default 0 to wait indefinitely). (optional)
7577
'connect_timeout' => 0, // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
7678
'version' => null, // The API version. (Default null). (optional)
77-
'handler' => null // The callable Handler or HandlerStack. (Default null). (optional)
78-
'features' => null // A set of additional features [e.g. lucene search]. (Default null). (optional)
79+
'handler' => null, // The callable Handler or HandlerStack. (Default null). (optional)
80+
'features' => null, // A set of additional features [e.g. lucene search]. (Default null). (optional)
81+
'guzzle' => null, // Additional Guzzle Request Options. These options can overwrite some default options (Default null). (optional)
7982
];
8083

8184

@@ -114,10 +117,12 @@ $config = [
114117
'timeout' => 0, // The API timeout. In seconds (default 0 to wait indefinitely). (optional)
115118
'connect_timeout' => 0, // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
116119
'version' => null, // The API version. (Default null). (optional)
117-
'handler' => null // The callable Handler or HandlerStack. (Default null). (optional)
118-
'features' => null // A set of additional features [e.g. lucene search]. (Default null). (optional)
120+
'handler' => null, // The callable Handler or HandlerStack. (Default null). (optional)
121+
'features' => null, // A set of additional features [e.g. lucene search]. (Default null). (optional)
122+
'guzzle' => null, // Additional Guzzle Request Options. These options can overwrite some default options (Default null). (optional)
119123
];
120124
```
125+
**UPDATE (v1.9.0):** a new config parameter called "guzzle" is introduced, which is intended to pass additional guzzle request options to the call. These options will take precedence over the default configs like uri, auth and timeouts, but some other options like query, fome_params and json will be overwritten by the function if present.
121126
**UPDATE (v1.7.0):** the new items called `features` is added to the configuration array. As of now, it is meant to hanlde the toggle behavior to enable/disable Lucene search endpoint simply by adding `'features' => ['lucene' => true]`. Just keep in mind that this endpoint id off by default and won't work in Opencast 16 onwards. Therefore, developer must be very careful to use this feature and to toggle it!
122127

123128
NOTE: the configuration for presentation (`engage` node) responsible for search has to follow the same definition as normal config. But in case any parameter is missing, the value will be taken from the main config param.

src/OpencastApi/Opencast.php

+13-8
Original file line numberDiff line numberDiff line change
@@ -78,21 +78,23 @@ class Opencast
7878
'username' => 'admin', // The API username. (required)
7979
'password' => 'opencast', // The API password. (required)
8080
'timeout' => 0, // The API timeout. In seconds (default 0 to wait indefinitely). (optional)
81-
'connect_timeout' => 0 // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
82-
'version' => null // The API Version. (Default null). (optional)
83-
'handler' => null // The callable Handler or HandlerStack. (Default null). (optional)
84-
'features' => null // A set of additional features [e.g. lucene search]. (Default null). (optional)
81+
'connect_timeout' => 0, // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
82+
'version' => null, // The API Version. (Default null). (optional)
83+
'handler' => null, // The callable Handler or HandlerStack. (Default null). (optional)
84+
'features' => null, // A set of additional features [e.g. lucene search]. (Default null). (optional)
85+
'guzzle' => null, // Additional Guzzle Request Options. These options can overwrite some default options (Default null). (optional)
8586
]
8687
8788
$engageConfig = [
8889
'url' => 'https://develop.opencast.org/', // The API url of the opencast instance (required)
8990
'username' => 'admin', // The API username. (required)
9091
'password' => 'opencast', // The API password. (required)
9192
'timeout' => 0, // The API timeout. In seconds (default 0 to wait indefinitely). (optional)
92-
'connect_timeout' => 0 // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
93-
'version' => null // The API Version. (Default null). (optional)
94-
'handler' => null // The callable Handler or HandlerStack. (Default null). (optional)
95-
'features' => null // A set of additional features [e.g. lucene search]. (Default null). (optional)
93+
'connect_timeout' => 0, // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
94+
'version' => null, // The API Version. (Default null). (optional)
95+
'handler' => null, // The callable Handler or HandlerStack. (Default null). (optional)
96+
'features' => null, // A set of additional features [e.g. lucene search]. (Default null). (optional)
97+
'guzzle' => null, // Additional Guzzle Request Options. These options can overwrite some default options (Default null). (optional)
9698
]
9799
*/
98100
/**
@@ -176,6 +178,9 @@ private function setEngageRestClient($config, $engageConfig)
176178
if (!isset($engageConfig['features']) && isset($config['features'])) {
177179
$engageConfig['features'] = $config['features'];
178180
}
181+
if (!isset($engageConfig['guzzle']) && isset($config['guzzle'])) {
182+
$engageConfig['guzzle'] = $config['guzzle'];
183+
}
179184
$this->engageRestClient = new OcRestClient($engageConfig);
180185
}
181186

src/OpencastApi/Rest/OcRestClient.php

+11-3
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ class OcRestClient extends Client
1818
private $noHeader = false;
1919
private $origin;
2020
private $features = [];
21+
private $globalOptions = [];
22+
2123
/*
2224
$config = [
2325
'url' => 'https://develop.opencast.org/', // The API url of the opencast instance (required)
@@ -27,7 +29,8 @@ class OcRestClient extends Client
2729
'connect_timeout' => 0, // The API connection timeout. In seconds (default 0 to wait indefinitely) (optional)
2830
'version' => null, // The API Version. (Default null). (optional)
2931
'handler' => null, // The callable Handler or HandlerStack. (Default null). (optional)
30-
'features' => null // A set of additional features [e.g. lucene search]. (Default null). (optional)
32+
'features' => null, // A set of additional features [e.g. lucene search]. (Default null). (optional)
33+
'guzzle' => null, // Additional Guzzle Request Options. These options can overwrite some default options (Default null). (optional)
3134
]
3235
*/
3336
public function __construct($config)
@@ -58,6 +61,10 @@ public function __construct($config)
5861
$this->features = $config['features'];
5962
}
6063

64+
if (isset($config['guzzle'])) {
65+
$this->globalOptions = $config['guzzle'];
66+
}
67+
6168
parent::__construct($parentConstructorConfig);
6269
}
6370

@@ -101,11 +108,12 @@ public function setRequestConnectionTimeout($connectionTimeout)
101108

102109
private function addRequestOptions($uri, $options)
103110
{
111+
$globalOptions = $this->globalOptions;
104112

105113
// Perform a temp no header request.
106114
if ($this->noHeader) {
107115
$this->noHeader = false;
108-
return array_merge($options , ['headers' => null]);
116+
return array_merge($globalOptions, $options, ['headers' => null]);
109117
}
110118

111119
$generalOptions = [];
@@ -150,7 +158,7 @@ private function addRequestOptions($uri, $options)
150158
}
151159
}
152160

153-
$requestOptions = array_merge($generalOptions, $options);
161+
$requestOptions = array_merge($generalOptions, $globalOptions, $options);
154162
return $requestOptions;
155163
}
156164

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace Tests\Unit;
5+
6+
use PHPUnit\Framework\TestCase;
7+
use OpencastApi\Opencast;
8+
9+
class OcRestClientWithGuzzleOptionTest extends TestCase
10+
{
11+
protected function setUp(): void
12+
{
13+
parent::setUp();
14+
$config = \Tests\DataProvider\SetupDataProvider::getConfig();
15+
$config['guzzle'] = [
16+
'debug' => true,
17+
'query' => ['limit' => 1],
18+
'auth' => [
19+
$config['username'], $config['password']
20+
]
21+
];
22+
$ocRestApi = new Opencast($config, [], false);
23+
$this->ocBaseApi = $ocRestApi->baseApi;
24+
$this->ocEventApi = $ocRestApi->eventsApi;
25+
26+
$config['guzzle']['auth'] = [
27+
'faulty', 'faulty'
28+
];
29+
$ocRestApiFaulty = new Opencast($config, [], false);
30+
$this->ocBaseApiFaulty = $ocRestApiFaulty->baseApi;
31+
}
32+
33+
/**
34+
* @test
35+
*/
36+
public function get(): void
37+
{
38+
$response = $this->ocBaseApi->get();
39+
$this->assertSame(200, $response['code'], 'Failure to get base info');
40+
}
41+
42+
/**
43+
* @test
44+
*/
45+
public function get_events_overwrite_guzzle_option(): void
46+
{
47+
$response = $this->ocEventApi->getAll(['limit' => 4]);
48+
$this->assertSame(200, $response['code'], 'Failure to get events');
49+
$this->assertSame(4, count($response['body']), 'Failure to get specifically 4 events.');
50+
}
51+
52+
/**
53+
* @test
54+
*/
55+
public function get_faulty(): void
56+
{
57+
$response = $this->ocBaseApiFaulty->get();
58+
$this->assertSame(401, $response['code'], 'Failure to overwrite default option!');
59+
}
60+
61+
/**
62+
* @test
63+
*/
64+
public function get_no_auth(): void
65+
{
66+
$response = $this->ocBaseApi->noHeader()->get();
67+
$this->assertSame(200, $response['code'], 'Failure to get base info');
68+
$this->assertSame(true, is_object($response['body']), 'Failure to get base info correctly');
69+
}
70+
}
71+
?>

0 commit comments

Comments
 (0)