Module: Setsuzoku::ApiStrategy

Extended by:
Forwardable, T::Helpers, T::Sig
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

Instance Method Summary collapse

Instance Attribute Details

#current_actionObject

Returns the value of attribute current_action.



16
17
18
# File 'lib/setsuzoku/api_strategy.rb', line 16

def current_action
  @current_action
end

#serviceObject

Returns the value of attribute service.



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

def service
  @service
end

Instance Method Details

#call_external_api(request:, strategy: nil, **options) ⇒ Object



67
68
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
# File 'lib/setsuzoku/api_strategy.rb', line 67

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)
  exception = T.let(nil, T.nilable(Setsuzoku::Exception))
  action_details = case strategy
                   when :auth
                     { actions: self.plugin.auth_actions, url: self.plugin.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 (request.action == :refresh_token || 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

    {
        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)
end

#finalApiStrategy

Initialize the auth_strategy and provide reference to service.

Parameters:

  • service (Service)

    the new instance of service with its correct strategies.

Returns:



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

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



37
38
39
40
41
# File 'lib/setsuzoku/api_strategy.rb', line 37

def initialize(service:, **args)
  #TODO: here we need to assign credentials etc, I think.
  self.service = service
  self
end

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



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

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

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



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

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