wcc_ministries_client 1.0.2 → 1.0.4.rc1

Sign up to get free protection for your applications and to get access to all the features.
@@ -15,7 +15,7 @@ require 'json'
15
15
  require 'logger'
16
16
  require 'tempfile'
17
17
  require 'time'
18
- require 'typhoeus'
18
+ require 'faraday'
19
19
 
20
20
  module WCC::Ministries::Client
21
21
  class ApiClient
@@ -47,26 +47,29 @@ module WCC::Ministries::Client
47
47
  # @return [Array<(Object, Integer, Hash)>] an array of 3 elements:
48
48
  # the data deserialized from response body (could be nil), response status code and response headers.
49
49
  def call_api(http_method, path, opts = {})
50
- request = build_request(http_method, path, opts)
51
- response = request.run
50
+ begin
51
+ response = @config.connection.public_send(http_method.to_sym.downcase) do |req|
52
+ build_request(http_method, path, req, opts)
53
+ end
52
54
 
53
- if @config.debugging
54
- @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
55
- end
55
+ if @config.debugging
56
+ @config.logger.debug "HTTP response body ~BEGIN~\n#{response.body}\n~END~\n"
57
+ end
56
58
 
57
- unless response.success?
58
- if response.timed_out?
59
- fail ApiError.new('Connection timed out')
60
- elsif response.code == 0
61
- # Errors from libcurl will be made visible here
62
- fail ApiError.new(:code => 0,
63
- :message => response.return_message)
64
- else
65
- fail ApiError.new(:code => response.code,
66
- :response_headers => response.headers,
67
- :response_body => response.body),
68
- response.status_message
59
+ unless response.success?
60
+ if response.status == 0
61
+ # Errors from libcurl will be made visible here
62
+ fail ApiError.new(:code => 0,
63
+ :message => response.return_message)
64
+ else
65
+ fail ApiError.new(:code => response.status,
66
+ :response_headers => response.headers,
67
+ :response_body => response.body),
68
+ response.status
69
+ end
69
70
  end
71
+ rescue Faraday::TimeoutError
72
+ fail ApiError.new('Connection timed out')
70
73
  end
71
74
 
72
75
  if opts[:return_type]
@@ -74,7 +77,7 @@ module WCC::Ministries::Client
74
77
  else
75
78
  data = nil
76
79
  end
77
- return data, response.code, response.headers
80
+ return data, response.status, response.headers
78
81
  end
79
82
 
80
83
  # Builds the HTTP request
@@ -86,7 +89,7 @@ module WCC::Ministries::Client
86
89
  # @option opts [Hash] :form_params Query parameters
87
90
  # @option opts [Object] :body HTTP body (JSON/XML)
88
91
  # @return [Typhoeus::Request] A Typhoeus Request
89
- def build_request(http_method, path, opts = {})
92
+ def build_request(http_method, path, request, opts = {})
90
93
  url = build_request_url(path, opts)
91
94
  http_method = http_method.to_sym.downcase
92
95
 
@@ -94,9 +97,7 @@ module WCC::Ministries::Client
94
97
  query_params = opts[:query_params] || {}
95
98
  form_params = opts[:form_params] || {}
96
99
 
97
-
98
- # set ssl_verifyhosts option based on @config.verify_ssl_host (true/false)
99
- _verify_ssl_host = @config.verify_ssl_host ? 2 : 0
100
+ update_params_for_auth! header_params, query_params, opts[:auth_names]
100
101
 
101
102
  req_opts = {
102
103
  :method => http_method,
@@ -104,16 +105,9 @@ module WCC::Ministries::Client
104
105
  :params => query_params,
105
106
  :params_encoding => @config.params_encoding,
106
107
  :timeout => @config.timeout,
107
- :ssl_verifypeer => @config.verify_ssl,
108
- :ssl_verifyhost => _verify_ssl_host,
109
- :sslcert => @config.cert_file,
110
- :sslkey => @config.key_file,
111
108
  :verbose => @config.debugging
112
109
  }
113
110
 
114
- # set custom cert, if provided
115
- req_opts[:cainfo] = @config.ssl_ca_cert if @config.ssl_ca_cert
116
-
117
111
  if [:post, :patch, :put, :delete].include?(http_method)
118
112
  req_body = build_request_body(header_params, form_params, opts[:body])
