Skip to content

Commit 1f163d4

Browse files
committed
formatting and method extraction
1 parent f9b85ba commit 1f163d4

File tree

1 file changed

+41
-33
lines changed

1 file changed

+41
-33
lines changed

src/Illuminate/Http/Client/PendingRequest.php

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -471,19 +471,7 @@ public function send(string $method, string $url, array $options = [])
471471

472472
return retry($this->tries ?? 1, function () use ($method, $url, $options) {
473473
try {
474-
$laravelData = $options[$this->bodyFormat] ?? $options['query'] ?? [];
475-
476-
$urlString = Str::of($url);
477-
478-
if (empty($laravelData) && $method === 'GET' && $urlString->contains('?')) {
479-
$laravelData = (string) $urlString->after('?');
480-
}
481-
482-
if (is_string($laravelData)) {
483-
parse_str($laravelData, $parsedData);
484-
485-
$laravelData = is_array($parsedData) ? $parsedData : [];
486-
}
474+
$laravelData = $this->parseRequestData($method, $url, $options);
487475

488476
return tap(new Response($this->buildClient()->request($method, $url, $this->mergeOptions([
489477
'laravel_data' => $laravelData,
@@ -504,6 +492,46 @@ public function send(string $method, string $url, array $options = [])
504492
}, $this->retryDelay ?? 100);
505493
}
506494

495+
/**
496+
* Parse multi-part form data.
497+
*
498+
* @param array $data
499+
* @return array|array[]
500+
*/
501+
protected function parseMultipartBodyFormat(array $data)
502+
{
503+
return collect($data)->map(function ($value, $key) {
504+
return is_array($value) ? $value : ['name' => $key, 'contents' => $value];
505+
})->values()->all();
506+
}
507+
508+
/**
509+
* Get the request data as an array so that we can attach it to the request for convenient assertions.
510+
*
511+
* @param string $method
512+
* @param string $url
513+
* @param array $options
514+
* @return array
515+
*/
516+
protected function parseRequestData($method, $url, array $options)
517+
{
518+
$laravelData = $options[$this->bodyFormat] ?? $options['query'] ?? [];
519+
520+
$urlString = Str::of($url);
521+
522+
if (empty($laravelData) && $method === 'GET' && $urlString->contains('?')) {
523+
$laravelData = (string) $urlString->after('?');
524+
}
525+
526+
if (is_string($laravelData)) {
527+
parse_str($laravelData, $parsedData);
528+
529+
$laravelData = is_array($parsedData) ? $parsedData : [];
530+
}
531+
532+
return $laravelData;
533+
}
534+
507535
/**
508536
* Build the Guzzle client.
509537
*
@@ -634,24 +662,4 @@ public function stub($callback)
634662

635663
return $this;
636664
}
637-
638-
/**
639-
* Parse multipart form data to allow for ['key' => 'value'] post data.
640-
*
641-
* @param array $data
642-
*
643-
* @return array|array[]
644-
*/
645-
protected function parseMultipartBodyFormat(array $data)
646-
{
647-
return array_map(function ($value, $key) {
648-
// If the value is an array Guzzle with throw an InvalidArgumentException,
649-
// so this use case is the original way of sending data, e.g. [['name' => 'name', 'contents' => 'content']]
650-
if (is_array($value)) {
651-
return $value;
652-
}
653-
654-
return ['name' => $key, 'contents' => $value];
655-
}, $data, array_keys($data));
656-
}
657665
}

0 commit comments

Comments
 (0)