mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
added chainable module, a la http gem
This commit is contained in:
parent
a74f225a3b
commit
2742f4f9af
@ -12,6 +12,7 @@ require "httpx/connection"
|
||||
require "httpx/headers"
|
||||
require "httpx/request"
|
||||
require "httpx/response"
|
||||
require "httpx/chainable"
|
||||
require "httpx/client"
|
||||
|
||||
module HTTPX
|
||||
@ -40,4 +41,5 @@ module HTTPX
|
||||
end
|
||||
end
|
||||
|
||||
extend Chainable
|
||||
end
|
||||
|
69
lib/httpx/chainable.rb
Normal file
69
lib/httpx/chainable.rb
Normal file
@ -0,0 +1,69 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module HTTPX
|
||||
module Chainable
|
||||
def head(uri, **options)
|
||||
request(:head, uri, **options)
|
||||
end
|
||||
|
||||
def get(uri, **options)
|
||||
request(:get, uri, **options)
|
||||
end
|
||||
|
||||
def post(uri, **options)
|
||||
request(:post, uri, **options)
|
||||
end
|
||||
|
||||
def put(uri, **options)
|
||||
request(:put, uri, **options)
|
||||
end
|
||||
|
||||
def delete(uri, **options)
|
||||
request(:delete, uri, **options)
|
||||
end
|
||||
|
||||
def trace(uri, **options)
|
||||
request(:trace, uri, **options)
|
||||
end
|
||||
|
||||
def options(uri, **options)
|
||||
request(:options, uri, **options)
|
||||
end
|
||||
|
||||
def connect(uri, **options)
|
||||
request(:connect, uri, **options)
|
||||
end
|
||||
|
||||
def patch(uri, **options)
|
||||
request(:patch, uri, **options)
|
||||
end
|
||||
|
||||
def request(verb, uri, **options)
|
||||
branch(**options).request(verb, uri)
|
||||
end
|
||||
|
||||
def timeout(klass, **options)
|
||||
branch(timeout: Timeout.by(klass, **options))
|
||||
end
|
||||
|
||||
def headers(headers)
|
||||
branch(default_options.with_headers(headers))
|
||||
end
|
||||
|
||||
def encoding(encoding)
|
||||
branch(default_options.with_encoding(encoding))
|
||||
end
|
||||
|
||||
def accept(type)
|
||||
headers("accept" => String(type))
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
# :nodoc:
|
||||
def branch(options)
|
||||
Client.new(options)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
@ -2,6 +2,8 @@
|
||||
|
||||
module HTTPX
|
||||
class Client
|
||||
include Chainable
|
||||
|
||||
def initialize(**options)
|
||||
@default_options = self.class.default_options.merge(options)
|
||||
@connection = Connection.new(@default_options)
|
||||
|
Loading…
x
Reference in New Issue
Block a user