119
113
  req_opts.update :body => req_body
@@ -121,8 +115,10 @@ module WCC::Ministries::Client
121
115
  @config.logger.debug "HTTP request body param ~BEGIN~\n#{req_body}\n~END~\n"
122
116
  end
123
117
  end
124
-
125
- request = Typhoeus::Request.new(url, req_opts)
118
+ request.headers = header_params
119
+ request.body = req_body
120
+ request.url url
121
+ request.params = query_params
126
122
  download_file(request) if opts[:return_type] == 'File'
127
123
  request
128
124
  end
@@ -135,13 +131,17 @@ module WCC::Ministries::Client
135
131
  # @return [String] HTTP body data in the form of string
136
132
  def build_request_body(header_params, form_params, body)
137
133
  # http form
138
- if header_params['Content-Type'] == 'application/x-www-form-urlencoded' ||
139
- header_params['Content-Type'] == 'multipart/form-data'
134
+ if header_params['Content-Type'] == 'application/x-www-form-urlencoded'
135
+ data = URI.encode_www_form(form_params)
136
+ elsif header_params['Content-Type'] == 'multipart/form-data'
140
137
  data = {}
141
138
  form_params.each do |key, value|
142
139
  case value
143
- when ::File, ::Array, nil
144
- # let typhoeus handle File, Array and nil parameters
140
+ when ::File, ::Tempfile
141
+ # TODO hardcode to application/octet-stream, need better way to detect content type
142
+ data[key] = Faraday::UploadIO.new(value.path, 'application/octet-stream', value.path)
143
+ when ::Array, nil
144
+ # let Faraday handle Array and nil parameters
145
145
  data[key] = value
146
146
  else
147
147
  data[key] = value.to_s
@@ -155,41 +155,12 @@ module WCC::Ministries::Client
155
155
  data
156
156
  end
157
157
 
158
- # Save response body into a file in (the defined) temporary folder, using the filename
159
- # from the "Content-Disposition" header if provided, otherwise a random filename.
160
- # The response body is written to the file in chunks in order to handle files which
161
- # size is larger than maximum Ruby String or even larger than the maximum memory a Ruby
162
- # process can use.
163
- #
164
- # @see Configuration#temp_folder_path
165
158
  def download_file(request)
166
- tempfile = nil
167
- encoding = nil
168
- request.on_headers do |response|
169
- content_disposition = response.headers['Content-Disposition']
170
- if content_disposition && content_disposition =~ /filename=/i
171
- filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
172
- prefix = sanitize_filename(filename)
173
- else
174
- prefix = 'download-'
175
- end
176
- prefix = prefix + '-' unless prefix.end_with?('-')
177
- encoding = response.body.encoding
178
- tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
179
- @tempfile = tempfile
180
- end
181
- request.on_body do |chunk|
182
- chunk.force_encoding(encoding)
183
- tempfile.write(chunk)
184
- end
185
- request.on_complete do |response|
186
- if tempfile
187
- tempfile.close
188
- @config.logger.info "Temp file written to #{tempfile.path}, please copy the file to a proper folder "\
189
- "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
190
- "will be deleted automatically with GC. It's also recommended to delete the temp file "\
191
- "explicitly with `tempfile.delete`"
192
- end
159
+ @stream = []
160
+
161
+ # handle streaming Responses
162
+ request.options.on_data = Proc.new do |chunk, overall_received_bytes|
163
+ @stream << chunk
193
164
  end
194
165
  end
195
166
 
@@ -214,7 +185,25 @@ module WCC::Ministries::Client
214
185
 
215
186
  # handle file downloading - return the File instance processed in request callbacks
216
187
  # note that response body is empty when the file is written in chunks in request on_body callback
