Module: Setsuzoku::ApiStrategy

Extended by:
Forwardable, T::Helpers, T::Sig
Includes:
HasConfigContext
Included in:
Service::WebService::ApiStrategy
Defined in:
lib/setsuzoku/api_strategy.rb

Overview

The API Type Interface definition. Any ApiStrategy that implements this interface must implement all abstract methods defined by ApiStrategy.

Defines all necessary methods for handling interfacing with an external API/Service for any authentication strategy.

Instance Attribute Summary collapse

Attributes included from HasConfigContext

#config_context

Instance Method Summary collapse

Methods included from HasConfigContext

#get_from_context

Instance Attribute Details

#current_actionObject

Returns the value of attribute current_action.



18
19
20
# File 'lib/setsuzoku/api_strategy.rb', line 18

def current_action
  @current_action
end

#serviceObject

Returns the value of attribute service.



19
20
21
# File 'lib/setsuzoku/api_strategy.rb', line 19

def service
  @service
end

Instance Method Details

#call_external_api(request:, strategy: nil, **options) ⇒ 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
# File 'lib/setsuzoku/api_strategy.rb', line 69

def call_external_api(request:, strategy: nil, **options)
  self.current_action = request.action
  formatted_response = T.let(nil, T.nilable(T::Hash[Symbol, T.untyped]))
  success = T.let(false, T::Boolean)
  full_object = {}
  exception = T.let(nil, T.nilable(Setsuzoku::Exception))
  action_details = case strategy
                   when :auth
                     { actions: self.auth_strategy.credential.auth_actions, url: self.auth_strategy.credential.auth_base_url }
                   when :webhook
                     { actions: self.plugin.api_actions, url: self.plugin.webhook_base_url }
                   else
                     { actions: self.plugin.api_actions, url: self.plugin.api_base_url }
                   end
  self.external_api_handler.call_external_api_wrapper(plugin: self.plugin, request: request, action_details: action_details) do
    begin
      # raise if the token is invalid and needs refreshed, but don't raise if we are trying to get a refresh token
      raise Setsuzoku::Exception::InvalidAuthCredentials.new unless (strategy == :auth || self.auth_strategy.auth_credential_valid?)
      raw_response = self.perform_external_call(request: request, action_details: action_details, **options)
      formatted_response = self.parse_response(response: raw_response, response_type: action_details[:actions][request.action][:response_type])
      success = true
    rescue ::Exception => e
      exception = e
      self.external_api_handler.call_external_api_exception(
          plugin: self.plugin,
          request: request,
          action_details: action_details,
          options: options,
          raw_response: raw_response,
          formatted_response: formatted_response,
          exception: exception
      )
    end

    full_object = {
        success: success,
        plugin: self.plugin,
        request: request,
        options: options,
        raw_response: raw_response,
        formatted_response: formatted_response,
        exception: exception
    }
  end
  self.current_action = nil
  ApiResponse.new(data: formatted_response, success: success, full_object: full_object)
end

#finalApiStrategy

Initialize the auth_strategy and provide reference to service.

Parameters:

  • service (Service)

    the new instance of service with its correct strategies.

Returns:



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/setsuzoku/api_strategy.rb', line 27

sig(:final) do
  params(
      service: T.any(
          Setsuzoku::Service::WebService::Service,
          T.untyped
      ),
      args: T.untyped
  ).returns(T.any(
      Setsuzoku::Service::WebService::ApiStrategies::RestStrategy,
      T.untyped
  ))
end

#initialize(service:, **args) ⇒ Object



39
40
41
42
43
# File 'lib/setsuzoku/api_strategy.rb', line 39

def initialize(service:, **args)
  self.service = service
  self.config_context = args
  self
end

#parse_response(response:, **options) ⇒ Object



125
# File 'lib/setsuzoku/api_strategy.rb', line 125

def parse_response(response:, **options); end

#perform_external_call(request:, action_details:, **options) ⇒ Object



60
# File 'lib/setsuzoku/api_strategy.rb', line 60

def perform_external_call(request:, action_details:, **options); end