-
Notifications
You must be signed in to change notification settings - Fork 14
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -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; | ||||||||||
|
||||||||||
|
@@ -127,8 +128,19 @@ public function send(string $method, string $url, array $data = [], array $heade | |||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
public function parseMultipart(string $content): StreamedPart | ||||||||||
{ | ||||||||||
$stream = fopen('php://temp', 'rw'); | ||||||||||
fwrite($stream, $content); | ||||||||||
rewind($stream); | ||||||||||
|
||||||||||
return app()->makeWith(StreamedPart::class, ['stream' => $stream]); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
} | ||||||||||
|
||||||||||
protected function sendRequest($method, $url, array $data = [], array $headers = []): ResponseInterface | ||||||||||
{ | ||||||||||
$headers = array_change_key_case($headers); | ||||||||||
|
||||||||||
$this->setOptions($headers); | ||||||||||
$this->setData($method, $headers, $data); | ||||||||||
|
||||||||||
|
@@ -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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
{ | ||||||||||
Arr::forget($this->options, 'headers.content-type'); | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please increase Laravel min version requirements up to 5.7 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. please keep only one method |
||||||||||
{ | ||||||||||
$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; | ||||||||||
} | ||||||||||
} |
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" | ||
] | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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