217
- return @tempfile if return_type == 'File'
188
+ if return_type == 'File'
189
+ content_disposition = response.headers['Content-Disposition']
190
+ if content_disposition && content_disposition =~ /filename=/i
191
+ filename = content_disposition[/filename=['"]?([^'"\s]+)['"]?/, 1]
192
+ prefix = sanitize_filename(filename)
193
+ else
194
+ prefix = 'download-'
195
+ end
196
+ prefix = prefix + '-' unless prefix.end_with?('-')
197
+ encoding = body.encoding
198
+ @tempfile = Tempfile.open(prefix, @config.temp_folder_path, encoding: encoding)
199
+ @tempfile.write(@stream.join.force_encoding(encoding))
200
+ @tempfile.close
201
+ @config.logger.info "Temp file written to #{@tempfile.path}, please copy the file to a proper folder "\
202
+ "with e.g. `FileUtils.cp(tempfile.path, '/new/file/path')` otherwise the temp file "\
203
+ "will be deleted automatically with GC. It's also recommended to delete the temp file "\
204
+ "explicitly with `tempfile.delete`"
205
+ return @tempfile
206
+ end
218
207
 
219
208
  return nil if body.nil? || body.empty?
220
209
 
@@ -10,6 +10,9 @@ OpenAPI Generator version: unset
10
10
 
11
11
  =end
12
12
 
13
+ require 'faraday-http-cache'
14
+ require 'faraday/follow_redirects'
15
+
13
16
  module WCC::Ministries::Client
14
17
  class Configuration
15
18
  # Defines url scheme
@@ -98,33 +101,28 @@ module WCC::Ministries::Client
98
101
  # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
99
102
  #
100
103
  # @return [true, false]
101
- attr_accessor :verify_ssl
104
+ attr_accessor :ssl_verify
102
105
 
103
106
  ### TLS/SSL setting
104
- # Set this to false to skip verifying SSL host name
105
- # Default to true.
107
+ # Any `OpenSSL::SSL::` constant (see https://ruby-doc.org/stdlib-2.5.1/libdoc/openssl/rdoc/OpenSSL/SSL.html)
106
108
  #
107
109
  # @note Do NOT set it to false in production code, otherwise you would face multiple types of cryptographic attacks.
108
110
  #
109
- # @return [true, false]
110
- attr_accessor :verify_ssl_host
111
+ attr_accessor :ssl_verify_mode
111
112
 
112
113
  ### TLS/SSL setting
113
114
  # Set this to customize the certificate file to verify the peer.
114
115
  #
115
116
  # @return [String] the path to the certificate file
116
- #
117
- # @see The `cainfo` option of Typhoeus, `--cert` option of libcurl. Related source code:
118
- # https://github.com/typhoeus/typhoeus/blob/master/lib/typhoeus/easy_factory.rb#L145
119
- attr_accessor :ssl_ca_cert
117
+ attr_accessor :ssl_ca_file
120
118
 
121
119
  ### TLS/SSL setting
122
120
  # Client certificate file (for client certificate)
123
- attr_accessor :cert_file
121
+ attr_accessor :ssl_client_cert
124
122
 
125
123
  ### TLS/SSL setting
126
124
  # Client private key file (for client certificate)
127
- attr_accessor :key_file
125
+ attr_accessor :ssl_client_key
128
126
 
129
127
  # Set this to customize parameters encoding of array parameter with multi collectionFormat.
130
128
  # Default to nil.
@@ -136,6 +134,8 @@ module WCC::Ministries::Client
136
134
  attr_accessor :inject_format
137
135
 
138
136
  attr_accessor :force_ending_format
137
+
138
+ attr_accessor :cache_store
139
139
 
140
140
  def initialize
141
141
  @scheme = 'https'
@@ -149,15 +149,16 @@ module WCC::Ministries::Client
149
149
  @api_key_prefix = {}
150
150
  @timeout = 0
151
151
  @client_side_validation = true
152
- @verify_ssl = true
153
- @verify_ssl_host = true
154
- @params_encoding = nil
155
- @cert_file = nil
156
- @key_file = nil
152
+ @ssl_verify = true
153
+ @ssl_verify_mode = nil
154
+ @ssl_ca_file = nil
155
+ @ssl_client_cert = nil
156
+ @ssl_client_key = nil
157
157
  @debugging = false
158
158
  @inject_format = false
159
159
  @force_ending_format = false
160
160
  @logger = defined?(Rails) ? Rails.logger : Logger.new(STDOUT)
161
+ @cache_store = defined?(Rails) ? Rails.cache : nil
161
162
 
162
163
  yield(self) if block_given?
163
164
  end
@@ -233,6 +234,34 @@ module WCC::Ministries::Client
233
234
  }
234
235
  end
235
236
 
237
+ def ssl_options
238
+ {
239
+ :ca_file => ssl_ca_file,
240
+ :verify => ssl_verify,
241
+ :verify_mode => ssl_verify_mode,
242
+ :client_cert => ssl_client_cert,
243
+ :client_key => ssl_client_key
244
+ }
245
+ end
246
+
247
+ # Allows supplying a custom Faraday connection
248
+ attr_writer :connection
249
+ def connection
250
+ # new one each time unless set in configuration
251
+ @connection ||
252
+ Faraday.new(:url => base_url, :ssl => ssl_options) do |conn|
253
+ # Use Rails.cache for conditional GET requests, b/c the Ministries
254
+ # API responds much faster to conditional GETs when fresh
255
+ # requires faraday-http-cache
256
+ conn.use :http_cache, store: cache_store if cache_store
257
+
258
+ conn.adapter(Faraday.default_adapter)
259
+
260
+ # Follow redirects (requires 'faraday_middleware')
261
+ conn.response :follow_redirects
262
+ end
263
+ end
264
+
236
265
  # Returns URL based on server settings
237
266
  #
238
267
  # @param index array index of the server settings
@@ -13,7 +13,7 @@ OpenAPI Generator version: unset
13
13
  module WCC
14
14
  module Ministries
15
15
  module Client
16
- VERSION = '1.0.2'
16
+ VERSION = '1.0.4.rc1'
17
17
  end
18
18
  end
19
19
  end
data/openapitools.json CHANGED
@@ -8,6 +8,7 @@
8
8
  "generatorName": "ruby",
9
9
  "output": "#{cwd}",
10
10
  "glob": "swagger.json",
11
+ "library": "faraday",
11
12
  "additionalProperties": {
12
13
  "gemName": "wcc_ministries_client",
13
14
  "moduleName": "WCC::Ministries::Client",
Binary file
@@ -51,44 +51,6 @@ describe WCC::Ministries::Client::ApiClient do
51
51
  end
52
52
  end
53
53
 
54
- describe 'params_encoding in #build_request' do
55
- let(:config) { WCC::Ministries::Client::Configuration.new }
56
- let(:api_client) { WCC::Ministries::Client::ApiClient.new(config) }
57
-
58
- it 'defaults to nil' do
59
- expect(WCC::Ministries::Client::Configuration.default.params_encoding).to eq(nil)
60
- expect(config.params_encoding).to eq(nil)
61
-
62
- request = api_client.build_request(:get, '/test')
63
- expect(request.options[:params_encoding]).to eq(nil)
64
- end
65
-
66
- it 'can be customized' do
67
- config.params_encoding = :multi
68
- request = api_client.build_request(:get, '/test')
69
- expect(request.options[:params_encoding]).to eq(:multi)
70
- end
71
- end
72
-
73
- describe 'timeout in #build_request' do
74
- let(:config) { WCC::Ministries::Client::Configuration.new }
75
- let(:api_client) { WCC::Ministries::Client::ApiClient.new(config) }
76
-
77
- it 'defaults to 0' do
78
- expect(WCC::Ministries::Client::Configuration.default.timeout).to eq(0)
79
- expect(config.timeout).to eq(0)
80
-
81
- request = api_client.build_request(:get, '/test')
82
- expect(request.options[:timeout]).to eq(0)
83
- end
84
-
85
- it 'can be customized' do
86
- config.timeout = 100
87
- request = api_client.build_request(:get, '/test')
88
- expect(request.options[:timeout]).to eq(100)
89
- end
90
- end
91
-
92
54
  describe '#deserialize' do
93
55
  it "handles Array<Integer>" do
94
56
  api_client = WCC::Ministries::Client::ApiClient.new
data/spec/spec_helper.rb CHANGED
@@ -10,6 +10,11 @@ OpenAPI Generator version: unset
10
10
 
11
11
  =end
12
12
 
13
+ require 'simplecov'
14
+ SimpleCov.start
15
+ require 'simplecov-cobertura'
16
+ SimpleCov.formatter = SimpleCov::Formatter::CoberturaFormatter
17
+
13
18
  # load the gem
14
19
  require 'wcc_ministries_client'
15
20
 
@@ -27,7 +27,10 @@ Gem::Specification.new do |s|
27
27
  s.license = "MIT"
28
28
  s.required_ruby_version = ">= 2.4"
29
29
 
30
- s.add_runtime_dependency 'typhoeus', '~> 1.0', '>= 1.0.1'
30
+ # Expand the range of Faraday that can be used
31
+ s.add_runtime_dependency 'faraday', '>= 2.0', '< 3.0'
32
+ s.add_runtime_dependency 'faraday-http-cache', '>= 2.0', '< 3.0'
33
+ s.add_runtime_dependency 'faraday-follow_redirects'
31
34
 
32
35
  s.add_development_dependency 'rspec', '~> 3.6', '>= 3.6.0'
33
36
 
metadata CHANGED
@@ -1,35 +1,69 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wcc_ministries_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.4.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Watermark Community Church
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-10-19 00:00:00.000000000 Z
11
+ date: 2023-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
- name: typhoeus
14
+ name: faraday
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - "~>"
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ - - "<"
21
+ - !ruby/object:Gem::Version
22
+ version: '3.0'
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - ">="
18
28
  - !ruby/object:Gem::Version
19
- version: '1.0'
29
+ version: '2.0'
30
+ - - "<"
31
+ - !ruby/object:Gem::Version
32
+ version: '3.0'
33
+ - !ruby/object:Gem::Dependency
34
+ name: faraday-http-cache
35
+ requirement: !ruby/object:Gem::Requirement
36
+ requirements:
20
37
  - - ">="
21
38
  - !ruby/object:Gem::Version
22
- version: 1.0.1
39
+ version: '2.0'
40
+ - - "<"
41
+ - !ruby/object:Gem::Version
42
+ version: '3.0'
23
43
  type: :runtime
24
44
  prerelease: false
25
45
  version_requirements: !ruby/object:Gem::Requirement
26
46
  requirements:
27
- - - "~>"
47
+ - - ">="
28
48
  - !ruby/object:Gem::Version
29
- version: '1.0'
49
+ version: '2.0'
50
+ - - "<"
51
+ - !ruby/object:Gem::Version
52
+ version: '3.0'
53
+ - !ruby/object:Gem::Dependency
54
+ name: faraday-follow_redirects
55
+ requirement: !ruby/object:Gem::Requirement
56
+ requirements:
30
57
  - - ">="
31
58
  - !ruby/object:Gem::Version
32
- version: 1.0.1
59
+ version: '0'
60
+ type: :runtime
61
+ prerelease: false
62
+ version_requirements: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: '0'
33
67
  - !ruby/object:Gem::Dependency
34
68
  name: rspec
35
69
  requirement: !ruby/object:Gem::Requirement
@@ -62,6 +96,10 @@ files:
62
96
  - README.md
63
97
  - Rakefile
64
98
  - bin/gen
99
+ - coverage/.last_run.json
100
+ - coverage/.resultset.json
101
+ - coverage/.resultset.json.lock
102
+ - coverage/coverage.xml
65
103
  - docs/Asset.md
66
104
  - docs/AssetFile.md
67
105
  - docs/AssetFileDetails.md
@@ -109,9 +147,8 @@ files:
109
147
  - lib/wcc_ministries_client/models/page_relationship_data.rb
110
148
  - lib/wcc_ministries_client/version.rb
111
149
  - openapitools.json
112
- - pkg/wcc_ministries_client-1.0.0.gem
113
- - pkg/wcc_ministries_client-1.0.1.gem
114
- - pkg/wcc_ministries_client-1.0.2.gem
150
+ - pkg/wcc_ministries_client-1.0.4.gem
151
+ - pkg/wcc_ministries_client-1.0.4.rc1.gem
115
152
  - spec/api/ministry_api_spec.rb
116
153
  - spec/api_client_spec.rb
117
154
  - spec/configuration_spec.rb
@@ -152,12 +189,11 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
189
  version: '2.4'
153
190
  required_rubygems_version: !ruby/object:Gem::Requirement
154
191
  requirements:
155
- - - ">="
192
+ - - ">"
156
193
  - !ruby/object:Gem::Version
157
- version: '0'
194
+ version: 1.3.1
158
195
  requirements: []
159
- rubyforge_project:
160
- rubygems_version: 2.7.6.2
196
+ rubygems_version: 3.1.6
161
197
  signing_key:
162
198
  specification_version: 4
163
199
  summary: WCC Ministries API Ruby Gem
Binary file
Binary file
Binary file