setsuzoku 0.10.9 → 0.11.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6c8b08a243d276dfb01612b33007ee093766cccadfadce6ef915b7c4bf9b429d
4
- data.tar.gz: c06e45bdb58caa6d110dadb67b77e962022b753e94bc694cf473cf658d75c56f
3
+ metadata.gz: 576427e8c71dfaf7306d5c7de069440bc47f4d8d9de185f3eb230a8304ea8162
4
+ data.tar.gz: 24fc28270185bca4db25ac804b783e28ec2e6c505c17e545e35625cc2695ca8e
5
5
  SHA512:
6
- metadata.gz: 83448fd5e96fc2b25d0b0532dbdea1671be0f3282fcae35ea8e2d8cf1f0e0ed7d1294411fc5d69e50dc8fe66ad40ee16c4df5fbeb74c6d1b948c7781de5d528c
7
- data.tar.gz: 05d695d1ebae47ba2e82f116926c0bdad0acce7310b4d738c3d0287d07254392fb095ef87270ab826bad7aecc2f889b676e819d598d7ebedbfaaba29ddd8a5d3
6
+ metadata.gz: 66c24bc8e924754a8b47fff0180ebd8ea73c7f42d1ddfe9694c3600850870514b6357a87f86c6b820b657e5393a081ab68d635534a2555dcc10419243540c2d7
7
+ data.tar.gz: f55aae400065ab41b8f5d98e66154f80ca044c8de60cd552b3e29fd3d8d9895d09a930bf9a3e7253d2d5ce835062a75ec89f26cde7275419bda7f0a389dc11f9
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- setsuzoku (0.10.9)
4
+ setsuzoku (0.11.5)
5
5
  activesupport (~> 5.0)
6
6
  faraday (~> 0.11)
7
7
  httparty (~> 0.16.4)
@@ -57,7 +57,7 @@ GEM
57
57
  safe_yaml (1.0.5)
58
58
  sorbet (0.5.5675)
59
59
  sorbet-static (= 0.5.5675)
60
- sorbet-runtime (0.5.5794)
60
+ sorbet-runtime (0.5.5862)
61
61
  sorbet-static (0.5.5675-universal-darwin-14)
62
62
  thread_safe (0.3.6)
63
63
  tzinfo (1.2.7)
@@ -4,7 +4,7 @@
4
4
  module Setsuzoku
5
5
  # The generic API response object that all API responses should return.
6
6
  class ApiResponse < T::Struct
7
- prop :data, T.nilable(T::Hash[Symbol, T.untyped]), default: {}
7
+ prop :data, T.nilable(T.untyped), default: {}
8
8
  prop :success, T::Boolean, default: false
9
9
  prop :error, T.nilable(String), default: nil
10
10
  end
@@ -80,7 +80,7 @@ module Setsuzoku
80
80
  self.external_api_handler.call_external_api_wrapper(plugin: self.plugin, request: request, action_details: action_details) do
81
81
  begin
82
82
  # raise if the token is invalid and needs refreshed, but don't raise if we are trying to get a refresh token
83
- raise Setsuzoku::Exception::InvalidAuthCredentials.new unless (request.action == :refresh_token || self.auth_strategy.auth_credential_valid?)
83
+ raise Setsuzoku::Exception::InvalidAuthCredentials.new unless (strategy == :auth || self.auth_strategy.auth_credential_valid?)
84
84
  raw_response = self.perform_external_call(request: request, action_details: action_details, **options)
85
85
  formatted_response = self.parse_response(response: raw_response, response_type: action_details[:actions][request.action][:response_type])
86
86
  success = true
@@ -118,7 +118,7 @@ module Setsuzoku
118
118
  # @param options [Hash] the parsing options. Generally the response_type. e.g. :xml, :json
119
119
  #
120
120
  # @return [Hash] the parsed hash of the response object.
121
- sig { abstract.params(response: T.untyped, options: T.untyped).returns(T::Hash[Symbol, T.untyped]) }
121
+ sig { abstract.params(response: T.untyped, options: T.untyped).returns(T.untyped) }
122
122
  def parse_response(response:, **options); end
123
123
  end
124
124
  end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'active_support/core_ext/class/subclasses'
