Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#81: HttpRequestService ability to work with multipart #112

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"tecnickcom/tcpdf": "~6.2.22",
"doctrine/dbal": "^3.6",
"ext-json": "*",
"php-mock/php-mock-phpunit": "^2.9"
"php-mock/php-mock-phpunit": "^2.9",
"riverline/multipart-parser": "^2.1"
},
"require-dev": {
"fakerphp/faker": "^1.23",
Expand Down
58 changes: 57 additions & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

55 changes: 54 additions & 1 deletion src/Services/HttpRequestService.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use GuzzleHttp\Cookie\CookieJar;
use Psr\Http\Message\ResponseInterface;
use GuzzleHttp\Exception\RequestException;
use Riverline\MultiPartParser\StreamedPart;
use RonasIT\Support\Exceptions\InvalidJSONFormatException;
use RonasIT\Support\Exceptions\UnknownRequestMethodException;

Expand Down Expand Up @@ -127,8 +128,19 @@ public function send(string $method, string $url, array $data = [], array $heade
}
}

public function parseMultipart(string $content): StreamedPart
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
public function parseMultipart(string $content): StreamedPart
public function multipart(): Generator

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's get content from the response instead

{
$stream = fopen('php://temp', 'rw');
fwrite($stream, $content);
rewind($stream);

return app()->makeWith(StreamedPart::class, ['stream' => $stream]);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return app()->makeWith(StreamedPart::class, ['stream' => $stream]);
return app()
->makeWith(StreamedPart::class, ['stream' => $stream])
->getParts();

}

protected function sendRequest($method, $url, array $data = [], array $headers = []): ResponseInterface
{
$headers = array_change_key_case($headers);

$this->setOptions($headers);
$this->setData($method, $headers, $data);

Expand Down Expand Up @@ -222,15 +234,56 @@ protected function setData(string $method, array $headers, array $data = []): vo
return;
}

$headers = array_change_key_case($headers);
$contentType = Arr::get($headers, 'content-type');

if (preg_match('/application\/json/', $contentType)) {
$this->options['json'] = $data;
} elseif (preg_match('/application\/x-www-form-urlencoded/', $contentType)) {
$this->options['form_params'] = $data;
} elseif (preg_match('/multipart\/form-data/', $contentType)) {
$this->options['multipart'] = $this->getMultipartOptionReplacement($data);
} else {
$this->options['body'] = json_encode($data);
}
}

protected function getMultipartOptionReplacement(array $data): array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
protected function getMultipartOptionReplacement(array $data): array
protected function convertToMultipart(array $data): array

{
Arr::forget($this->options, 'headers.content-type');
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please increase Laravel min version requirements up to 5.7

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's move this logic out of this function


$options = [];

foreach ($data as $key => $value) {
if (is_array($value)) {
$options = array_merge($options, $this->getArrayMultipartOptionReplacement($key, $value));
} else {
$options[] = [
'name' => $key,
'contents' => $value
];
}
}

return $options;
}

protected function getArrayMultipartOptionReplacement(string $parentKey, array $items): array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please keep only one method convertToMultipart with optional parentKey arg

{
$options = [];

foreach ($items as $key => $item) {
$preparedKey = "{$parentKey}[{$key}]";

if (is_array($item)) {
$options = array_merge($options, $this->getArrayMultipartOptionReplacement($preparedKey, $item));
} else {
$options[] = [
'name' => $preparedKey,
'contents' => $item
];
}
}

return $options;
}
}
70 changes: 68 additions & 2 deletions tests/HttpRequestServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public function testSendPutWithMultipartContentType()
[
'headers' => [
'some_header' => 'some_header_value',
'Content-type' => 'application/x-www-form-urlencoded'
'content-type' => 'application/x-www-form-urlencoded'
],
'cookies' => null,
'allow_redirects' => true,
Expand All @@ -163,6 +163,72 @@ public function testSendPutWithMultipartContentType()
]);
}

public function testSendPutMultipartContentTypeWithFiles(): void
{
$this->mockGuzzleClient('put', [
'https://some.url.com',
[
'headers' => [],
'cookies' => null,
'allow_redirects' => true,
'connect_timeout' => 0,
'multipart' => [
[
'name' => '0[first_file]',
'contents' => 'first_file_content',
],
[
'name' => '0[second_file][first_file]',
'contents' => 'first_file_content',
],
[
'name' => '0[second_file][second_file]',
'contents' => 'second_file_content',
],
[
'name' => '1[first_file]',
'contents' => 'first_file_content',
],
[
'name' => '1[second_file]',
'contents' => 'second_file_content',
]
]
]
]);

$this->httpRequestServiceClass->put('https://some.url.com', [
[
'first_file' => 'first_file_content',
'second_file' => [
'first_file' => 'first_file_content',
'second_file' => 'second_file_content'
]
],
[
'first_file' => 'first_file_content',
'second_file' => 'second_file_content'
]
], [
'Content-type' => 'multipart/form-data;'
]);
}

public function testParseMultipartContent(): void
{
$multipartContent = $this->getFixture('multipart_content');

$multipartObject = $this->httpRequestServiceClass->parseMultipart($multipartContent);

$parsedData = [];

foreach ($multipartObject->getParts() as $part) {
$parsedData[] = [$part->getName(), $part->getBody()];
}

$this->assertEqualsFixture('parsed_multipart_content.json', $parsedData);
}

public function sendPutAsJSONData(): array
{
return [
Expand Down Expand Up @@ -194,7 +260,7 @@ public function testSendPutAsJSON(array $headers)
$this->mockGuzzleClient('put', [
'https://some.url.com',
[
'headers' => $headers,
'headers' => ['content-type' => 'application/json'],
'cookies' => null,
'allow_redirects' => true,
'connect_timeout' => 0,
Expand Down
21 changes: 21 additions & 0 deletions tests/fixtures/HttpRequestServiceTest/multipart_content
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<<<EOL
User-Agent: curl/7.21.2 (x86_64-apple-darwin)
Host: localhost:8080
Accept: */*
Content-Type: multipart/form-data; boundary=----------------------------83ff53821b7c

------------------------------83ff53821b7c
Content-Disposition: form-data; name="foo"

bar
------------------------------83ff53821b7c
Content-Transfer-Encoding: base64

YmFzZTY0
------------------------------83ff53821b7c
Content-Disposition: form-data; name="upload"; filename="text.txt"
Content-Type: text/plain

File content
------------------------------83ff53821b7c--
EOL;
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
[
"foo",
"bar"
],
[
null,
"base64"
],
[
"upload",
"File content"
]
]
Loading