Module: Dsc::Auto::HttpClient
- Defined in:
- lib/dsc_auto_http_client.rb,
lib/dsc_auto_http_client/http.rb,
lib/dsc_auto_http_client/parse_json.rb
Instance Attribute Summary collapse
-
#access_token ⇒ Object
Returns the value of attribute access_token.
-
#auth_type ⇒ Object
Returns the value of attribute auth_type.
-
#base_uri ⇒ Object
Returns the value of attribute base_uri.
-
#content_type ⇒ Object
Returns the value of attribute content_type.
-
#cookie ⇒ Object
Returns the value of attribute cookie.
-
#data_files_path ⇒ Object
Returns the value of attribute data_files_path.
-
#email ⇒ Object
Returns the value of attribute email.
-
#history ⇒ Object
Returns the value of attribute history.
-
#host ⇒ Object
Returns the value of attribute host.
-
#http ⇒ Object
Returns the value of attribute http.
-
#log_level ⇒ Object
Returns the value of attribute log_level.
-
#parsed_response ⇒ Object
Returns the value of attribute parsed_response.
-
#password ⇒ Object
Returns the value of attribute password.
-
#response ⇒ Object
Returns the value of attribute response.
-
#token ⇒ Object
Returns the value of attribute token.
-
#user_agent ⇒ Object
Returns the value of attribute user_agent.
-
#version ⇒ Object
Returns the value of attribute version.
Instance Method Summary collapse
-
#build_query(_params = {}) ⇒ Object
- Takes a hash of params and builds a query string for api calls == Parameters: params
-
hash of params - { key_one: “ValueOne”, key_two: “ValueTwo” } => ‘key_one=ValueOne?key_two=ValueTwo’.
-
#call(_method, _path, _args = {}) ⇒ Object
A symbol declaring the http method to use must be :get, :post, :put, :delete calls http method on base_uri.
- #delete(_path, _args = {}) ⇒ Object
- #delete_external(_path, _args = {}) ⇒ Object
-
#get(_path, _args = {}) ⇒ Object
HTTP Methods to be called on @base_uri _path will be appended to @base_url.
-
#get_external(_path, _args = {}) ⇒ Object
HTTP methods to be called on external hosts.
- #get_header ⇒ Object
- #get_request_header ⇒ Object
- #http_request(_method, _path, _args = {}) ⇒ Object
- #parse_json(_json_string, _options = {}) ⇒ Object
- #parsed_data_file(_filename) ⇒ Object
- #post(_path, _args = {}) ⇒ Object
- #post_external(_path, _args = {}) ⇒ Object
- #put(_path, _args = {}) ⇒ Object
- #put_external(_path, _args = {}) ⇒ Object
- #query_string_to_hash(_query_string) ⇒ Object
- #response_has_body?(response_object) ⇒ Boolean
- #response_has_code?(response_object) ⇒ Boolean
- #set_http_client_attributes(_args = {}) ⇒ Object
Instance Attribute Details
#access_token ⇒ Object
Returns the value of attribute access_token.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def access_token @access_token end |
#auth_type ⇒ Object
Returns the value of attribute auth_type.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def auth_type @auth_type end |
#base_uri ⇒ Object
Returns the value of attribute base_uri.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def base_uri @base_uri end |
#content_type ⇒ Object
Returns the value of attribute content_type.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def content_type @content_type end |
#cookie ⇒ Object
Returns the value of attribute cookie.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def @cookie end |
#data_files_path ⇒ Object
Returns the value of attribute data_files_path.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def data_files_path @data_files_path end |
#email ⇒ Object
Returns the value of attribute email.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def email @email end |
#history ⇒ Object
Returns the value of attribute history.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def history @history end |
#host ⇒ Object
Returns the value of attribute host.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def host @host end |
#http ⇒ Object
Returns the value of attribute http.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def http @http end |
#log_level ⇒ Object
Returns the value of attribute log_level.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def log_level @log_level end |
#parsed_response ⇒ Object
Returns the value of attribute parsed_response.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def parsed_response @parsed_response end |
#password ⇒ Object
Returns the value of attribute password.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def password @password end |
#response ⇒ Object
Returns the value of attribute response.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def response @response end |
#token ⇒ Object
Returns the value of attribute token.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def token @token end |
#user_agent ⇒ Object
Returns the value of attribute user_agent.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def user_agent @user_agent end |
#version ⇒ Object
Returns the value of attribute version.
19 20 21 |
# File 'lib/dsc_auto_http_client.rb', line 19 def version @version end |
Instance Method Details
#build_query(_params = {}) ⇒ Object
Takes a hash of params and builds a query string for api calls
Parameters:
- params
-
hash of params - { key_one: “ValueOne”, key_two: “ValueTwo” }
> ‘key_one=ValueOne?key_two=ValueTwo’
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/dsc_auto_http_client/http.rb', line 41 def build_query(_params = {}) res = '' _params.each do |key, val| if val.kind_of?(Array) val.each do |v| res << "#{key}=#{v}&" end else res << "#{key}=#{val}&" end end res end |
#call(_method, _path, _args = {}) ⇒ Object
A symbol declaring the http method to use must be :get, :post, :put, :delete calls http method on base_uri
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/dsc_auto_http_client/http.rb', line 126 def call(_method, _path, _args = {}) query = nil error_code = nil error_body = nil path = "#{@base_uri}#{_path}" headers = _args[:headers] || get_request_header payload = _args[:payload] || _args[:data] ### if get and pass in data, create url params out of the data hash if _method == :get && payload && payload.respond_to?(:each) && payload.length > 0 query = build_query( payload ) path << "?#{query}" end @logger.debug("DSC::Auto::RestClient.Call : #{query ? "GET, #{path}" : "#{_method.upcase}, #{path}, #{_args}"}, #{headers}\r\n") ### default args for get , delete - no payload should be just URL and headers http_request_args = [ _method, path, { headers: headers }] ### add data to args if post or update if _method == :post || :put payload = _args[:payload] || _args[:data] if payload http_request_args[2][:payload] = payload.to_json end end if _args[:user] http_request_args[2][:user] = _args[:user] end if _args[:password] http_request_args[2][:password] = _args[:password] end http_request_args[2][:surpress_call_error] = _args[:surpress_call_error] response = self.send(:http_request, *http_request_args) @response = response end |
#delete(_path, _args = {}) ⇒ Object
188 189 190 |
# File 'lib/dsc_auto_http_client/http.rb', line 188 def delete(_path, _args = {}) call(:delete, _path, _args) end |
#delete_external(_path, _args = {}) ⇒ Object
206 207 208 |
# File 'lib/dsc_auto_http_client/http.rb', line 206 def delete_external(_path, _args = {}) http_request(:delete, _path, _args) end |
#get(_path, _args = {}) ⇒ Object
HTTP Methods to be called on @base_uri _path will be appended to @base_url
176 177 178 |
# File 'lib/dsc_auto_http_client/http.rb', line 176 def get(_path, _args = {}) call(:get, _path, _args) end |
#get_external(_path, _args = {}) ⇒ Object
HTTP methods to be called on external hosts
194 195 196 |
# File 'lib/dsc_auto_http_client/http.rb', line 194 def get_external(_path, _args = {}) http_request(:get, _path, _args) end |
#get_header ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/dsc_auto_http_client/http.rb', line 14 def get_header { :content_type => @content_type, :accept => '*/*', :cookies => @cookie } end |
#get_request_header ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/dsc_auto_http_client/http.rb', line 22 def get_request_header if @access_token { :content_type => @content_type, :accept => '*/*', :authorization => "Bearer #{@access_token}", :cookies => @cookie, 'User-Agent' => @user_agent } else get_header end end |
#http_request(_method, _path, _args = {}) ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 |
# File 'lib/dsc_auto_http_client/http.rb', line 69 def http_request(_method, _path, _args = {}) verify_ssl = _args[:verify_ssl] == false ? false : true response = begin request = { url: _path, method: _method, verify_ssl: verify_ssl, } payload = _args[:data] || _args[:payload] request[:payload] = payload request[:headers] = _args[:headers] || { :content_type => :json, :accept => '*/*' } if _args[:user] request[:user] = _args[:user] end if _args[:password] request[:password] = _args[:password] end @logger.debug("RestClient Request : \r\n#{request}") RestClient::Request.execute(request) rescue RestClient::ExceptionWithResponse => e unless _args[:surpress_call_error] @logger.error("Dsc::Auto::HttpClient ERROR \r\n \ This may be expected if using this client to test an API\r\n\s \ Base URI : #{@base_uri}\r\n\s \ API VERSION : /api/v#{@version} \r\n\s \ METHOD : #{_method.upcase}\r\n\s \ PATH : #{_path} \r\n\s \ PARAMETERS : #{_args}\r\n\s \ HEADERS : #{request[:headers]}\r\n" ) @logger.error(e.) @logger.error(e.backtrace[0..7].join("\n")) error_code = e.response.code if response_has_code?(e.response) error_body = e.response.body if response_has_body?(e.response) @logger.error("ERROR API RESPONSE HEADERS - #{e.response.headers}") if e.response.headers @logger.error("ERROR API RESPONSE CODE - #{error_code}") if error_code @logger.error("ERROR API RESPONSE BODY - #{error_body}") if error_body end e.response end @history << response response end |
#parse_json(_json_string, _options = {}) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/dsc_auto_http_client/parse_json.rb', line 5 def parse_json(_json_string, = {}) fail_msg = [:failure_message] || [:path] || "parse_json failed trying to parse #{_json_string}" begin JSON.parse(_json_string) rescue JSON::ParserError => e full_fail_msg = "ERROR \r\n #{fail_msg} \r\n \ .parse_json expected param to be json, \ instead was #{_json_string} \r\n \ #{e.} \r\n #{e.backtrace[0..5].join("\r\n")}" raise(JSON::ParserError, full_fail_msg, caller ) end end |
#parsed_data_file(_filename) ⇒ Object
8 9 10 11 12 |
# File 'lib/dsc_auto_http_client/http.rb', line 8 def parsed_data_file(_filename) filename = _filename.end_with?('.json') ? _filename : "#{_filename}.json" @logger.debug("File : #{@data_files_path}/#{filename}") parse_json(File.read("#{@data_files_path}/#{filename}")) end |
#post(_path, _args = {}) ⇒ Object
180 181 182 |
# File 'lib/dsc_auto_http_client/http.rb', line 180 def post(_path, _args = {}) call(:post, _path, _args) end |
#post_external(_path, _args = {}) ⇒ Object
198 199 200 |
# File 'lib/dsc_auto_http_client/http.rb', line 198 def post_external(_path, _args = {}) http_request(:post, _path, _args) end |
#put(_path, _args = {}) ⇒ Object
184 185 186 |
# File 'lib/dsc_auto_http_client/http.rb', line 184 def put(_path, _args = {}) call(:put, _path, _args) end |
#put_external(_path, _args = {}) ⇒ Object
202 203 204 |
# File 'lib/dsc_auto_http_client/http.rb', line 202 def put_external(_path, _args = {}) http_request(:put, _path, _args) end |
#query_string_to_hash(_query_string) ⇒ Object
56 57 58 |
# File 'lib/dsc_auto_http_client/http.rb', line 56 def query_string_to_hash(_query_string) Hash[CGI.parse(_query_string).map { |key, values| [key.to_sym, values[0] ||= true] }] end |
#response_has_body?(response_object) ⇒ Boolean
60 61 62 |
# File 'lib/dsc_auto_http_client/http.rb', line 60 def response_has_body?(response_object) response_object.respond_to?('body') end |
#response_has_code?(response_object) ⇒ Boolean
64 65 66 |
# File 'lib/dsc_auto_http_client/http.rb', line 64 def response_has_code?(response_object) response_object.respond_to?('code') end |
#set_http_client_attributes(_args = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/dsc_auto_http_client.rb', line 21 def set_http_client_attributes(_args = {}) @logger = Logger.new(STDOUT) @log_level = _args[:log_level] || "INFO" @logger.level = Logger.const_get(@log_level) @host = _args[:host] @version = _args[:version] @base_uri = _args[:base_uri] || "#{@host}/api/v#{@version}" @content_type = _args[:content_type] || :json @user_agent = _args[:user_agent] || "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) "\ "AppleWebKit/537.36 (KHTML, like Gecko) "\ "Chrome/48.0.2564.103 Safari/537.36" @token = _args[:token] @cookie = _args[:cookie] @access_token = _args[:access_token] @user @email = _args[:email] @password = _args[:password] @auth_type = _args[:auth_type] @history = [] end |