Skip to content

Commit 2a5f238

Browse files
fix: combine StreamGeneratorResponse with StreamTraversableResponse
Signed-off-by: SebastianKrupinski <krupinskis05@gmail.com>
1 parent 0417403 commit 2a5f238

7 files changed

Lines changed: 20 additions & 80 deletions

File tree

apps/dav/lib/Controller/CalendarExportController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
1616
use OCP\AppFramework\Http\Attribute\UserRateLimit;
1717
use OCP\AppFramework\Http\DataResponse;
18-
use OCP\AppFramework\Http\StreamGeneratorResponse;
18+
use OCP\AppFramework\Http\StreamTraversableResponse;
1919
use OCP\AppFramework\OCSController;
2020
use OCP\Calendar\CalendarExportOptions;
2121
use OCP\Calendar\ICalendarExport;
@@ -46,7 +46,7 @@ public function __construct(
4646
* @param array{rangeStart:string,rangeCount:positive-int} $options configuration options
4747
* @param string|null $user system user id
4848
*
49-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'text/calendar; charset=UTF-8'|'application/calendar+json; charset=UTF-8'|'application/calendar+xml; charset=UTF-8'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
49+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'text/calendar; charset=UTF-8'|'application/calendar+json; charset=UTF-8'|'application/calendar+xml; charset=UTF-8'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5050
*
5151
* 200: data in requested format
5252
* 400: invalid parameters
@@ -99,7 +99,7 @@ public function export(string $target, ?string $type = null, ?array $options = n
9999
'xcal' => 'application/calendar+xml; charset=UTF-8',
100100
default => 'text/calendar; charset=UTF-8'
101101
};
102-
$response = new StreamGeneratorResponse($this->exportService->export($calendar, $options), $contentType, Http::STATUS_OK);
102+
$response = new StreamTraversableResponse($this->exportService->export($calendar, $options), Http::STATUS_OK, ['Content-Type' => $contentType]);
103103
$response->cacheFor(0);
104104

105105
return $response;

apps/dav/lib/Controller/CalendarImportController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2121
use OCP\AppFramework\Http\Attribute\UserRateLimit;
2222
use OCP\AppFramework\Http\DataResponse;
23-
use OCP\AppFramework\Http\StreamGeneratorResponse;
23+
use OCP\AppFramework\Http\StreamTraversableResponse;
2424
use OCP\AppFramework\OCSController;
2525
use OCP\Calendar\CalendarImportOptions;
2626
use OCP\Calendar\IManager;
@@ -53,7 +53,7 @@ public function __construct(
5353
* @param string $data calendar data
5454
* @param string|null $user system user id
5555
*
56-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
56+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5757
*
5858
* 200: NDJSON stream of import event objects
5959
* 400: invalid parameters
@@ -62,7 +62,7 @@ public function __construct(
6262
#[ApiRoute(verb: 'POST', url: '/import', root: '/calendar')]
6363
#[UserRateLimit(limit: 10, period: 3600)]
6464
#[NoAdminRequired]
65-
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamGeneratorResponse {
65+
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamTraversableResponse {
6666
$calendarId = $target;
6767
$format = isset($options['format']) ? $options['format'] : null;
6868
$validation = isset($options['validation']) ? (int)$options['validation'] : null;
@@ -144,6 +144,6 @@ public function import(string $transaction, string $target, array $options, stri
144144
}
145145
})();
146146

147-
return new StreamGeneratorResponse($stream, 'application/x-ndjson');
147+
return new StreamTraversableResponse($stream, Http::STATUS_OK, ['Content-Type' => 'application/x-ndjson']);
148148
}
149149
}

apps/dav/lib/Controller/ContactsImportController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
2020
use OCP\AppFramework\Http\Attribute\UserRateLimit;
2121
use OCP\AppFramework\Http\DataResponse;
22-
use OCP\AppFramework\Http\StreamGeneratorResponse;
22+
use OCP\AppFramework\Http\StreamTraversableResponse;
2323
use OCP\AppFramework\OCSController;
2424
use OCP\Contacts\ContactsImportOptions;
2525
use OCP\Contacts\IManager;
@@ -53,7 +53,7 @@ public function __construct(
5353
* @param string $data contacts data
5454
* @param string|null $user system user id
5555
*
56-
* @return StreamGeneratorResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
56+
* @return StreamTraversableResponse<Http::STATUS_OK, array{Content-Type:'application/x-ndjson'}> | DataResponse<Http::STATUS_BAD_REQUEST|Http::STATUS_UNAUTHORIZED, array{error?: non-empty-string}, array{}>
5757
*
5858
* 200: NDJSON stream of import event objects
5959
* 400: invalid parameters
@@ -62,7 +62,7 @@ public function __construct(
6262
#[ApiRoute(verb: 'POST', url: '/import', root: '/contacts')]
6363
#[UserRateLimit(limit: 10, period: 3600)]
6464
#[NoAdminRequired]
65-
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamGeneratorResponse {
65+
public function import(string $transaction, string $target, array $options, string $data, ?string $user = null): DataResponse|StreamTraversableResponse {
6666
$addressBookId = $target;
6767
$format = isset($options['format']) ? $options['format'] : null;
6868
$validation = isset($options['validation']) ? (int)$options['validation'] : null;
@@ -141,6 +141,6 @@ public function import(string $transaction, string $target, array $options, stri
141141
}
142142
})();
143143

144-
return new StreamGeneratorResponse($stream, 'application/x-ndjson');
144+
return new StreamTraversableResponse($stream, Http::STATUS_OK, ['Content-Type' => 'application/x-ndjson']);
145145
}
146146
}

lib/composer/composer/autoload_classmap.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@
165165
'OCP\\AppFramework\\Http\\RedirectToDefaultAppResponse' => $baseDir . '/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php',
166166
'OCP\\AppFramework\\Http\\Response' => $baseDir . '/lib/public/AppFramework/Http/Response.php',
167167
'OCP\\AppFramework\\Http\\StandaloneTemplateResponse' => $baseDir . '/lib/public/AppFramework/Http/StandaloneTemplateResponse.php',
168-
'OCP\\AppFramework\\Http\\StreamGeneratorResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamGeneratorResponse.php',
169168
'OCP\\AppFramework\\Http\\StreamResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamResponse.php',
170169
'OCP\\AppFramework\\Http\\StreamTraversableResponse' => $baseDir . '/lib/public/AppFramework/Http/StreamTraversableResponse.php',
171170
'OCP\\AppFramework\\Http\\TemplateResponse' => $baseDir . '/lib/public/AppFramework/Http/TemplateResponse.php',

lib/composer/composer/autoload_static.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
206206
'OCP\\AppFramework\\Http\\RedirectToDefaultAppResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/RedirectToDefaultAppResponse.php',
207207
'OCP\\AppFramework\\Http\\Response' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/Response.php',
208208
'OCP\\AppFramework\\Http\\StandaloneTemplateResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StandaloneTemplateResponse.php',
209-
'OCP\\AppFramework\\Http\\StreamGeneratorResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamGeneratorResponse.php',
210209
'OCP\\AppFramework\\Http\\StreamResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamResponse.php',
211210
'OCP\\AppFramework\\Http\\StreamTraversableResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/StreamTraversableResponse.php',
212211
'OCP\\AppFramework\\Http\\TemplateResponse' => __DIR__ . '/../../..' . '/lib/public/AppFramework/Http/TemplateResponse.php',

lib/public/AppFramework/Http/StreamGeneratorResponse.php

Lines changed: 0 additions & 63 deletions
This file was deleted.

tests/lib/AppFramework/Http/StreamGeneratorResponseTest.php renamed to tests/lib/AppFramework/Http/StreamTraversableResponseTest.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
namespace Test\AppFramework\Http;
1010

1111
use OCP\AppFramework\Http\IOutput;
12-
use OCP\AppFramework\Http\StreamGeneratorResponse;
12+
use OCP\AppFramework\Http\StreamTraversableResponse;
1313

14-
class StreamGeneratorResponseTest extends \Test\TestCase {
14+
class StreamTraversableResponseTest extends \Test\TestCase {
1515

1616
protected function setUp(): void {
1717
parent::setUp();
@@ -22,7 +22,7 @@ public function testConstructor() {
2222
yield 'chunk1';
2323
yield 'chunk2';
2424
};
25-
$response = new StreamGeneratorResponse($generator(), 'text/plain');
25+
$response = new StreamTraversableResponse($generator(), 200, ['Content-Type' => 'text/plain']);
2626

2727
$headers = $response->getHeaders();
2828
$this->assertEquals('text/plain', $headers['Content-Type']);
@@ -37,8 +37,13 @@ public function testCallback() {
3737
$count++;
3838
yield 'chunk2';
3939
};
40-
$response = new StreamGeneratorResponse($generator(), 'text/plain');
40+
$response = new StreamTraversableResponse($generator(), 200, ['Content-Type' => 'text/plain']);
4141
$output = $this->createMock(IOutput::class);
42+
$output->expects($this->exactly(2))
43+
->method('setOutput')
44+
->with($this->callback(function ($chunk) {
45+
return in_array($chunk, ['chunk1', 'chunk2'], true);
46+
}));
4247

4348
$response->callback($output);
4449
$this->assertEquals($count, 2);

0 commit comments

Comments
 (0)