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

Support --data-raw #18

Merged
merged 2 commits into from
Oct 13, 2022
Merged
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 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');