-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ed1e51b
commit 7e6185c
Showing
3 changed files
with
186 additions
and
45 deletions.
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 |