setsuzoku 0.10.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitattributes +2 -0
- data/.gitignore +13 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/Gemfile.lock +82 -0
- data/LICENSE.txt +21 -0
- data/README.md +93 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/setsuzoku.rb +37 -0
- data/lib/setsuzoku/api_response.rb +11 -0
- data/lib/setsuzoku/api_strategy.rb +124 -0
- data/lib/setsuzoku/auth_strategy.rb +72 -0
- data/lib/setsuzoku/credential.rb +37 -0
- data/lib/setsuzoku/exception.rb +18 -0
- data/lib/setsuzoku/external_api_handler.rb +19 -0
- data/lib/setsuzoku/pluggable.rb +87 -0
- data/lib/setsuzoku/plugin.rb +128 -0
- data/lib/setsuzoku/rspec.rb +2 -0
- data/lib/setsuzoku/rspec/dynamic_spec_helper.rb +281 -0
- data/lib/setsuzoku/service.rb +70 -0
- data/lib/setsuzoku/service/web_service.rb +21 -0
- data/lib/setsuzoku/service/web_service/api_strategies/rest_api_request.rb +17 -0
- data/lib/setsuzoku/service/web_service/api_strategies/rest_strategy.rb +155 -0
- data/lib/setsuzoku/service/web_service/api_strategy.rb +169 -0
- data/lib/setsuzoku/service/web_service/auth_strategies/basic_auth_strategy.rb +50 -0
- data/lib/setsuzoku/service/web_service/auth_strategies/o_auth_strategy.rb +173 -0
- data/lib/setsuzoku/service/web_service/auth_strategy.rb +48 -0
- data/lib/setsuzoku/service/web_service/credentials/basic_auth_credential.rb +52 -0
- data/lib/setsuzoku/service/web_service/credentials/o_auth_credential.rb +83 -0
- data/lib/setsuzoku/service/web_service/service.rb +31 -0
- data/lib/setsuzoku/utilities.rb +7 -0
- data/lib/setsuzoku/version.rb +6 -0
- data/setsuzoku.gemspec +50 -0
- data/sorbet/config +2 -0
- data/sorbet/rbi/gems/activesupport.rbi +1125 -0
- data/sorbet/rbi/gems/addressable.rbi +199 -0
- data/sorbet/rbi/gems/concurrent-ruby.rbi +1586 -0
- data/sorbet/rbi/gems/crack.rbi +62 -0
- data/sorbet/rbi/gems/faraday.rbi +615 -0
- data/sorbet/rbi/gems/hashdiff.rbi +66 -0
- data/sorbet/rbi/gems/httparty.rbi +401 -0
- data/sorbet/rbi/gems/i18n.rbi +133 -0
- data/sorbet/rbi/gems/mime-types-data.rbi +17 -0
- data/sorbet/rbi/gems/mime-types.rbi +218 -0
- data/sorbet/rbi/gems/multi_xml.rbi +35 -0
- data/sorbet/rbi/gems/multipart-post.rbi +53 -0
- data/sorbet/rbi/gems/nokogiri.rbi +1011 -0
- data/sorbet/rbi/gems/public_suffix.rbi +104 -0
- data/sorbet/rbi/gems/rake.rbi +646 -0
- data/sorbet/rbi/gems/rspec-core.rbi +1893 -0
- data/sorbet/rbi/gems/rspec-expectations.rbi +1123 -0
- data/sorbet/rbi/gems/rspec-mocks.rbi +1090 -0
- data/sorbet/rbi/gems/rspec-support.rbi +280 -0
- data/sorbet/rbi/gems/rspec.rbi +15 -0
- data/sorbet/rbi/gems/safe_yaml.rbi +124 -0
- data/sorbet/rbi/gems/thread_safe.rbi +82 -0
- data/sorbet/rbi/gems/tzinfo.rbi +406 -0
- data/sorbet/rbi/gems/webmock.rbi +532 -0
- data/sorbet/rbi/hidden-definitions/hidden.rbi +13722 -0
- data/sorbet/rbi/sorbet-typed/lib/activesupport/all/activesupport.rbi +1431 -0
- data/sorbet/rbi/sorbet-typed/lib/bundler/all/bundler.rbi +8684 -0
- data/sorbet/rbi/sorbet-typed/lib/httparty/all/httparty.rbi +427 -0
- data/sorbet/rbi/sorbet-typed/lib/minitest/all/minitest.rbi +108 -0
- data/sorbet/rbi/sorbet-typed/lib/ruby/all/open3.rbi +111 -0
- data/sorbet/rbi/sorbet-typed/lib/ruby/all/resolv.rbi +543 -0
- data/sorbet/rbi/todo.rbi +11 -0
- metadata +255 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: aef3c39e156bb15569d8bf6de134794150fa92d13d09c90ebd2353db146abe5a
|
4
|
+
data.tar.gz: 0e583bdf0d58ef9e51ff614ccb604a8ac936c0e57f6c1d310bba7ec856539ffa
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8f4a10541b26ac6e634aa9f615d636961016667c85a80d371f67df0391c8267ef7c0123bd9e2bdbd9894b0e1d0a1bfdc8573f0d7df5afa53c5e269007e7ce8df
|
7
|
+
data.tar.gz: 21fe9ec50e376486befa73d5228a953e772db4f02372bd17da83984ecad29448679fbc9cc73c02c141afbf727c4fef6a34d55827a233a75be97e84a16168effc
|
data/.gitattributes
ADDED
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at luke@rocketreferrals.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
setsuzoku (0.10.1)
|
5
|
+
activesupport (~> 5.0)
|
6
|
+
faraday (~> 0.11)
|
7
|
+
httparty (~> 0.16.4)
|
8
|
+
nokogiri (~> 1.10)
|
9
|
+
sorbet-runtime (~> 0.5)
|
10
|
+
|
11
|
+
GEM
|
12
|
+
remote: https://rubygems.org/
|
13
|
+
specs:
|
14
|
+
activesupport (5.2.4.3)
|
15
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
16
|
+
i18n (>= 0.7, < 2)
|
17
|
+
minitest (~> 5.1)
|
18
|
+
tzinfo (~> 1.1)
|
19
|
+
addressable (2.7.0)
|
20
|
+
public_suffix (>= 2.0.2, < 5.0)
|
21
|
+
concurrent-ruby (1.1.6)
|
22
|
+
crack (0.4.3)
|
23
|
+
safe_yaml (~> 1.0.0)
|
24
|
+
diff-lcs (1.3)
|
25
|
+
faraday (0.17.3)
|
26
|
+
multipart-post (>= 1.2, < 3)
|
27
|
+
hashdiff (1.0.1)
|
28
|
+
httparty (0.16.4)
|
29
|
+
mime-types (~> 3.0)
|
30
|
+
multi_xml (>= 0.5.2)
|
31
|
+
i18n (1.8.3)
|
32
|
+
concurrent-ruby (~> 1.0)
|
33
|
+
mime-types (3.3.1)
|
34
|
+
mime-types-data (~> 3.2015)
|
35
|
+
mime-types-data (3.2020.0512)
|
36
|
+
mini_portile2 (2.4.0)
|
37
|
+
minitest (5.14.1)
|
38
|
+
multi_xml (0.6.0)
|
39
|
+
multipart-post (2.1.1)
|
40
|
+
nokogiri (1.10.9)
|
41
|
+
mini_portile2 (~> 2.4.0)
|
42
|
+
public_suffix (4.0.5)
|
43
|
+
rake (10.5.0)
|
44
|
+
rspec (3.9.0)
|
45
|
+
rspec-core (~> 3.9.0)
|
46
|
+
rspec-expectations (~> 3.9.0)
|
47
|
+
rspec-mocks (~> 3.9.0)
|
48
|
+
rspec-core (3.9.2)
|
49
|
+
rspec-support (~> 3.9.3)
|
50
|
+
rspec-expectations (3.9.2)
|
51
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
52
|
+
rspec-support (~> 3.9.0)
|
53
|
+
rspec-mocks (3.9.1)
|
54
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
55
|
+
rspec-support (~> 3.9.0)
|
56
|
+
rspec-support (3.9.3)
|
57
|
+
safe_yaml (1.0.5)
|
58
|
+
sorbet (0.5.5675)
|
59
|
+
sorbet-static (= 0.5.5675)
|
60
|
+
sorbet-runtime (0.5.5786)
|
61
|
+
sorbet-static (0.5.5675-universal-darwin-14)
|
62
|
+
thread_safe (0.3.6)
|
63
|
+
tzinfo (1.2.7)
|
64
|
+
thread_safe (~> 0.1)
|
65
|
+
webmock (3.8.3)
|
66
|
+
addressable (>= 2.3.6)
|
67
|
+
crack (>= 0.3.2)
|
68
|
+
hashdiff (>= 0.4.0, < 2.0.0)
|
69
|
+
|
70
|
+
PLATFORMS
|
71
|
+
ruby
|
72
|
+
|
73
|
+
DEPENDENCIES
|
74
|
+
bundler (~> 1.17)
|
75
|
+
rake (~> 10.0)
|
76
|
+
rspec (~> 3.0)
|
77
|
+
setsuzoku!
|
78
|
+
sorbet (~> 0.5)
|
79
|
+
webmock (~> 3.8)
|
80
|
+
|
81
|
+
BUNDLED WITH
|
82
|
+
1.17.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Luke Stadtler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
# Setsuzoku
|
2
|
+
We found ourselves writing lots of 3rd-party integration code
|
3
|
+
that started to look really similar (lots of common API call, credential management and error handling logic).
|
4
|
+
Additionally, we found that our application code was becoming too
|
5
|
+
strongly coupled to a specific 3rd-party implementation. Heavily relying on the structures
|
6
|
+
and conventions of a payment platform for instance, making our reliance on that vendor inseverable.
|
7
|
+
|
8
|
+
Our solution was to build a generic interface layer between 3rd-party services and a ruby application.
|
9
|
+
|
10
|
+
Setsuzoku lets you build and use plugins that can generically define behaviors expected with a 3rd party service.
|
11
|
+
|
12
|
+
As an example: the first plugin we built was a mobile/sms plugin. It defines generic API actions
|
13
|
+
for the application to consume. E.g. send_message, search_local, search_toll_free. With these defined
|
14
|
+
and your application configured to use this generalized plugin API adding a 3rd-party vendor implementation becomes much cleaner.
|
15
|
+
All a new mobile/sms plugin is required to do is define its API actions, endpoints and any vendor-specific mapping required to normalize the requests and response.
|
16
|
+
This way your application can interact with a plugin agnostic of how the plugin implements its methods under the hood.
|
17
|
+
Meaning if you're using one 3rd-party vendor and want to replace it, you simply create the new plugin, implement the methods you want
|
18
|
+
and swap it out.
|
19
|
+
|
20
|
+
To ensure plugins are holding up their end of the bargain the base plugin of a business use-case, e.g. billing/sms etc
|
21
|
+
can define a spec for each API action it wants implemented. If a plugin implements this action it must
|
22
|
+
satisfy the test's expectations.
|
23
|
+
|
24
|
+
Additionally, application code that relies on plugins has its data stubbed
|
25
|
+
without relying on the evaluation of the plugin's code. To ensure the stubbed data upholds its contract
|
26
|
+
we also test that the plugin's stub definition satisfy the data contract.
|
27
|
+
|
28
|
+
This ensures a base plugin has properly stubbed its data for application testing, and any implementation
|
29
|
+
plugin has properly implemented the actions it chooses to implement.
|
30
|
+
|
31
|
+
## Usage
|
32
|
+
Any class in your application can register a plugin. By registering a plugin a class has established
|
33
|
+
that is has dependence on a third-party service and would like off-load the busy work of interfacing to the plugin.
|
34
|
+
To correctly register a plugin, often, a credential must be provided, as well as plugin specific parameters.
|
35
|
+
|
36
|
+
Once registered your integration to the generic 3rd-party service should be pretty seamless and well decoupled from your application.
|
37
|
+
|
38
|
+
####Our general practice as an example is this:
|
39
|
+
|
40
|
+
First we define a base application class that will consume the plugin api actions.
|
41
|
+
This should handle all logic and be able to interface w/ all plugins such that
|
42
|
+
the code here is generic, and any changes should happen in the plugin implementations.
|
43
|
+
|
44
|
+
MobileIntegration < ApplicationRecord
|
45
|
+
# Application work to do when sending a message
|
46
|
+
def send_message(body)
|
47
|
+
#this resp is generic regardless of the plugin
|
48
|
+
resp = plugin.send_message(body)
|
49
|
+
notify_customer(resp) if resp.success
|
50
|
+
end
|
51
|
+
....
|
52
|
+
end
|
53
|
+
|
54
|
+
Then we define 2 service specific implementations that will register plugins.
|
55
|
+
You will also need to specify credential, and instance level methods here.
|
56
|
+
|
57
|
+
FirstMobileIntegration < MobileIntegration
|
58
|
+
self.register_plugin(plugin_class: Setsuzoku::FirstMobilePlugin)
|
59
|
+
end
|
60
|
+
|
61
|
+
|
62
|
+
SecondMobileIntegration < MobileIntegration
|
63
|
+
self.register_plugin(plugin_class: Setsuzoku::SecondMobilePlugin)
|
64
|
+
end
|
65
|
+
|
66
|
+
Now, as long as your application is getting an instance of the correct plugin.
|
67
|
+
E.g. your application knows when to use `FirstMobileIntegration` vs `SecondMobileIntegration`
|
68
|
+
setsuzoku plugins should handle the request mapping/request transmission/response parsing/response mapping
|
69
|
+
and return your application a nicely formatted exception handled response. Now your application can interface with
|
70
|
+
all these 3rd-parties in a simple to understand and common way.
|
71
|
+
|
72
|
+
## Installation
|
73
|
+
Add this line to your application's Gemfile:
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
gem 'setsuzoku'
|
77
|
+
```
|
78
|
+
|
79
|
+
And then execute:
|
80
|
+
```bash
|
81
|
+
$ bundle
|
82
|
+
```
|
83
|
+
|
84
|
+
Or install it yourself as:
|
85
|
+
```bash
|
86
|
+
$ gem install setsuzoku
|
87
|
+
```
|
88
|
+
|
89
|
+
## Contributing
|
90
|
+
|
91
|
+
|
92
|
+
## License
|
93
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "setsuzoku"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/setsuzoku.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
# typed: true
|
2
|
+
|
3
|
+
require 'faraday'
|
4
|
+
require 'httparty'
|
5
|
+
require 'nokogiri'
|
6
|
+
require 'sorbet-runtime'
|
7
|
+
require 'active_support'
|
8
|
+
|
9
|
+
#Define base module for orchestrating configuration of classes
|
10
|
+
module Setsuzoku
|
11
|
+
|
12
|
+
autoload :ApiResponse, 'setsuzoku/api_response'
|
13
|
+
autoload :ApiStrategy, 'setsuzoku/api_strategy'
|
14
|
+
autoload :AuthStrategy, 'setsuzoku/auth_strategy'
|
15
|
+
autoload :Credential, 'setsuzoku/credential'
|
16
|
+
autoload :Exception, 'setsuzoku/exception'
|
17
|
+
autoload :ExternalApiHandler, 'setsuzoku/external_api_handler'
|
18
|
+
autoload :Pluggable, 'setsuzoku/pluggable'
|
19
|
+
autoload :Service, 'setsuzoku/service' #needs to load before plugin
|
20
|
+
autoload :Plugin, 'setsuzoku/plugin'
|
21
|
+
autoload :Utilities, 'setsuzoku/utilities'
|
22
|
+
autoload :VERSION, 'setsuzoku/version'
|
23
|
+
|
24
|
+
@@external_api_handler = ExternalApiHandler
|
25
|
+
|
26
|
+
def self.external_api_handler
|
27
|
+
@@external_api_handler
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.external_api_handler=(val)
|
31
|
+
@@external_api_handler = val
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.configure
|
35
|
+
yield(self)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# typed: strict
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Setsuzoku
|
5
|
+
# The generic API response object that all API responses should return.
|
6
|
+
class ApiResponse < T::Struct
|
7
|
+
prop :data, T.nilable(T::Hash[Symbol, T.untyped]), default: {}
|
8
|
+
prop :success, T::Boolean, default: false
|
9
|
+
prop :error, T.nilable(String), default: nil
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,124 @@
|
|
1
|
+
# typed: false
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module Setsuzoku
|
5
|
+
# The API Type Interface definition.
|
6
|
+
# Any ApiStrategy that implements this interface must implement all abstract methods defined by ApiStrategy.
|
7
|
+
#
|
8
|
+
# Defines all necessary methods for handling interfacing with an external API/Service for any authentication strategy.
|
9
|
+
module ApiStrategy
|
10
|
+
|
11
|
+
extend Forwardable
|
12
|
+
extend T::Sig
|
13
|
+
extend T::Helpers
|
14
|
+
abstract!
|
15
|
+
|
16
|
+
attr_accessor :current_action
|
17
|
+
attr_accessor :service
|
18
|
+
def_delegators :@service, :plugin, :auth_strategy, :external_api_handler
|
19
|
+
|
20
|
+
# Initialize the auth_strategy and provide reference to service.
|
21
|
+
#
|
22
|
+
# @param service [Service] the new instance of service with its correct strategies.
|
23
|
+
#
|
24
|
+
# @return [ApiStrategy] the new instance of api_strategy
|
25
|
+
sig(:final) do
|
26
|
+
params(
|
27
|
+
service: T.any(
|
28
|
+
Setsuzoku::Service::WebService::Service,
|
29
|
+
T.untyped
|
30
|
+
),
|
31
|
+
args: T.untyped
|
32
|
+
).returns(T.any(
|
33
|
+
Setsuzoku::Service::WebService::ApiStrategies::RestStrategy,
|
34
|
+
T.untyped
|
35
|
+
))
|
36
|
+
end
|
37
|
+
def initialize(service:, **args)
|
38
|
+
#TODO: here we need to assign credentials etc, I think.
|
39
|
+
self.service = service
|
40
|
+
self
|
41
|
+
end
|
42
|
+
|
43
|
+
# Perform the external call for the external API.
|
44
|
+
# Each ApiStrategy must define how this works.
|
45
|
+
#
|
46
|
+
# It should:
|
47
|
+
# 1. Make the external request
|
48
|
+
# 2. Parse the response
|
49
|
+
# 3. Handle any bad response
|
50
|
+
# 4. Format the response
|
51
|
+
#
|
52
|
+
# @param request [APIRequest] the request object to be used for the request. Each strategy defines its request structure.
|
53
|
+
# @param action_details [Hash] the action_details for the action to execute.
|
54
|
+
# @param options [Any] any additional options needed to pass to correctly perform the request.
|
55
|
+
#
|
56
|
+
# @return [Any] the formatted response object.
|
57
|
+
sig { abstract.params(request: T.untyped, action_details: T::Hash[T.untyped, T.untyped], options: T.untyped).returns(T.untyped) }
|
58
|
+
def perform_external_call(request:, action_details:, **options); end
|
59
|
+
|
60
|
+
# Perform an external system call.
|
61
|
+
# This provides a uniform way of executing and handling responses for calls to external systems.
|
62
|
+
#
|
63
|
+
# @param request [Hash] the request hash used in the API request.
|
64
|
+
#
|
65
|
+
# @return [Hash] the response from the external system.
|
66
|
+
sig { params(request: T.untyped, strategy: T.nilable(Symbol), options: T.untyped).returns(ApiResponse) }
|
67
|
+
def call_external_api(request:, strategy: nil, **options)
|
68
|
+
self.current_action = request.action
|
69
|
+
formatted_response = T.let(nil, T.nilable(T::Hash[Symbol, T.untyped]))
|
70
|
+
success = T.let(false, T::Boolean)
|
71
|
+
exception = T.let(nil, T.nilable(Setsuzoku::Exception))
|
72
|
+
action_details = case strategy
|
73
|
+
when :auth
|
74
|
+
{ actions: self.plugin.auth_actions, url: self.plugin.auth_base_url }
|
75
|
+
when :webhook
|
76
|
+
{ actions: self.plugin.api_actions, url: self.plugin.webhook_base_url }
|
77
|
+
else
|
78
|
+
{ actions: self.plugin.api_actions, url: self.plugin.api_base_url }
|
79
|
+
end
|
80
|
+
self.external_api_handler.call_external_api_wrapper(plugin: self.plugin, request: request, action_details: action_details) do
|
81
|
+
begin
|
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?)
|
84
|
+
raw_response = self.perform_external_call(request: request, action_details: action_details, **options)
|
85
|
+
formatted_response = self.parse_response(response: raw_response, response_type: action_details[:actions][request.action][:response_type])
|
86
|
+
success = true
|
87
|
+
rescue Exception => e
|
88
|
+
exception = e
|
89
|
+
self.external_api_handler.call_external_api_exception(
|
90
|
+
plugin: self.plugin,
|
91
|
+
request: request,
|
92
|
+
action_details: action_details,
|
93
|
+
options: options,
|
94
|
+
raw_response: raw_response,
|
95
|
+
formatted_response: formatted_response,
|
96
|
+
exception: exception
|
97
|
+
)
|
98
|
+
end
|
99
|
+
|
100
|
+
{
|
101
|
+
success: success,
|
102
|
+
plugin: self.plugin,
|
103
|
+
request: request,
|
104
|
+
options: options,
|
105
|
+
raw_response: raw_response,
|
106
|
+
formatted_response: formatted_response,
|
107
|
+
exception: exception
|
108
|
+
}
|
109
|
+
end
|
110
|
+
self.current_action = nil
|
111
|
+
ApiResponse.new(data: formatted_response, success: success)
|
112
|
+
end
|
113
|
+
|
114
|
+
# Parse the response from the API for the given request.
|
115
|
+
# This should just convert JSON strings/XML/SQL rows to a formatted response Hash.
|
116
|
+
#
|
117
|
+
# @param response [Any] the response from the external request.
|
118
|
+
# @param options [Hash] the parsing options. Generally the response_type. e.g. :xml, :json
|
119
|
+
#
|
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]) }
|
122
|
+
def parse_response(response:, **options); end
|
123
|
+
end
|
124
|
+
end
|