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

Demonstrate Parameters for HTTP::Client #5145

Merged
merged 4 commits into from Sep 7, 2018
Merged

Demonstrate Parameters for HTTP::Client #5145

merged 4 commits into from Sep 7, 2018

Conversation

HCLarsen
Copy link
Contributor

Adding parameters to an HTTP request seems like a common use case, so some example code would be very useful in the documentation.

# params = HTTP::Params.encode({ "q" => "test"}) # => q=test
# response = HTTP::Client.get "http://www.example.com?" + params
# response.status_code # => 200
# response.body.lines.first # => "<!doctype html>"
Copy link
Member

Choose a reason for hiding this comment

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

No need to repeat this from the previous example if it's not relevant.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it'd be a good idea to keep at least one of those output lines to show symmetry with the previous example.

# ```
# require "http/client"
#
# params = HTTP::Params.encode({ "q" => "test"}) # => q=test
Copy link
Member

Choose a reason for hiding this comment

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

No space in {". Run crystal tool format, that might fix it.

Copy link
Member

Choose a reason for hiding this comment

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

This is also what caused the CI failure.

# require "http/client"
#
# params = HTTP::Params.encode({ "q" => "test"}) # => q=test
# response = HTTP::Client.get "http://www.example.com?" + params
Copy link
Member

Choose a reason for hiding this comment

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

Maybe worth having a more complex example? 2 parameters, one of which contains something that needs to be escaped? {"author" => "John Doe", "offset" => 20}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, those sound good to me.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Although I had to change the second value to a string instead of an int. "20" works, but 20 throws an error.

@RX14
Copy link
Contributor

RX14 commented Oct 20, 2017

I strongly dislike using this method for creating queries with params. The correct way is to set the params using a URI object. We have to parse any String values into a URI anyway, so we should probably have a named param for the String version which sets the params on the URI.

@HCLarsen
Copy link
Contributor Author

I like it. It's what I was advised to do when I asked about this use case in the gitter chat. I like that it keeps everything within the HTTP module. I also really like that you can take a hash, and just turn it into a URL encoded string in just one step. Very user friendly.

@RX14
Copy link
Contributor

RX14 commented Oct 20, 2017

Surely just adding a named parameter with the URL params is more friendly than manually appending the URL with ? and using HTTP::Params.encode manually.

@oprypin
Copy link
Member

oprypin commented Oct 20, 2017

Can you finally show the better replacement that you're talking about, in an example?

@HCLarsen
Copy link
Contributor Author

I don't see any params method in the URI class, so I'm not sure what you're getting at. I agree that an example would help us understand what you're suggesting.

@RX14
Copy link
Contributor

RX14 commented Oct 20, 2017

HTTP::Client.get("http://www.example.com", query_params: {"foo" => "bar"}) would be ideal but currently you need to do

uri = URI.parse("http://www.example.com")
uri.query = HTTP::Params.encode({ "q" => "test"})
HTTP::Client.get(uri)

@HCLarsen
Copy link
Contributor Author

I see how that works, but I don't think it's inherently better. Now granted, that example should be added to the documentation, but I'd place it in the URI class documentation. Quite frankly, having just looked through that documentation, I did not see anything to suggest this as possible.

@HCLarsen
Copy link
Contributor Author

HCLarsen commented Oct 20, 2017

Also @RX14 , I love the idea of HTTP::Client.get("http://www.example.com", query_params: {"foo" => "bar"}). Would you like me to add an issue for suggesting that?

@asterite
Copy link
Member

What about:

HTTP::Client.get("http://www.example.com?a=b", query_params: {"foo" => "bar"})

Or even:

HTTP::Client.get("http://www.example.com?foo=baz", query_params: {"foo" => "bar"})

That means HTTP::Client now has to parse the URI, parse the query parameters and combine them with the given ones.

Please, let's not add more and more features for no reason. In fact, can we declare a feature freeze? :-)

@HCLarsen
Copy link
Contributor Author

First, can someone tell me why the checks are failing? Not sure what I did wrong.

@HCLarsen
Copy link
Contributor Author

@asterite, in that case, I would say that the developer is doing a very bad job by writing some params in the address, and others in the params hash. With proper documentation, any good developer would know to put all their params in the params hash, and NOT in the address string. In any case, it seems like a different discussion than this PR, so I'm game for opening an issue if you guys are.

@oprypin
Copy link
Member

oprypin commented Oct 20, 2017

First, can someone tell me why the checks are failing?

crystal tool format that I mentioned earlier

@HCLarsen
Copy link
Contributor Author

Just did that, and it changed files that I haven't touched. Is it supposed to do that?

@HCLarsen
Copy link
Contributor Author

When I ran crystal tool format it changed

src/compiler/crystal/tools/init.cr
src/llvm/lib_llvm_ext.cr

As well as client.cr. Before I commit and push, can I be sure that it won't break anything?

@oprypin
Copy link
Member

oprypin commented Oct 20, 2017

Dang... you might have to run the latest formatter on the latest source code.
make
bin/crystal tool format

@HCLarsen
Copy link
Contributor Author

Make gave me an error.

Could not locate llvm-config

@oprypin
Copy link
Member

oprypin commented Oct 20, 2017

Well, basically you need to do the proper setup for compiling Crystal, in order to contribute to it. But that's not necessary for now. The tool pointed out the problem with the file you changed. Just ignore the rest and move on.

@HCLarsen
Copy link
Contributor Author

Like this?

@jkthorne
Copy link
Contributor

@HCLarsen I think the is a good example.

@HCLarsen
Copy link
Contributor Author

This is the first activity on this PR in a few months. Do you guys want to merge this?

@jkthorne
Copy link
Contributor

This looks good to me.

@jkthorne
Copy link
Contributor

@asterite I hope you dont mind me pinging you but I see you are the last one that posted a doc change. Does this look good to you?

@jkthorne
Copy link
Contributor

jkthorne commented Sep 4, 2018

@HCLarsen what is the status on this?

@HCLarsen
Copy link
Contributor Author

HCLarsen commented Sep 4, 2018

You're asking me? I fixed what I was told to fix, and yet there's been no movement on this in over a year. For all I know, the changes made to the language made since then have now made my example invalid.

@jkthorne
Copy link
Contributor

jkthorne commented Sep 5, 2018

@HCLarsen I dont know why it was not merged I would have liked more docs around this.

@RX14 can we merge this?

@HCLarsen
Copy link
Contributor Author

HCLarsen commented Sep 5, 2018

I would like more docs around anything.

@RX14 RX14 merged commit 625e76a into crystal-lang:master Sep 7, 2018
@RX14 RX14 added this to the 0.27.0 milestone Sep 7, 2018
ezrast pushed a commit to ezrast/crystal that referenced this pull request Oct 2, 2018
* Demonstrate Parameters for HTTP::Client

* Improve HTTP::Client with Params example

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

Successfully merging this pull request may close these issues.

None yet

6 participants