Skip to content

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

Merged
taylorotwell merged 4 commits into
9.xfrom
feat/http-uri-templates
Feb 6, 2023
Merged

[9.x] Add uri templates to Http client#45982
taylorotwell merged 4 commits into
9.xfrom
feat/http-uri-templates

Conversation

@jbrooksuk

@jbrooksuk jbrooksuk commented Feb 6, 2023

Copy link
Copy Markdown
Member

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.

* @return \Illuminate\Http\Client\Response
*/
public function patch($url, $data = [])
public function patch(string $url, $data = [])

Copy link
Copy Markdown
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;
});

@mathieutu mathieutu Feb 6, 2023

Copy link
Copy Markdown
Contributor

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
Copy Markdown
Member

This is sick 🔥

@tvup

tvup commented Feb 9, 2023

Copy link
Copy Markdown

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

@jbrooksuk

jbrooksuk commented Feb 10, 2023

Copy link
Copy Markdown
Member Author

@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
Copy Markdown
Collaborator

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
Copy Markdown
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