Class: BaseCRM::HttpClient
- Inherits:
-
Object
- Object
- BaseCRM::HttpClient
- Defined in:
- lib/basecrm/http_client.rb
Instance Attribute Summary collapse
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
Instance Method Summary collapse
- #delete(path, params = {}, headers = {}) ⇒ Object
- #get(path, params = {}, headers = {}) ⇒ Object
-
#initialize(config) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #post(path, body = {}, headers = {}) ⇒ Object
- #put(path, body = {}, headers = {}) ⇒ Object
- #request(method, path, data = {}, headers = {}) ⇒ Object
Constructor Details
#initialize(config) ⇒ HttpClient
Returns a new instance of HttpClient.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/basecrm/http_client.rb', line 9 def initialize(config) @config = config @headers = { 'User-Agent' => config.user_agent, 'Accept' => 'application/json' } @api_version = "/v2" = { # timeout: config.timeout } [:ssl] = { verify: false } unless config.verify_ssl @client = Faraday.new(config.base_url, ) do |faraday| faraday.request :retry, faraday.use BaseCRM::Middlewares::OAuthBearerToken, config.access_token faraday.use BaseCRM::Middlewares::RaiseError faraday.response :logger, config.logger if config.debug? faraday.adapter Faraday.default_adapter end end |
Instance Attribute Details
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/basecrm/http_client.rb', line 7 def headers @headers end |
Instance Method Details
#delete(path, params = {}, headers = {}) ⇒ Object
46 47 48 |
# File 'lib/basecrm/http_client.rb', line 46 def delete(path, params={}, headers={}) request(:delete, path, params) end |
#get(path, params = {}, headers = {}) ⇒ Object
34 35 36 |
# File 'lib/basecrm/http_client.rb', line 34 def get(path, params={}, headers={}) request(:get, path, params, headers) end |
#post(path, body = {}, headers = {}) ⇒ Object
38 39 40 |
# File 'lib/basecrm/http_client.rb', line 38 def post(path, body={}, headers={}) request(:post, path, body, headers) end |
#put(path, body = {}, headers = {}) ⇒ Object
42 43 44 |
# File 'lib/basecrm/http_client.rb', line 42 def put(path, body={}, headers={}) request(:put, path, body, headers) end |
#request(method, path, data = {}, headers = {}) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/basecrm/http_client.rb', line 50 def request(method, path, data={}, headers={}) = { headers: @headers.merge(headers.to_h) } case method.to_s.downcase.to_sym when :get, :head, :delete [:query] = encode_params(data) unless data.empty? else unless data.empty? [:headers]['Content-Type'] = 'application/json' [:body] = Envelope.wrap(data) end end path = "#{@api_version}#{path}" res = instance_eval <<-RUBY, __FILE__, __LINE__ + 1 @client.#{method}(path) do |req| req.body = options[:body] if options[:body] req.headers.update(options[:headers]) req.params.update(options[:query]) if options[:query] end RUBY body = extract_body(res) @config.logger.debug body if @config.debug? && body && @config.logger [res.status, res.headers, body] rescue Faraday::ConnectionFailed => e raise ConnectionError, e. end |