module Setsuzoku::ApiStrategy

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.

Attributes

current_action[RW]
service[RW]

Public Class Methods

new(service:, **args) click to toggle source
# File lib/setsuzoku/api_strategy.rb, line 39
def initialize(service:, **args)
  self.service = service
  self.config_context = args
  self
end

Public Instance Methods

call_external_api(request:, strategy: nil, **options) click to toggle source
# 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)
  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

    {
        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
parse_response(response:, **options) click to toggle source
# File lib/setsuzoku/api_strategy.rb, line 124
def parse_response(response:, **options); end
perform_external_call(request:, action_details:, **options) click to toggle source
# File lib/setsuzoku/api_strategy.rb, line 60
def perform_external_call(request:, action_details:, **options); end