4
4
  require 'active_support/inflector/methods'
5
+ require 'faraday'
6
+
5
7
  module Setsuzoku
6
8
  module DynamicSpecHelper
7
9
 
@@ -150,20 +152,21 @@ module Setsuzoku
150
152
  stub.each do |action_name, props|
151
153
  props.each do |propz|
152
154
  plugin.api_strategy.current_action = action_name
153
- default_headers = plugin.api_strategy.request_options(propz[:request_format])[:headers] || { 'Authorization' => 'Bearer stubbed_token', 'Content-Type' => "application/#{propz[:request_format]}" }
155
+ default_headers = plugin.api_strategy.request_options(propz[:request_format])[:headers] || { 'Content-Type' => "application/#{propz[:request_format]}" }
154
156
  if plugin.auth_strategy.is_a?(Setsuzoku::Service::WebService::AuthStrategies::BasicAuthStrategy)
155
157
  basic_auth = plugin.api_strategy.request_options(propz[:request_format])[:basic_auth]
156
158
  auth_header = { 'Authorization' => "Basic #{Base64.encode64("#{basic_auth[:username]}:#{basic_auth[:password]}").gsub("\n", '')}" }
157
- default_headers.merge!(auth_header)
159
+ elsif plugin.auth_strategy.is_a?(Setsuzoku::Service::WebService::AuthStrategies::OAuthStrategy)
160
+ auth_header = { 'Authorization' => "Bearer #{plugin.api_strategy.request_options(propz[:request_format])[:token]}" }
158
161
  end
162
+ default_headers.merge!(auth_header) if auth_header
159
163
  headers = propz[:headers] ? [propz[:headers]] : [default_headers]
160
164
  stub_directory ||= "#{plugin.class.plugin_namespace}/#{provider}"
165
+
161
166
  headers.each do |header|
162
- header = {
163
- 'Accept'=>'*/*',
164
- 'Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3',
165
- 'User-Agent'=>'Ruby'
166
- }.merge(header.deep_stringify_keys)
167
+ header = Faraday.new.headers
168
+ .merge({'Accept'=>'*/*','Accept-Encoding'=>'gzip;q=1.0,deflate;q=0.6,identity;q=0.3'})
169
+ .merge(header.deep_stringify_keys)
167
170
 
168
171
  if propz[:stub_data]
169
172
  # we have specified specific stub data for this action
@@ -265,9 +268,11 @@ module Setsuzoku
265
268
  url, body = plugin.api_strategy.replace_dynamic_vars(full_url: url, req_params: req_params)
266
269
  end
267
270
 
268
- case format
269
- when :json
271
+ if propz[:request_format] == :file
272
+ body = body.to_json if body.is_a?(Hash)
273
+ elsif format == :json
270
274
  #faraday sorts hash keys for some reason
275
+ body = body.to_json if body.is_a?(Hash)
271
276
  body = JSON.parse(body).sort.to_h
272
277
  end
273
278
  [url, body]
@@ -33,7 +33,7 @@ module Setsuzoku
33
33
  # media_type - 'json'
34
34
  #
35
35
  # @return [Hash] the parsed response object.
36
- sig { override.params(request: T.untyped, action_details: T::Hash[T.untyped, T.untyped], options: T.untyped).returns(Faraday::Response) }
36
+ sig { override.params(request: RestAPIRequest, action_details: T::Hash[T.untyped, T.untyped], options: T.untyped).returns(Faraday::Response) }
37
37
  def perform_external_call(request:, action_details:, **options)
38
38
  request_properties = self.get_request_properties(action_name: request.action, action_details: action_details, req_params: request.body)
39
39
  request_options = self.request_options(request_properties[:request_format], action_details[:actions][request.action])
@@ -43,11 +43,14 @@ module Setsuzoku
43
43
  @faraday = Faraday.new(url: request_properties[:formatted_full_url], request: { params_encoder: Faraday::FlatParamsEncoder }) do |faraday|
44
44
  faraday.request(:multipart) if options[:attachments].present?
45
45
  faraday.request(:url_encoded)
46
- # faraday.basic_auth(request_options[:basic_auth][:username], request_options[:basic_auth][:password]) if request_options[:basic_auth]
47
- faraday.request(:basic_auth, request_options[:basic_auth][:username], request_options[:basic_auth][:password]) if request_options[:basic_auth]
48
- # faraday.response request_properties[:response_format] if request_properties[:response_format]
49
- faraday.authorization(:Bearer, request_properties[:token]) if request_properties[:token]
50
- # request_options[:headers][:Authorization].prepend('Bearer ') if request_options[:headers][:Authorization]
46
+ #TODO: change these to faraday = self.auth_strategy.set_authorization!(request: faraday)
47
+ unless request.without_headers
48
+ if request_options[:token]
49
+ faraday.authorization(:Bearer, request_options[:token])
50
+ elsif request_options[:basic_auth]
51
+ faraday.request(:basic_auth, request_options[:basic_auth][:username], request_options[:basic_auth][:password])
52
+ end
53
+ end
51
54
  faraday.adapter Faraday.default_adapter
52
55
  end
53
56
 
@@ -60,7 +63,11 @@ module Setsuzoku
60
63
  req.body = payload
61
64
  end
62
65
  else
63
- @faraday.headers = request_options[:headers] if request_options[:headers]
66
+ if request.without_headers
67
+ @faraday.headers = {}
68
+ elsif request_options[:headers]
69
+ @faraday.headers = (@faraday.headers || {}).merge(request_options[:headers])
70
+ end
64
71
  resp = @faraday.send(request_properties[:request_method], request_properties[:formatted_full_url], full_request)
65
72
  end
66
73
  resp
@@ -81,7 +88,6 @@ module Setsuzoku
81
88
  request_method = request_method.downcase.to_sym
82
89
  request_format = action[:request_type]
83
90
  response_format = action[:response_type]
84
- token = action[:token]
85
91
  stub_data = action[:stub_data] if for_stub
86
92
  full_url = url + endpoint
87
93
  formatted_full_url, req_params = self.replace_dynamic_vars(full_url: full_url, req_params: req_params)
@@ -93,7 +99,6 @@ module Setsuzoku
93
99
  formatted_full_url: formatted_full_url,
94
100
  req_params: req_params,
95
101
  stub_data: stub_data,
96
- token: token
97
102
  }
