Skip to content

Commit 1092817

Browse files
committed
Merge remote-tracking branch 'origin/feat-multipart' into feat-php-multiparts
# Conflicts: # mock-server/app/http.php # mock-server/src/Utopia/Response.php # tests/resources/spec.json
2 parents 1277306 + b3f8668 commit 1092817

File tree

18 files changed

+262
-131
lines changed

18 files changed

+262
-131
lines changed

mock-server/app/http.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464
->label('sdk.response.type', Response::CONTENT_TYPE_JSON)
6565
->inject('response')
6666
->action(function (Response $response) {
67-
$response->json(['version' => '1.0.0']);
67+
$response->json([ 'version' => '1.0.0' ]);
6868
});
6969

7070
// Mock Routes
@@ -292,6 +292,7 @@
292292
->label('sdk.mock', true)
293293
->inject('response')
294294
->action(function (Response $response) {
295+
295296
$response
296297
->setContentType('text/plain')
297298
->addHeader('Content-Disposition', 'attachment; filename="test.txt"')
@@ -316,10 +317,11 @@
316317
->param('x', '', new Text(100), 'Sample string param')
317318
->param('y', '', new Integer(true), 'Sample numeric param')
318319
->param('z', null, new ArrayList(new Text(256), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Sample array param')
319-
->param('file', [], new File(), 'Sample file param', skipValidation: true)
320+
->param('payload', [], new File(), 'Sample file param', skipValidation: true)
320321
->inject('request')
321322
->inject('response')
322323
->action(function (string $x, int $y, array $z, mixed $file, Request $request, Response $response) {
324+
323325
$file = $request->getFiles('payload');
324326

325327
$contentRange = $request->getHeader('content-range');
@@ -364,7 +366,7 @@
364366
if ($end !== $size - 1) {
365367
$response->json([
366368
'$id' => ID::custom('newfileid'),
367-
'chunksTotal' => (int)ceil($size / ($end + 1 - $start)),
369+
'chunksTotal' => (int) ceil($size / ($end + 1 - $start)),
368370
'chunksUploaded' => ceil($start / $chunkSize) + 1
369371
]);
370372
}
@@ -401,7 +403,7 @@
401403
->label('sdk.mock', true)
402404
->inject('response')
403405
->action(function (Response $response) {
404-
$file = \fread(\fopen(\getcwd() . '/resources/file.png', 'r'), \filesize(\getcwd() . '/resources/file.png'));
406+
$file = \file_get_contents(\getcwd() . '/resources/file.png');
405407

406408
$response->multipart([
407409
'x' => 'abc',
@@ -410,7 +412,6 @@
410412
]);
411413
});
412414

413-
414415
App::get('/v1/mock/tests/general/redirect')
415416
->desc('Redirect')
416417
->groups(['mock'])
@@ -424,7 +425,7 @@
424425
->label('sdk.response.model', Response::MODEL_MOCK)
425426
->label('sdk.mock', true)
426427
->inject('response')
427-
->action(function (UtopiaSwooleResponse $response) {
428+
->action(function (Response $response) {
428429
$response->redirect('/v1/mock/tests/general/redirect/done');
429430
});
430431

@@ -457,7 +458,7 @@
457458
->label('sdk.mock', true)
458459
->inject('response')
459460
->inject('request')
460-
->action(function (UtopiaSwooleResponse $response, Request $request) {
461+
->action(function (Response $response, Request $request) {
461462
$response->addCookie('cookieName', 'cookieValue', \time() + 31536000, '/', $request->getHostname(), true, true);
462463
});
463464

@@ -492,7 +493,7 @@
492493
->label('sdk.response.model', Response::MODEL_NONE)
493494
->label('sdk.mock', true)
494495
->inject('response')
495-
->action(function (UtopiaSwooleResponse $response) {
496+
->action(function (Response $response) {
496497
$response->noContent();
497498
});
498499

@@ -569,7 +570,8 @@
569570
->label('sdk.response.model', Response::MODEL_ANY)
570571
->label('sdk.mock', true)
571572
->inject('response')
572-
->action(function (UtopiaSwooleResponse $response) {
573+
->action(function (Response $response) {
574+
573575
$response
574576
->setStatusCode(502)
575577
->text('This is a text error');
@@ -590,7 +592,7 @@
590592
->param('success', '', new Text(1024), 'OAuth2 success redirect URI.')
591593
->param('failure', '', new Text(1024), 'OAuth2 failure redirect URI.')
592594
->inject('response')
593-
->action(function (string $clientId, array $scopes, string $state, string $success, string $failure, UtopiaSwooleResponse $response) {
595+
->action(function (string $clientId, array $scopes, string $state, string $success, string $failure, Response $response) {
594596
$response->redirect($success . '?' . \http_build_query(['code' => 'abcdef', 'state' => $state]));
595597
});
596598

@@ -608,7 +610,7 @@
608610
->param('code', '', new Text(100), 'OAuth2 state.', true)
609611
->param('refresh_token', '', new Text(100), 'OAuth2 refresh token.', true)
610612
->inject('response')
611-
->action(function (string $client_id, string $client_secret, string $grantType, string $redirectURI, string $code, string $refreshToken, UtopiaSwooleResponse $response) {
613+
->action(function (string $client_id, string $client_secret, string $grantType, string $redirectURI, string $code, string $refreshToken, Response $response) {
612614
if ($client_id != '1') {
613615
throw new Exception(Exception::GENERAL_MOCK, 'Invalid client ID');
614616
}
@@ -647,7 +649,7 @@
647649
->label('docs', false)
648650
->param('token', '', new Text(100), 'OAuth2 Access Token.')
649651
->inject('response')
650-
->action(function (string $token, UtopiaSwooleResponse $response) {
652+
->action(function (string $token, Response $response) {
651653
if ($token != '123456') {
652654
throw new Exception(Exception::GENERAL_MOCK, 'Invalid token');
653655
}
@@ -665,7 +667,8 @@
665667
->label('scope', 'public')
666668
->label('docs', false)
667669
->inject('response')
668-
->action(function (UtopiaSwooleResponse $response) {
670+
->action(function (Response $response) {
671+
669672
$response->json([
670673
'result' => 'success',
671674
]);
@@ -677,7 +680,8 @@
677680
->label('scope', 'public')
678681
->label('docs', false)
679682
->inject('response')
680-
->action(function (UtopiaSwooleResponse $response) {
683+
->action(function (Response $response) {
684+
681685
$response
682686
->setStatusCode(Response::STATUS_CODE_BAD_REQUEST)
683687
->json([
@@ -690,11 +694,12 @@
690694
->inject('utopia')
691695
->inject('response')
692696
->inject('request')
693-
->action(function (App $utopia, UtopiaSwooleResponse $response, Request $request) {
697+
->action(function (App $utopia, Response $response, Request $request) {
698+
694699
$result = [];
695-
$route = $utopia->getRoute();
696-
$path = APP_STORAGE_CACHE . '/tests.json';
697-
$tests = (\file_exists($path)) ? \json_decode(\file_get_contents($path), true) : [];
700+
$route = $utopia->getRoute();
701+
$path = APP_STORAGE_CACHE . '/tests.json';
702+
$tests = (\file_exists($path)) ? \json_decode(\file_get_contents($path), true) : [];
698703

699704
if (!\is_array($tests)) {
700705
throw new Exception(Exception::GENERAL_MOCK, 'Failed to read results', 500);
@@ -708,7 +713,9 @@
708713
throw new Exception(Exception::GENERAL_MOCK, 'Failed to save results', 500);
709714
}
710715

711-
$response->json(['result' => $route->getMethod() . ':' . $route->getPath() . ':passed']);
716+
if ($route->getPath() !== '/v1/mock/tests/general/multipart') {
717+
$response->json(['result' => $route->getMethod() . ':' . $route->getPath() . ':passed']);
718+
}
712719
});
713720

714721
App::error()
@@ -798,6 +805,7 @@ function () use ($http) {
798805
$http->on(Constant::EVENT_REQUEST, function (SwooleRequest $swooleRequest, SwooleResponse $swooleResponse) {
799806
$request = new Request($swooleRequest);
800807
$response = new Response($swooleResponse);
808+
801809
$app = new App('UTC');
802810

803811
$app->run($request, $response);

mock-server/src/Utopia/Response.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class Response extends UtopiaResponse
2525
public const MODEL_ERROR_DEV = 'errorDev';
2626
public const MODEL_BASE_LIST = 'baseList';
2727
public const MODEL_MULTIPART = 'multipart';
28+
2829
// Mock
2930
public const MODEL_MOCK = 'mock';
3031

@@ -51,6 +52,7 @@ public function __construct(SwooleResponse $response)
5152
public const CONTENT_TYPE_YAML = 'application/x-yaml';
5253
public const CONTENT_TYPE_NULL = 'null';
5354
public const CONTENT_TYPE_MULTIPART = 'multipart/form-data';
55+
5456
/**
5557
* List of defined output objects
5658
*/
@@ -148,6 +150,8 @@ public function dynamic(Document $document, string $model): void
148150
}
149151
}
150152

153+
154+
151155
/**
152156
* Generate valid response object from document data
153157
*

src/SDK/Language/Python.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,13 @@ public function getFiles(): array
157157
],
158158
[
159159
'scope' => 'default',
160-
'destination' => '{{ spec.title | caseSnake}}/input_file.py',
161-
'template' => 'python/package/input_file.py.twig',
160+
'destination' => '{{ spec.title | caseSnake}}/payload.py',
161+
'template' => 'python/package/payload.py.twig',
162+
],
163+
[
164+
'scope' => 'default',
165+
'destination' => '{{ spec.title | caseSnake}}/multipart.py',
166+
'template' => 'python/package/multipart.py.twig',
162167
],
163168
[
164169
'scope' => 'default',
@@ -232,7 +237,8 @@ public function getTypeName(array $parameter, array $spec = []): string
232237
return \ucfirst($parameter['name']);
233238
}
234239
return match ($parameter['type'] ?? '') {
235-
self::TYPE_FILE => 'InputFile',
240+
self::TYPE_PAYLOAD,
241+
self::TYPE_FILE => 'Payload',
236242
self::TYPE_NUMBER,
237243
self::TYPE_INTEGER => 'float',
238244
self::TYPE_BOOLEAN => 'bool',
@@ -327,8 +333,11 @@ public function getParamExample(array $param): string
327333
case self::TYPE_OBJECT:
328334
$output .= '{}';
329335
break;
336+
case self::TYPE_PAYLOAD:
337+
$output .= 'Payload.from_json({ "key": "value" })';
338+
break;
330339
case self::TYPE_FILE:
331-
$output .= "InputFile.from_path('file.png')";
340+
$output .= "Payload.from_file('file.png')";
332341
break;
333342
}
334343
} else {
@@ -345,8 +354,11 @@ public function getParamExample(array $param): string
345354
case self::TYPE_STRING:
346355
$output .= "'{$example}'";
347356
break;
357+
case self::TYPE_PAYLOAD:
358+
$output .= 'Payload.from_json({ "key": "value" })';
359+
break;
348360
case self::TYPE_FILE:
349-
$output .= "InputFile.from_path('file.png')";
361+
$output .= "Payload.from_file('file.png')";
350362
break;
351363
}
352364
}

src/SDK/Language/Swift.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -526,10 +526,6 @@ protected function getPropertyType(array $property, array $spec, string $generic
526526
$type = $this->getTypeName($property);
527527
}
528528

529-
if (!$property['required']) {
530-
$type .= '?';
531-
}
532-
533529
return $type;
534530
}
535531

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
return self.client.call('{{ method.method | caseLower }}', api_path, {
2-
{% for parameter in method.parameters.header %}
2+
{%~ for parameter in method.parameters.header %}
33
'{{ parameter.name }}': {{ parameter.name | escapeKeyword | caseSnake }},
4-
{% endfor %}
5-
{% for key, header in method.headers %}
4+
{%- endfor %}
5+
{%~ for key, header in method.headers %}
66
'{{ key }}': '{{ header }}',
7-
{% endfor %}
7+
{%~ endfor %}
88
}, api_params{% if method.type == 'webAuth' %}, response_type='location'{% endif %})
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
{% for parameter in method.parameters.all %}
2-
{% if parameter.type == 'file' %}
1+
{%~ for parameter in method.parameters.all %}
2+
{%~ if parameter.type == 'file' %}
33
param_name = '{{ parameter.name }}'
44

5-
{% endif %}
6-
{% endfor %}
5+
{%- endif %}
6+
{%- endfor %}
77

88
upload_id = ''
9-
{% for parameter in method.parameters.all %}
10-
{% if parameter.isUploadID %}
9+
{%~ for parameter in method.parameters.all %}
10+
{%~ if parameter.isUploadID %}
1111
upload_id = {{ parameter.name | escapeKeyword | caseSnake }}
12-
{% endif %}
13-
{% endfor %}
12+
{%- endif %}
13+
{%- endfor %}
1414

1515
return self.client.chunked_upload(api_path, {
16-
{% for parameter in method.parameters.header %}
16+
{%~ for parameter in method.parameters.header %}
1717
'{{ parameter.name }}': {{ parameter.name | escapeKeyword | caseSnake }},
18-
{% endfor %}
19-
{% for key, header in method.headers %}
18+
{%- endfor %}
19+
{%~ for key, header in method.headers %}
2020
'{{ key }}': '{{ header }}',
21-
{% endfor %}
21+
{%~ endfor %}
2222
}, api_params, param_name, on_progress, upload_id)

templates/python/docs/example.md.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from {{ spec.title | caseSnake }}.client import Client
22
{% if method.parameters.all | filter((param) => param.type == 'file') | length > 0 %}
3-
from {{ spec.title | caseSnake }}.input_file import InputFile
3+
from {{ spec.title | caseSnake }}.payload import Payload
44
{% endif %}
55
{% set added = [] %}
66
{% for parameter in method.parameters.all %}

0 commit comments

Comments
 (0)