abstract class Lucky::BaseHTTPClient

Overview

A client for making HTTP requests

Makes it easy to pass params, use Lucky route helpers, and chain header methods.

Defined in:

lucky/base_http_client.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from class Object

blank? : Bool blank?, present? : Bool present?

Constructor Detail

def self.new(client : HTTP::Client = build_client) #

[View source]

Class Method Detail

def self.app(app : Lucky::BaseAppServer) #

[View source]
def self.delete(*args, **named_args) #

[View source]
def self.exec(*args, **named_args) #

[View source]
def self.get(*args, **named_args) #

[View source]
def self.head(*args, **named_args) #

[View source]
def self.options(*args, **named_args) #

[View source]
def self.patch(*args, **named_args) #

[View source]
def self.post(*args, **named_args) #

[View source]
def self.put(*args, **named_args) #

[View source]

Instance Method Detail

def delete(path : String, params : NamedTuple) : HTTP::Client::Response #

[View source]
def delete(path : String, **params) : HTTP::Client::Response #

[View source]
def exec(route_helper : Lucky::RouteHelper, params : NamedTuple) : HTTP::Client::Response #

See docs for #exec


[View source]
def exec(action : Lucky::Action.class, **params) : HTTP::Client::Response #

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)

[View source]
def exec(route_helper : Lucky::RouteHelper, **params) : HTTP::Client::Response #

See docs for #exec


[View source]
def exec_raw(action : Lucky::Action.class, body : String) : HTTP::Client::Response #

#exec_raw works the same as #exec, but allows you to pass in a raw string. This is used as an escape hatch as the string could be unsafe, or formatted in a custom format.


[View source]
def exec_raw(route_helper : Lucky::RouteHelper, body : String) : HTTP::Client::Response #

See docs for #exec_raw


[View source]
def get(path : String, params : NamedTuple) : HTTP::Client::Response #

[View source]
def get(path : String, **params) : HTTP::Client::Response #

[View source]
def head(path : String, params : NamedTuple) : HTTP::Client::Response #

[View source]
def head(path : String, **params) : HTTP::Client::Response #

[View source]
def headers(**header_values) : self #

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")

[View source]
def options(path : String, params : NamedTuple) : HTTP::Client::Response #

[View source]
def options(path : String, **params) : HTTP::Client::Response #

[View source]
def patch(path : String, params : NamedTuple) : HTTP::Client::Response #

[View source]
def patch(path : String, **params) : HTTP::Client::Response #

[View source]
def post(path : String, params : NamedTuple) : HTTP::Client::Response #

[View source]
def post(path : String, **params) : HTTP::Client::Response #

[View source]
def put(path : String, params : NamedTuple) : HTTP::Client::Response #

[View source]
def put(path : String, **params) : HTTP::Client::Response #

[View source]