98
103
  end
99
104
 
@@ -72,13 +72,15 @@ module Setsuzoku
72
72
  # @param options [Hash] the parsing options. Generally the response_type. e.g. :xml, :json
73
73
  #
74
74
  # @return [Hash] the parsed hash of the response object.
75
- sig { override.params(response: Faraday::Response, options: T.untyped).returns(T::Hash[Symbol, T.untyped]) }
75
+ sig { override.params(response: Faraday::Response, options: T.untyped).returns(T.untyped) }
76
76
  def parse_response(response:, **options)
77
77
  case options[:response_type]
78
78
  when :json
79
79
  JSON.parse(response.body).deep_symbolize_keys
80
80
  when :xml
81
81
  convert_xml_to_hash(response.body)
82
+ when :html
83
+ response.body
82
84
  else
83
85
  JSON.parse(response.body).deep_symbolize_keys
84
86
  end
@@ -31,9 +31,7 @@ module Setsuzoku
31
31
  sig { override.returns(T::Hash[Symbol, T.untyped]) }
32
32
  def auth_headers
33
33
  {
34
- headers: {
35
- 'Authorization': "Bearer #{self.credential.token}"
36
- }
34
+ token: self.credential.token
37
35
  }
38
36
  end
39
37
 
@@ -112,7 +110,7 @@ module Setsuzoku
112
110
  sig { params(body: T::Hash[Symbol, String], action: Symbol).void }
113
111
  def get_token!(body, action)
114
112
  success = false
115
- request = self.api_strategy.request_class.new(action: action, body: body)
113
+ request = self.api_strategy.request_class.new(action: action, body: body, without_headers: true)
116
114
 
117
115
  resp = self.api_strategy.call_external_api(request: request, strategy: :auth)
118
116
 
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Setsuzoku
5
- VERSION = '0.10.9'
5
+ VERSION = '0.11.5'
6
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: setsuzoku
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.10.9
4
+ version: 0.11.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Stadtler
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-04 00:00:00.000000000 Z
11
+ date: 2020-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler