Skip to content

Commit 564c059

Browse files
authored
Parse query string into data array (#4)
1 parent c8a3273 commit 564c059

File tree

5 files changed

+34
-1
lines changed

5 files changed

+34
-1
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ yarn-error.log
1515
/.idea
1616
/.vscode
1717
composer.lock
18+
docker-compose.yml

Diff for: app/Models/Request.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ private function __construct($url, $method)
3030

3131
public static function create(array $data): self
3232
{
33-
$request = new self($data['url'], $data['method']);
33+
$url = parse_url($data['url']);
34+
35+
$request = new self(self::buildUrl($url), $data['method']);
36+
37+
if (isset($url['query'])) {
38+
parse_str($url['query'], $request->data);
39+
}
3440

3541
if (!empty($data['headers'])) {
3642
$request->headers = collect($data['headers'])
@@ -128,4 +134,19 @@ private static function parseData(array $data): array
128134

129135
return $data;
130136
}
137+
138+
private static function buildUrl(array $url): string
139+
{
140+
$output = $url['scheme'] . '://' . $url['host'];
141+
142+
if (isset($url['port'])) {
143+
$output .= ':' . $url['port'];
144+
}
145+
146+
if (isset($url['path'])) {
147+
$output .= $url['path'];
148+
}
149+
150+
return $output;
151+
}
131152
}

Diff for: tests/Feature/Console/Commands/CurlCommandTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function curlCommandFixtures()
3333
'Request with collapsable headers' => ['with-collapsable-headers'],
3434
'PUT request with data' => ['put-with-data'],
3535
'GET request with headers' => ['with-headers'],
36+
'GET request with query string' => ['with-query-string'],
3637
'Mailgun example request' => ['mailgun-example'],
3738
'Digital Ocean example request' => ['digital-ocean-example'],
3839
'Stripe example request' => ['stripe-example'],

Diff for: tests/fixtures/with-query-string.in

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
curl "https://api.postmarkapp.com/messages/[email protected]&count=50&offset=0&tag=welcome&status=sent&todate=2015-01-12&fromdate=2015-01-01" -X GET

Diff for: tests/fixtures/with-query-string.out

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
Http::get('https://api.postmarkapp.com/messages/outbound', [
2+
'recipient' => '[email protected]',
3+
'count' => '50',
4+
'offset' => '0',
5+
'tag' => 'welcome',
6+
'status' => 'sent',
7+
'todate' => '2015-01-12',
8+
'fromdate' => '2015-01-01',
9+
]);

0 commit comments

Comments
 (0)