dsc-auto-http-client 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 2ea1faf52af90dd502fa35d87d6996b9621cded8
4
+ data.tar.gz: d85180c31129bfe04c689f0bb8521972091cca75
5
+ SHA512:
6
+ metadata.gz: 635c5946e2b1fe8478fb57d8747a93aef5799338952b628e0cdea4aafa504dc1f6de063f218aed071a22ebd8803ef551ea0d8932bdb528d424d826305098f435
7
+ data.tar.gz: 960736547db660f82b5950e416af7d2a29428296fd2a829dec7b0b8942df050966f869c5771435f22dfb35df3e8c16aa95ac5bcf85c41293cfa3909df25049a3
@@ -0,0 +1,52 @@
1
+ $LOAD_PATH.unshift File.dirname(__FILE__)
2
+
3
+ require 'json'
4
+ require 'rest-client'
5
+ require 'uri'
6
+ require 'logger'
7
+ require 'optparse'
8
+
9
+
10
+
11
+
12
+ module Dsc
13
+
14
+ module Auto
15
+
16
+ module HttpClient
17
+
18
+
19
+ attr_accessor :host, :email, :password, :base_uri, :user_agent, :http, :content_type, :cookie, :token, :access_token, :data_files_path, :response, :parsed_response, :version, :log_level, :history, :auth_type
20
+
21
+ def set_http_client_attributes(_args = {})
22
+ @logger = Logger.new(STDOUT)
23
+ @log_level = _args[:log_level] || "INFO"
24
+ @logger.level = Logger.const_get(@log_level)
25
+
26
+ @host = _args[:host]
27
+ @version = _args[:version]
28
+ @base_uri = _args[:base_uri] || "#{@host}/api/v#{@version}"
29
+
30
+ @content_type = _args[:content_type] || :json
31
+
32
+ @user_agent = _args[:user_agent] || "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) "\
33
+ "AppleWebKit/537.36 (KHTML, like Gecko) "\
34
+ "Chrome/48.0.2564.103 Safari/537.36"
35
+
36
+ @token = _args[:token]
37
+ @cookie = _args[:cookie]
38
+ @access_token = _args[:access_token]
39
+
40
+ @user
41
+ @email = _args[:email]
42
+ @password = _args[:password]
43
+ @auth_type = _args[:auth_type]
44
+ @history = []
45
+ end
46
+ end
47
+ end
48
+ end
49
+
50
+
51
+ require_relative 'dsc_auto_http_client/http'
52
+ require_relative 'dsc_auto_http_client/parse_json'
@@ -0,0 +1,216 @@
1
+
2
+ module Dsc
3
+
4
+ module Auto
5
+
6
+ module HttpClient
7
+
8
+ def parsed_data_file(_filename)
9
+ filename = _filename.end_with?('.json') ? _filename : "#{_filename}.json"
10
+ @logger.debug("File : #{@data_files_path}/#{filename}")
11
+ parse_json(File.read("#{@data_files_path}/#{filename}"))
12
+ end
13
+
14
+ def get_header
15
+ {
16
+ :content_type => @content_type,
17
+ :accept => '*/*',
18
+ :cookies => @cookie
19
+ }
20
+ end
21
+
22
+ def get_request_header
23
+ if @access_token
24
+ {
25
+ :content_type => @content_type,
26
+ :accept => '*/*',
27
+ :authorization => "Bearer #{@access_token}",
28
+ :cookies => @cookie,
29
+ 'User-Agent' => @user_agent
30
+ }
31
+ else
32
+ get_header
33
+ end
34
+ end
35
+
36
+ # Takes a hash of params and builds a query string for api calls
37
+ # == Parameters:
38
+ # params::
39
+ # hash of params - { key_one: "ValueOne", key_two: "ValueTwo" }
40
+ # => 'key_one=ValueOne?key_two=ValueTwo'
41
+ def build_query(_params = {})
42
+ res = ''
43
+ _params.each do |key, val|
44
+ if val.kind_of?(Array)
45
+ val.each do |v|
46
+ res << "#{key}=#{v}&"
47
+ end
48
+ else
49
+ res << "#{key}=#{val}&"
50
+ end
51
+ end
52
+
53
+ res
54
+ end
55
+
56
+ def query_string_to_hash(_query_string)
57
+ Hash[CGI.parse(_query_string).map { |key, values| [key.to_sym, values[0] ||= true] }]
58
+ end
59
+
60
+ def response_has_body?(response_object)
61
+ response_object.respond_to?('body')
62
+ end
63
+
64
+ def response_has_code?(response_object)
65
+ response_object.respond_to?('code')
66
+ end
67
+
68
+
69
+ def http_request(_method, _path, _args = {})
70
+
71
+ verify_ssl = _args[:verify_ssl] == false ? false : true
72
+ response = begin
73
+
74
+ request = {
75
+ url: _path,
76
+ method: _method,
77
+ verify_ssl: verify_ssl,
78
+ }
79
+ payload = _args[:data] || _args[:payload]
80
+ request[:payload] = payload
81
+ request[:headers] = _args[:headers] || { :content_type => :json, :accept => '*/*' }
82
+
83
+ if _args[:user]
84
+ request[:user] = _args[:user]
85
+ end
86
+
87
+ if _args[:password]
88
+ request[:password] = _args[:password]
89
+ end
90
+
91
+ @logger.debug("RestClient Request : \r\n#{request}")
92
+
93
+ RestClient::Request.execute(request)
94
+
95
+ rescue RestClient::ExceptionWithResponse => e
96
+ unless _args[:surpress_call_error]
97
+ @logger.error("Dsc::Auto::HttpClient ERROR \r\n \
98
+ This may be expected if using this client to test an API\r\n\s \
99
+ Base URI : #{@base_uri}\r\n\s \
100
+ API VERSION : /api/v#{@version} \r\n\s \
101
+ METHOD : #{_method.upcase}\r\n\s \
102
+ PATH : #{_path} \r\n\s \
103
+ PARAMETERS : #{_args}\r\n\s \
104
+ HEADERS : #{request[:headers]}\r\n"
105
+ )
106
+ @logger.error(e.message)
107
+ @logger.error(e.backtrace[0..7].join("\n"))
108
+ error_code = e.response.code if response_has_code?(e.response)
109
+ error_body = e.response.body if response_has_body?(e.response)
110
+ @logger.error("ERROR API RESPONSE HEADERS - #{e.response.headers}") if e.response.headers
111
+ @logger.error("ERROR API RESPONSE CODE - #{error_code}") if error_code
112
+ @logger.error("ERROR API RESPONSE BODY - #{error_body}") if error_body
113
+ end
114
+
115
+ e.response
116
+
117
+ end
118
+
119
+ @history << response
120
+
121
+ response
122
+ end ## http_request
123
+
124
+ # A symbol declaring the http method to use must be :get, :post, :put, :delete
125
+ ### calls http method on base_uri
126
+ def call(_method, _path, _args = {})
127
+
128
+ query = nil
129
+ error_code = nil
130
+ error_body = nil
131
+
132
+ path = "#{@base_uri}#{_path}"
133
+ headers = _args[:headers] || get_request_header
134
+
135
+ payload = _args[:payload] || _args[:data]
136
+
137
+ ### if get and pass in data, create url params out of the data hash
138
+ if _method == :get && payload && payload.respond_to?(:each) && payload.length > 0
139
+ query = build_query( payload )
140
+ path << "?#{query}"
141
+ end
142
+
143
+ @logger.debug("DSC::Auto::RestClient.Call : #{query ? "GET, #{path}" :
144
+ "#{_method.upcase}, #{path}, #{_args}"}, #{headers}\r\n")
145
+
146
+ ### default args for get , delete - no payload should be just URL and headers
147
+ http_request_args = [ _method, path, { headers: headers }]
148
+ ### add data to args if post or update
149
+ if _method == :post || :put
150
+ payload = _args[:payload] || _args[:data]
151
+ if payload
152
+ http_request_args[2][:payload] = payload.to_json
153
+ end
154
+
155
+ end
156
+
157
+ if _args[:user]
158
+ http_request_args[2][:user] = _args[:user]
159
+ end
160
+
161
+ if _args[:password]
162
+ http_request_args[2][:password] = _args[:password]
163
+ end
164
+
165
+ http_request_args[2][:surpress_call_error] = _args[:surpress_call_error]
166
+
167
+ response = self.send(:http_request, *http_request_args)
168
+
169
+ @response = response
170
+ end
171
+
172
+
173
+ ### HTTP Methods to be called on @base_uri
174
+ ### _path will be appended to @base_url
175
+
176
+ def get(_path, _args = {})
177
+ call(:get, _path, _args)
178
+ end
179
+
180
+ def post(_path, _args = {})
181
+ call(:post, _path, _args)
182
+ end
183
+
184
+ def put(_path, _args = {})
185
+ call(:put, _path, _args)
186
+ end
187
+
188
+ def delete(_path, _args = {})
189
+ call(:delete, _path, _args)
190
+ end
191
+
192
+ ### HTTP methods to be called on external hosts
193
+
194
+ def get_external(_path, _args = {})
195
+ http_request(:get, _path, _args)
196
+ end
197
+
198
+ def post_external(_path, _args = {})
199
+ http_request(:post, _path, _args)
200
+ end
201
+
202
+ def put_external(_path, _args = {})
203
+ http_request(:put, _path, _args)
204
+ end
205
+
206
+ def delete_external(_path, _args = {})
207
+ http_request(:delete, _path, _args)
208
+ end
209
+
210
+
211
+
212
+ end
213
+
214
+ end
215
+
216
+ end
@@ -0,0 +1,23 @@
1
+ module Dsc
2
+ module Auto
3
+ module HttpClient
4
+
5
+ def parse_json(_json_string, _options = {})
6
+ fail_msg = _options[:failure_message] || _options[:path] || "parse_json failed trying to parse #{_json_string}"
7
+ begin
8
+ JSON.parse(_json_string)
9
+ rescue JSON::ParserError => e
10
+ full_fail_msg = "ERROR \r\n #{fail_msg} \r\n \
11
+ .parse_json expected param to be json, \
12
+ instead was #{_json_string} \r\n \
13
+ #{e.message} \r\n #{e.backtrace[0..5].join("\r\n")}"
14
+ raise(JSON::ParserError, full_fail_msg, caller )
15
+ end
16
+ end # self.parse_json
17
+
18
+
19
+
20
+
21
+ end
22
+ end
23
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dsc-auto-http-client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.8
5
+ platform: ruby
6
+ authors:
7
+ - Alex Janckila
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: logger
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rest-client
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Dollar Shave Club Automation Http Client Helper
56
+ email: alex.janckila@dollarshaveclub.com
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - lib/dsc_auto_http_client.rb
62
+ - lib/dsc_auto_http_client/http.rb
63
+ - lib/dsc_auto_http_client/parse_json.rb
64
+ homepage: https://github.com/dollarshaveclub/dsc-auto-http-client
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.6.7
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Dollar Shave Club Automation Http Client Helper
88
+ test_files: []