-
-
Notifications
You must be signed in to change notification settings - Fork 159
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
Improve BaseHTTPClient #875
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,102 @@ | ||
require "http/client" | ||
|
||
class Lucky::BaseHTTPClient | ||
# A client for making HTTP requests | ||
# | ||
# Makes it easy to pass params, use Lucky route helpers, and chain header methods. | ||
abstract class Lucky::BaseHTTPClient | ||
private getter client | ||
|
||
@client : HTTP::Client | ||
|
||
def initialize(host = Lucky::Server.settings.host, port = Lucky::Server.settings.port) | ||
@client = HTTP::Client.new(host, port: port) | ||
end | ||
|
||
def get(path : String, headers : HTTP::Headers? = nil, params : HTTP::Params? = nil) | ||
if params | ||
path = path + "?#{params}" | ||
{% for method in [:get, :put, :patch, :post] %} | ||
def self.{{ method.id }}(*args, **named_args) | ||
new.{{ method.id }}(*args, **named_args) | ||
end | ||
@client.get(path, headers: headers) | ||
end | ||
{% end %} | ||
|
||
{% for method in [:put, :patch, :post] %} | ||
# Set headers for requests | ||
# | ||
# ``` | ||
# # `content_type` will be normalized to `content-type` | ||
# AppClient.new.headers(content_type: "application/json") | ||
# | ||
# # You can also use string keys if you want | ||
# AppClient.new.headers("Content-Type": "application/json") | ||
# ``` | ||
|
||
def {{method.id}}(path : String, body : Hash(String, String), headers : HTTP::Headers? = nil) | ||
@client.{{method.id}}(path, headers: headers, form: body) | ||
# The header call is chainable and returns the client: | ||
# | ||
# ``` | ||
# # content_type will be normalized to `content-type` | ||
# AppClient.new | ||
# .headers(content_type: "application/json") | ||
# .headers(accept: "text/plain") | ||
# .get("/some-path") | ||
# ``` | ||
# | ||
# You can also set up headers in `initialize` or in instance methods: | ||
# | ||
# ``` | ||
# class AppClient < Lucky::BaseHTTPClient | ||
# def initialize | ||
# headers(content_type: "application/json") | ||
# end | ||
# | ||
# def accept_plain_text | ||
# headers(accept: "text/plain") | ||
# end | ||
# end | ||
# | ||
# AppClient.new | ||
# .accept_plain_text | ||
# .get("/some-path") | ||
# ``` | ||
def headers(**header_values) | ||
@client.before_request do |request| | ||
header_values.each do |key, value| | ||
request.headers[key.to_s.gsub("-", "_")] = value.to_s | ||
end | ||
end | ||
self | ||
end | ||
|
||
{% end %} | ||
# Sends a request with the path and method from a Lucky::Action | ||
# | ||
# ``` | ||
# # Make a request without body params | ||
# AppClient.new.exec Users::Index | ||
# | ||
# # Make a request with body params | ||
# AppClient.new.exec Users::Create, user: {email: "[email protected]"} | ||
# | ||
# # Actions that require path params work like normal | ||
# AppClient.new.exec Users::Show.with(user.id) | ||
# ``` | ||
def exec(action : Lucky::Action.class, **params) : HTTP::Client::Response | ||
exec(action.route, params) | ||
end | ||
|
||
def delete(path : String, headers : HTTP::Headers? = nil) | ||
@client.delete(path, headers: headers) | ||
# See docs for `exec` | ||
def exec(route_helper : Lucky::RouteHelper, **params) : HTTP::Client::Response | ||
exec(route_helper, params) | ||
end | ||
|
||
# See docs for `exec` | ||
def exec(route_helper : Lucky::RouteHelper, params : NamedTuple) : HTTP::Client::Response | ||
@client.exec(method: route_helper.method.to_s.upcase, path: route_helper.path, body: params.to_json) | ||
end | ||
|
||
{% for method in [:put, :patch, :post, :delete, :get] %} | ||
def {{ method.id }}(path : String, **params) : HTTP::Client::Response | ||
{{ method.id }}(path, params) | ||
end | ||
|
||
def {{ method.id }}(path : String, params : NamedTuple) : HTTP::Client::Response | ||
@client.{{ method.id }}(path, form: params.to_json) | ||
end | ||
{% end %} | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you're trying to say:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I can't find this anywhere. Where does this happen? I see that headers have
upcase
called on them but I don't understand how_
->-
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Headers are case insensitive! https://stackoverflow.com/questions/5258977/are-http-headers-case-sensitive So technically it does not capitalize them, but it does change the underscore to a hyphen. That's done by Crystal :D
Should I add a note that headers are case insensitive to the docs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
However it doesn't appear to change the underscore to a hyphen when setting (it does when getting the header value or the test would fail):
I think most implementations of header parsers convert underscore to hyphen (like Crystal's does) but I think we should do it before setting just in case :)
https://github.com/crystal-lang/crystal/blob/5e6a1b672a8b06d2a819210b9905b806ec3d7c71/src/http/headers.cr#L3-L4