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

Adding put_form to HTTP::Client. Fixes #4181 #4190

Merged
merged 5 commits into from Aug 22, 2017

Conversation

jwoertink
Copy link
Contributor

This commit adds in a helper put_form method for HTTP::Client. The only real change is that it submits a "PUT" request instead of "POST". Since the code seems to be almost identical to the post_form methods, I put it in a macro which would allow for a patch_form later if that's wanted.

client = HTTP::Client.new "www.example.com"
response = client.put_form "/", "foo=bar"

request.headers["Content-type"] = "application/x-www-form-urlencoded"
exec request
end
{% for http_method in ["post", "put"] %}
Copy link
Contributor

Choose a reason for hiding this comment

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

You could also add patch to the list. (%w(post put patch))

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, I wasn't sure if that would be a wanted change or not so I left it off. I'll add it in though.

@jwoertink jwoertink mentioned this pull request Jul 31, 2017
@bararchy
Copy link
Contributor

@jwoertink if you rebase , maybe @RX14 can take a look ?

@jwoertink
Copy link
Contributor Author

yikes! I think I did that wrong... I did a fetch and rebase. Looks like it pulled in all the commits since I originally did this back in march 😝 How I do I squish all those?

@bararchy
Copy link
Contributor

@RX14 or some other maintainer can squash them in the git PR window , or you can do it (Goole is your best friend )

@straight-shoota
Copy link
Member

straight-shoota commented Jul 31, 2017

Woah, there went something terribly wrong. It seems you've rebased on a wrong commit and now all commits to master have been rebased on that. Why is the commit Adding put_form to HTTP::Client. Fixes #4181 from 24 March even twice in the history?
This doesn't need a squash but a proper clean up.
A simple solution might be to check out origin/master in a new branch, cherry-pick c40a51a and dd37a5c from the old branch (there you'll need to resolve the merge conflict) and then force push the new branch with these two commits to jwoertink:feature/http_client_put_form. That should bring things in order.

@jwoertink jwoertink force-pushed the feature/http_client_put_form branch from 481ea67 to ec049a0 Compare August 1, 2017 00:06
@jwoertink
Copy link
Contributor Author

ok, much better. Thanks @straight-shoota 😄

# ```
def {{http_method.id}}_form(path, form : String | IO, headers : HTTP::Headers? = nil) : HTTP::Client::Response
request = new_request({{http_method.upcase}}, path, headers, form)
request.headers["Content-type"] = "application/x-www-form-urlencoded"
Copy link
Member

Choose a reason for hiding this comment

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

While you're at it, could you please change all Content-type to Content-Type with capital T? Apart from this file we have it this way everywhere and that's the spelling recommended by RFC 7231.

exec request
end
{% for http_method in %w(post put patch) %}
# Executes a {{http_method.id.upcase}} with form data. The "Content-type" header is set
Copy link
Member

Choose a reason for hiding this comment

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

A) Maybe add "request" after the method type? => Executes a POST request with form data...

request.headers["Content-type"] = "application/x-www-form-urlencoded"
exec(request) do |response|
yield response
# Executes a {{http_method.id.upcase}} with form data and yields the response to the block.
Copy link
Member

Choose a reason for hiding this comment

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

dito A)

Copy link
Member

Choose a reason for hiding this comment

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

Thank you! Could you put this in, too?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oops! Missed one. Sure thing

exec request
end
{% for http_method in %w(post put patch) %}
# Executes a {{http_method.id.upcase}} with form data. The "Content-type" header is set
Copy link
Member

Choose a reason for hiding this comment

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

B) Maybe add "and returns a Response." =>... with form data and returns a `Response`.

body = HTTP::Params.encode(form)
post_form path, body, headers
end
# Executes a {{http_method.id.upcase}} with form data. The "Content-type" header is set
Copy link
Member

Choose a reason for hiding this comment

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

dito A) B)

body = HTTP::Params.encode(form)
post_form(path, body, headers) do |response|
yield response
# Executes a {{http_method.id.upcase}} with form data and yields the response to the block.
Copy link
Member

Choose a reason for hiding this comment

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

ditto A)

# end
# ```
def {{http_method.id}}_form(path, form : Hash(String, _) | NamedTuple, headers : HTTP::Headers? = nil)
body = HTTP::Params.from_hash(form)
Copy link
Member

Choose a reason for hiding this comment

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

dito C)

def self.post_form(url, form : String | IO | Hash, headers : HTTP::Headers? = nil, tls = nil) : HTTP::Client::Response
exec(url, tls) do |client, path|
client.post_form(path, form, headers)
# Executes a {{http_method.id.upcase}} with form data. The "Content-type" header is set
Copy link
Member

Choose a reason for hiding this comment

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

dito A) B)

exec(url, tls) do |client, path|
client.post_form(path, form, headers) do |response|
yield response
# Executes a {{http_method.id.upcase}} with form data and yields the response to the block.
Copy link
Member

Choose a reason for hiding this comment

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

dito A)

# response = client.{{http_method.id}}_form "/", {"foo" => "bar"}
# ```
def {{http_method.id}}_form(path, form : Hash(String, _) | NamedTuple, headers : HTTP::Headers? = nil) : HTTP::Client::Response
body = HTTP::Params.from_hash(form)
Copy link
Member

Choose a reason for hiding this comment

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

C) HTTP::Params.from_hash has been renamed to HTTP::Params.encode (#4205). That's why the specs are failing.

Copy link
Member

@straight-shoota straight-shoota left a comment

Choose a reason for hiding this comment

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

Great!

@jwoertink
Copy link
Contributor Author

So... Who do I have to buy a drink for to get this merged? 😂

@asterite asterite merged commit ef17d5d into crystal-lang:master Aug 22, 2017
@asterite asterite added this to the Next milestone Aug 22, 2017
@jwoertink
Copy link
Contributor Author

yay! Thanks guys 😂

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