module Halite::Chainable
Defined in:
lib/halite/src/halite/chainable.crwebex/extensions/chainable.cr
Instance Method Summary
-
#accept(value : String) : Halite::Client
Accept the given MIME type
-
#auth(value : String) : Halite::Client
Make a request with the given Authorization header
-
#basic_auth(user : String, pass : String) : Halite::Client
Make a request with the given Basic authorization header
-
#cookies(cookies : HTTP::Cookies) : Halite::Client
Make a request with the given cookies
-
#cookies(cookies : Hash(String, _) | NamedTuple) : Halite::Client
Make a request with the given cookies
-
#cookies(**kargs) : Halite::Client
Make a request with the given cookies
-
#delete(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil) : Halite::Response
Delete a resource
-
#delete(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil, &block : Halite::Response -> )
Delete a streaming resource
-
#endpoint(endpoint : String | URI) : Halite::Client
Adds a endpoint to the request.
-
#follow(hops : Int32, strict = Halite::Options::Follow::STRICT) : Halite::Client
Returns
Options
self with given max hops of redirect times. -
#follow(strict = Halite::Options::Follow::STRICT) : Halite::Client
Returns
Options
self with automatically following redirects. -
#get(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil) : Halite::Response
Get a resource
-
#get(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil, &block : Halite::Response -> )
Get a streaming resource
-
#head(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil) : Halite::Response
Head a resource
-
#head(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil, &block : Halite::Response -> )
Head a streaming resource
-
#headers(headers : Hash(String, _) | NamedTuple) : Halite::Client
Make a request with the given headers
-
#headers(**kargs) : Halite::Client
Make a request with the given headers
-
#logging(enable : Bool = true)
Returns
Options
self with enable or disable logging. -
#logging(logging : Halite::Logging::Abstract = Halite::Logging::Common.new)
Returns
Options
self with given the logging which it integration fromHalite::Logging
. -
#logging(format : String = "common", *, for : String = "halite", skip_request_body = false, skip_response_body = false, skip_benchmark = false, colorize = true)
Returns
Options
self with given the file with the path. -
#options(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil) : Halite::Response
Options a resource
-
#options(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil, &block : Halite::Response -> )
Options a streaming resource
-
#patch(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil) : Halite::Response
Patch a resource
-
#patch(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil, &block : Halite::Response -> )
Patch a streaming resource
-
#post(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil) : Halite::Response
Post a resource
-
#post(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil, &block : Halite::Response -> )
Post a streaming resource
-
#put(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil) : Halite::Response
Put a resource
-
#put(uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil, &block : Halite::Response -> )
Put a streaming resource
-
#request(verb : String, uri : String, options : Halite::Options | Nil = nil) : Halite::Response
Make an HTTP request with the given verb and options
-
#request(verb : String, uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil) : Halite::Response
Make an HTTP request with the given verb
-
#request(verb : String, uri : String, options : Halite::Options | Nil = nil, &block : Halite::Response -> )
Make an HTTP request with the given verb and options
-
#request(verb : String, uri : String, *, headers : Hash(String, _) | NamedTuple | Nil = nil, params : Hash(String, _) | NamedTuple | Nil = nil, form : Hash(String, _) | NamedTuple | Nil = nil, json : Hash(String, _) | NamedTuple | Nil = nil, raw : String | Nil = nil, tls : OpenSSL::SSL::Context::Client | Nil = nil, &block : Halite::Response -> )
Make an HTTP request with the given verb and options
-
#timeout(connect : Int32 | Float64 | Time::Span | Nil = nil, read : Int32 | Float64 | Time::Span | Nil = nil, write : Int32 | Float64 | Time::Span | Nil = nil)
Adds a timeout to the request.
-
#use(*features)
Turn on given the name of features.
-
#use(feature : String, **opts)
Turn on given features and its options.
-
#user_agent(value : String) : Halite::Client
Set requests user agent
Instance Method Detail
Accept the given MIME type
Halite.accept("application/json")
.get("http://httpbin.org/get")
Make a request with the given Authorization header
Halite.auth("private-token", "6abaef100b77808ceb7fe26a3bcff1d0")
.get("http://httpbin.org/get")
Make a request with the given Basic authorization header
Halite.basic_auth("icyleaf", "p@ssw0rd")
.get("http://httpbin.org/get")
See Also: http://tools.ietf.org/html/rfc2617
Make a request with the given cookies
cookies = HTTP::Cookies.from_client_headers(headers)
Halite.cookies(cookies)
.get("http://httpbin.org/get")
Make a request with the given cookies
Halite.cookies({"private-token", "6abaef100b77808ceb7fe26a3bcff1d0"})
.get("http://httpbin.org/get")
# Or
Halite.cookies({private-token: "6abaef100b77808ceb7fe26a3bcff1d0"})
.get("http://httpbin.org/get")
Make a request with the given cookies
Halite.cookies(name: "icyleaf", "gender": "male")
.get("http://httpbin.org/get")
Delete a resource
Request with form data
Halite.delete("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.delete("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.delete("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Delete a streaming resource
Halite.delete("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Adds a endpoint to the request.
Halite.endpoint("https://httpbin.org")
.get("/get")
Returns Options
self with given max hops of redirect times.
# Max hops 3 times
Halite.follow(3)
.get("http://httpbin.org/relative-redirect/3")
# Always redirect with any request methods
Halite.follow(4, strict: false)
.get("http://httpbin.org/relative-redirect/4")
Returns Options
self with automatically following redirects.
# Automatically following redirects.
Halite.follow
.get("http://httpbin.org/relative-redirect/5")
# Always redirect with any request methods
Halite.follow(strict: false)
.get("http://httpbin.org/get")
Get a resource
Halite.get("http://httpbin.org/anything", params: {
first_name: "foo",
last_name: "bar"
})
Get a streaming resource
Halite.get("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Head a resource
Halite.head("http://httpbin.org/anything", params: {
first_name: "foo",
last_name: "bar"
})
Head a streaming resource
Halite.head("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Make a request with the given headers
Halite.headers({"Content-Type", "application/json", "Connection": "keep-alive"})
.get("http://httpbin.org/get")
# Or
Halite.headers({content_type: "application/json", connection: "keep-alive"})
.get("http://httpbin.org/get")
Make a request with the given headers
Halite.headers(content_type: "application/json", connection: "keep-alive")
.get("http://httpbin.org/get")
Returns Options
self with enable or disable logging.
Enable logging
Same as call #logging
method without any argument.
Halite.logging.get("http://httpbin.org/get")
Disable logging
Halite.logging(false).get("http://httpbin.org/get")
Returns Options
self with given the logging which it integration from Halite::Logging
.
Simple logging
Halite.logging
.get("http://httpbin.org/get", params: {name: "foobar"})
=> 2018-08-28 14:33:19 +08:00 | request | POST | http://httpbin.org/post
=> 2018-08-28 14:33:21 +08:00 | response | 200 | http://httpbin.org/post | 1.61s | application/json
{ ... }
Logger configuration
By default, Halite will logging all outgoing HTTP requests and their responses(without binary stream) to STDOUT
on DEBUG level.
You can configuring the following options:
skip_request_body
: By default isfalse
.skip_response_body
: By default isfalse
.skip_benchmark
: Display elapsed time, by default isfalse
.colorize
: Enable colorize in terminal, only apply incommon
format, by default istrue
.
Halite.logging(skip_request_body: true, skip_response_body: true)
.post("http://httpbin.org/get", form: {image: File.open("halite-logo.png")})
# => 2018-08-28 14:33:19 +08:00 | request | POST | http://httpbin.org/post
# => 2018-08-28 14:33:21 +08:00 | response | 200 | http://httpbin.org/post | 1.61s | application/json
Use custom logging
Creating the custom logging by integration Halite::Logging::Abstract
abstract class.
Here has two methods must be implement: #request
and #response
.
class CustomLogger < Halite::Logging::Abstract
def request(request)
@logger.info "| >> | %s | %s %s" % [request.verb, request.uri, request.body]
end
def response(response)
@logger.info "| << | %s | %s %s" % [response.status_code, response.uri, response.content_type]
end
end
# Add to adapter list (optional)
Halite::Logging.register_adapter "custom", CustomLogger.new
Halite.logging(logging: CustomLogger.new)
.get("http://httpbin.org/get", params: {name: "foobar"})
# We can also call it use format name if you added it.
Halite.logging(format: "custom")
.get("http://httpbin.org/get", params: {name: "foobar"})
# => 2017-12-13 16:40:13 +08:00 | >> | GET | http://httpbin.org/get?name=foobar
# => 2017-12-13 16:40:15 +08:00 | << | 200 | http://httpbin.org/get?name=foobar application/json
Returns Options
self with given the file with the path.
JSON-formatted logging
Halite.logging(format: "json")
.get("http://httpbin.org/get", params: {name: "foobar"})
create a http request and log to file
Log.setup("halite.file", backend: Log::IOBackend.new(File.open("/tmp/halite.log", "a")))
Halite.logging(for: "halite.file")
.get("http://httpbin.org/get", params: {name: "foobar"})
Always create new log file and store data to JSON formatted
Log.setup("halite.file", backend: Log::IOBackend.new(File.open("/tmp/halite.log", "w"))
Halite.logging(for: "halite.file", format: "json")
.get("http://httpbin.org/get", params: {name: "foobar"})
Check the log file content: /tmp/halite.log
Options a resource
Request with form data
Halite.options("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.options("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.options("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Options a streaming resource
Halite.options("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Patch a resource
Request with form data
Halite.patch("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.patch("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.patch("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Patch a streaming resource
Halite.patch("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Post a resource
Request with form data
Halite.post("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.post("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.post("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Post a streaming resource
Halite.post("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Put a resource
Request with form data
Halite.put("http://httpbin.org/anything", form: {
first_name: "foo",
last_name: "bar"
})
Request with json data
Halite.put("http://httpbin.org/anything", json: {
first_name: "foo",
last_name: "bar"
})
Request with raw string
Halite.put("http://httpbin.org/anything", raw: "name=Peter+Lee&address=%23123+Happy+Ave&Language=C%2B%2B")
Put a streaming resource
Halite.put("http://httpbin.org/anything") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Make an HTTP request with the given verb and options
This method will be executed with oneshot request.
Halite.request("get", "http://httpbin.org/get", Halite::Options.new(
"headers" = { "user_agent" => "halite" },
"params" => { "nickname" => "foo" },
"form" => { "username" => "bar" },
)
Make an HTTP request with the given verb
Halite.request("get", "http://httpbin.org/get", {
"headers" = { "user_agent" => "halite" },
"params" => { "nickname" => "foo" },
"form" => { "username" => "bar" },
})
Make an HTTP request with the given verb and options
This method will be executed with oneshot request.
Halite.request("get", "http://httpbin.org/stream/3") do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Make an HTTP request with the given verb and options
This method will be executed with oneshot request.
Halite.request("get", "http://httpbin.org/stream/3", headers: {"user-agent" => "halite"}) do |response|
puts response.status_code
while line = response.body_io.gets
puts line
end
end
Adds a timeout to the request.
How long to wait for the server to send data before giving up, as a int, float or time span. The timeout value will be applied to both the connect and the read timeouts.
Halite.timeout(3, 3.minutes, 5)
.post("http://httpbin.org/post", form: {file: "file.txt"})
# Or
Halite.timeout(3.04, 64, 10.0)
.get("http://httpbin.org/get")
Turn on given the name of features.
Available features to review all subclasses of Halite::Feature
.
Halite.use("logging", "your-custom-feature-name")
.get("http://httpbin.org/get", params: {name: "foobar"})
Turn on given features and its options.
Available features to review all subclasses of Halite::Feature
.
Use JSON logging
Halite.use("logging", format: "json")
.get("http://httpbin.org/get", params: {name: "foobar"})
# => { ... }
Use common format logging and skip response body
Halite.use("logging", format: "common", skip_response_body: true)
.get("http://httpbin.org/get", params: {name: "foobar"})
# => 2018-08-28 14:58:26 +08:00 | request | GET | http://httpbin.org/get
# => 2018-08-28 14:58:27 +08:00 | response | 200 | http://httpbin.org/get | 615.8ms | application/json
Set requests user agent
Halite.user_agent("Custom User Agent")
.get("http://httpbin.org/get")