Skip to content

Commit

Permalink
formatting and method extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
taylorotwell committed Apr 19, 2020
1 parent f9b85ba commit 1f163d4
Showing 1 changed file with 41 additions and 33 deletions.
74 changes: 41 additions & 33 deletions src/Illuminate/Http/Client/PendingRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -471,19 +471,7 @@ public function send(string $method, string $url, array $options = [])

return retry($this->tries ?? 1, function () use ($method, $url, $options) {
try {
$laravelData = $options[$this->bodyFormat] ?? $options['query'] ?? [];

$urlString = Str::of($url);

if (empty($laravelData) && $method === 'GET' && $urlString->contains('?')) {
$laravelData = (string) $urlString->after('?');
}

if (is_string($laravelData)) {
parse_str($laravelData, $parsedData);

$laravelData = is_array($parsedData) ? $parsedData : [];
}
$laravelData = $this->parseRequestData($method, $url, $options);

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

/**
* Parse multi-part form data.
*
* @param array $data
* @return array|array[]
*/
protected function parseMultipartBodyFormat(array $data)
{
return collect($data)->map(function ($value, $key) {
return is_array($value) ? $value : ['name' => $key, 'contents' => $value];
})->values()->all();
}

/**
* Get the request data as an array so that we can attach it to the request for convenient assertions.
*
* @param string $method
* @param string $url
* @param array $options
* @return array
*/
protected function parseRequestData($method, $url, array $options)
{
$laravelData = $options[$this->bodyFormat] ?? $options['query'] ?? [];

$urlString = Str::of($url);

if (empty($laravelData) && $method === 'GET' && $urlString->contains('?')) {
$laravelData = (string) $urlString->after('?');
}

if (is_string($laravelData)) {
parse_str($laravelData, $parsedData);

$laravelData = is_array($parsedData) ? $parsedData : [];
}

return $laravelData;
}

/**
* Build the Guzzle client.
*
Expand Down Expand Up @@ -634,24 +662,4 @@ public function stub($callback)

return $this;
}

/**
* Parse multipart form data to allow for ['key' => 'value'] post data.
*
* @param array $data
*
* @return array|array[]
*/
protected function parseMultipartBodyFormat(array $data)
{
return array_map(function ($value, $key) {
// If the value is an array Guzzle with throw an InvalidArgumentException,
// so this use case is the original way of sending data, e.g. [['name' => 'name', 'contents' => 'content']]
if (is_array($value)) {
return $value;
}

return ['name' => $key, 'contents' => $value];
}, $data, array_keys($data));
}
}

0 comments on commit 1f163d4

Please sign in to comment.