Skip to content

Commit

Permalink
Support --data-raw (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonmccreary authored Oct 13, 2022
1 parent c011a2e commit 20bab4d
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 14 deletions.
3 changes: 2 additions & 1 deletion src/Console/Commands/CurlCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

class CurlCommand extends Command
{
protected $signature = 'shift:curl {--X|request=} {--G|get} {--H|header=*} {--d|data=*} {--data-urlencode=*} {--F|form=*} {--digest} {--basic} {--connect-timeout=} {--max-timeout=} {--retry=} {--s|silent} {--u|user=} {--L|location} {--compressed} {--insecure} {url}';
protected $signature = 'shift:curl {--X|request=} {--G|get} {--H|header=*} {--d|data=*} {--data-urlencode=*} {--data-raw=*} {--F|form=*} {--digest} {--basic} {--connect-timeout=} {--max-timeout=} {--retry=} {--s|silent} {--u|user=} {--L|location} {--compressed} {--insecure} {url}';

protected $description = 'Convert a UNIX curl request to an HTTP Client request';

Expand All @@ -30,6 +30,7 @@ private function gatherOptions()
'url' => $this->argument('url'),
'headers' => $this->option('header'),
'data' => $this->option('data'),
'rawData' => $this->option('data-raw'),
'dataUrlEncode' => $this->option('data-urlencode'),
'fields' => $this->option('form'),
'digest' => $this->option('digest'),
Expand Down
29 changes: 19 additions & 10 deletions src/Models/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Request

private array $data = [];

private bool $jsonData = false;
private bool $rawData = false;

private bool $multipartFormData = false;

Expand Down Expand Up @@ -52,12 +52,25 @@ public static function create(array $data): self
->all();
}

if (!empty($data['dataUrlEncode'])) {
$request->data = array_merge($request->data, self::parseData($data['dataUrlEncode']));
}

if (!empty($data['rawData'])) {
if (count($data['rawData']) === 1 && empty($data['data']) && empty($data['dataUrlEncode'])) {
$request->data = $data['rawData'];
$request->rawData = true;
} else {
$request->data = array_merge($request->data, self::parseData($data['rawData']));
}
}

if (!empty($data['data'])) {
if (count($data['data']) === 1 && Str::startsWith($data['data'][0], '{')) {
$request->data = $data['data'];
$request->jsonData = true;
$request->rawData = true;
} else {
$request->data = self::parseData($data['data']);
$request->data = array_merge($request->data, self::parseData($data['data']));
}
}

Expand All @@ -66,11 +79,7 @@ public static function create(array $data): self
$request->multipartFormData = true;
}

if (!empty($data['dataUrlEncode'])) {
$request->data = self::parseData($data['dataUrlEncode']);
}

if (is_null($data['method']) && (!empty($data['data']) || !empty($data['fields']))) {
if (is_null($data['method']) && (!empty($data['rawData']) || !empty($data['data']) || !empty($data['fields']))) {
$request->method = 'POST';
}

Expand Down Expand Up @@ -114,9 +123,9 @@ public function headers(): array
return $this->headers;
}

public function isJsonData(): bool
public function isRawData(): bool
{
return $this->jsonData;
return $this->rawData;
}

public function method(): string
Expand Down
6 changes: 3 additions & 3 deletions src/Support/HttpCall.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ private static function generateOptions(Request $request): string
if (!empty($request->data()) && $request->method() !== 'GET') {
if ($request->isMultipartFormData()) {
$options[] = 'asMultipart()';
} elseif ($request->isJsonData()) {
$options[] = 'withBody(\'' . $request->data()[0] . '\')';
} elseif ($request->isRawData()) {
$options[] = 'withBody(\'' . current($request->data()) . '\')';
} else {
$options[] = 'asForm()';
}
Expand Down Expand Up @@ -102,7 +102,7 @@ private static function generateOptions(Request $request): string

private static function generateRequest(Request $request): string
{
if (empty($request->data()) || $request->isJsonData()) {
if (empty($request->data()) || $request->isRawData()) {
return "'" . $request->url() . "'";
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Feature/Console/Commands/CurlCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ public function curlCommandFixtures()
'Missing URL scheme' => ['missing-url-scheme'],
'GET request with compressed flag' => ['with-compressed-option'],
'GET request with insecure flag' => ['with-insecure-option'],
'Request with raw data' => ['with-raw-data'],
'POST request with mixed data' => ['raw-data-mixed'],
];
}

Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/raw-data-mixed.in
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
curl --request POST 'https://api.com' --header 'Accept: application/json' --data-raw 'some=data' -d foo=bar
6 changes: 6 additions & 0 deletions tests/fixtures/raw-data-mixed.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Http::asForm()
->acceptJson()
->post('https://api.com', [
'some' => 'data',
'foo' => 'bar',
]);
7 changes: 7 additions & 0 deletions tests/fixtures/with-raw-data.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
curl 'https://api.com' --header 'Accept: application/json' --header 'Content-Type: application/json' --data-raw '{
"messages": [
"a",
"b",
"c"
]
}'
9 changes: 9 additions & 0 deletions tests/fixtures/with-raw-data.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Http::withBody('{
"messages": [
"a",
"b",
"c"
]
}')
->acceptJson()
->post('https://api.com');

0 comments on commit 20bab4d

Please sign in to comment.