Skip to content

[9.x] Add uri templates to Http client #45982

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

Merged
merged 4 commits into from
Feb 6, 2023
Merged

Conversation

jbrooksuk
Copy link
Member

@jbrooksuk jbrooksuk commented Feb 6, 2023

This PR introduces the ability to use URI templates on the Http client. We pull in guzzlehttp/uri-templates which follows the spec at https://www.rfc-editor.org/rfc/rfc6570

Http::withUrlParameters([
    'endpoint' => 'https://laravel.com',
    'page' => 'docs',
    'version' => '9.x',
    'thing' => 'validation',
])->get('{+endpoint}/{page}/{version}/{thing}');

// https://laravel.com/docs/9.x/validation

This is using guzzlehttp/uri-template behind the scenes, which provides a much wider set of support features when expanding templates.

@@ -715,7 +736,7 @@ public function post(string $url, $data = [])
* @param array $data
* @return \Illuminate\Http\Client\Response
*/
public function patch($url, $data = [])
public function patch(string $url, $data = [])
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other methods were already type-hinted, so I updated these too. Let me know if this is wrong though.

@taylorotwell taylorotwell merged commit 6d279f6 into 9.x Feb 6, 2023
@taylorotwell taylorotwell deleted the feat/http-uri-templates branch February 6, 2023 16:48
Comment on lines +458 to +460
return tap($this, function () use ($parameters) {
$this->urlParameters = $parameters;
});
Copy link
Contributor

@mathieutu mathieutu Feb 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it the same as writing

Suggested change
return tap($this, function () use ($parameters) {
$this->urlParameters = $parameters;
});
$this->urlParameters = $parameters;
return $this;

Or am I missing something?

@timacdonald
Copy link
Member

This is sick 🔥

@tvup
Copy link

tvup commented Feb 9, 2023

@jbrooksuk
What will this do if you have a url like
http://endpoint.top/model?filter={"PriceType":"Expensive"}
?

@jbrooksuk
Copy link
Member Author

jbrooksuk commented Feb 10, 2023

@tvup good question, with multiple answers.

  1. If you're not composing a Http request with URL parameters, then nothing different happens.
  2. withUrlParameters will only expand on defined parameters. Since you're not doing anything with URL parameters, nothing changes.
  3. You should really move the filter into the second argument, Http::get('http://endpoint.top/model', ['filter' => json_encode(["PriceType" => "Expensive"])]);.

@GrahamCampbell
Copy link
Member

You should not include the question mark on the end there, in bullet v3. It is added for you.

Http::get('http://endpoint.top/model', ['filter' => json_encode(['PriceType' => 'Expensive'])]);

@jbrooksuk
Copy link
Member Author

Whoops, typo!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants