# LINE Messaging API
# This document describes LINE Messaging API.
#
# The version of the OpenAPI document: 0.0.1
#
# NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
# https://openapi-generator.tech
# Do not edit the class manually.

require 'json'

require 'line/bot/v2/http_client'
require 'line/bot/v2/reserved_words'
require 'line/bot/v2/utils'

module Line
  module Bot
    module V2
      module ModuleAttach
        class ApiClient
          # Initializes a new {Line::Bot::V2::ModuleAttach::ApiClient} instance.
          #
          # @param base_url [String] The base URL for requests (optional).
          #   Defaults to 'https://manager.line.biz' if none is provided.
          #   You can override this for testing or to use a mock server.
          # @param channel_id [String] The channel ID for Basic authentication.
          # @param channel_secret [String] The channel secret for Basic authentication.
          # @param http_options [Hash] HTTP options (same as Net::HTTP options).
          #   See: https://docs.ruby-lang.org/en/3.4/Net/HTTP.html to understand the options.
          #
          # @example
          #   @client ||= Line::Bot::V2::ModuleAttach::ApiClient.new(
          #     channel_id: "YOUR_CHANNEL_ID",
          #     channel_secret: "YOUR_CHANNEL_SECRET",
          #     http_options: {
          #       open_timeout: 5,
          #       read_timeout: 5,
          #     }
          #   )
          def initialize(base_url: nil, channel_id:, channel_secret:, http_options: {})
            @http_client = HttpClient.new(
              base_url: base_url || 'https://manager.line.biz',
              http_headers: {
                Authorization: 'Basic ' + Base64.encode64("#{channel_id}:#{channel_secret}")
              },
              http_options: http_options
            )
          end

          # Attach by operation of the module channel provider
          # This requests to <code>POST https://manager.line.biz/module/auth/v1/token</code>
          # This returns an array containing response, HTTP status code, and header in order. Please specify all header keys in lowercase.
          #
          # @param grant_type [String] authorization_code
          # @param code [String] Authorization code received from the LINE Platform.
          # @param redirect_uri [String] Specify the redirect_uri specified in the URL for authentication and authorization.
          # @param code_verifier [String, nil] Specify when using PKCE (Proof Key for Code Exchange) defined in the OAuth 2.0 extension specification as a countermeasure against authorization code interception attacks.
          # @param client_id [String, nil] Instead of using Authorization header, you can use this parameter to specify the channel ID of the module channel. You can find the channel ID of the module channel in the LINE Developers Console. 
          # @param client_secret [String, nil] Instead of using Authorization header, you can use this parameter to specify the channel secret of the module channel. You can find the channel secret of the module channel in the LINE Developers Console. 
          # @param region [String, nil] If you specified a value for region in the URL for authentication and authorization, specify the same value. 
          # @param basic_search_id [String, nil] If you specified a value for basic_search_id in the URL for authentication and authorization, specify the same value.
          # @param scope [String, nil] If you specified a value for scope in the URL for authentication and authorization, specify the same value.
          # @param brand_type [String, nil] If you specified a value for brand_type in the URL for authentication and authorization, specify the same value.
          # @see https://developers.line.biz/en/reference/partner-docs/#link-attach-by-operation-module-channel-provider
          # @return [Array(Line::Bot::V2::ModuleAttach::AttachModuleResponse, Integer, Hash{String => String})] when HTTP status code is 200
          # @return [Array((String|nil), Integer, Hash{String => String})] when other HTTP status code is returned. String is HTTP response body itself.
          def attach_module_with_http_info( # steep:ignore MethodBodyTypeMismatch 
            grant_type:, 
            code:, 
            redirect_uri:, 
            code_verifier: nil, 
            client_id: nil, 
            client_secret: nil, 
            region: nil, 
            basic_search_id: nil, 
            scope: nil, 
            brand_type: nil
          )
            path = "/module/auth/v1/token"

            form_params = {
              "grant_type": grant_type,
              "code": code,
              "redirect_uri": redirect_uri,
              "code_verifier": code_verifier,
              "client_id": client_id,
              "client_secret": client_secret,
              "region": region,
              "basic_search_id": basic_search_id,
              "scope": scope,
              "brand_type": brand_type
            }.compact

            response = @http_client.post_form(
              path: path,
              form_params: form_params,
            )

            case response.code.to_i
            when 200
              json = Line::Bot::V2::Utils.deep_underscore(JSON.parse(response.body))
              json.transform_keys! do |key|
                Line::Bot::V2::RESERVED_WORDS.include?(key) ? "_#{key}".to_sym : key
              end
              response_body = Line::Bot::V2::ModuleAttach::AttachModuleResponse.create(json) # steep:ignore InsufficientKeywordArguments
              [response_body, 200, response.each_header.to_h]
            else
              [response.body, response.code.to_i, response.each_header.to_h]
            end
          end

          # Attach by operation of the module channel provider
          # This requests to <code>POST https://manager.line.biz/module/auth/v1/token</code>
          # When you want to get HTTP status code or response headers, use {#attach_module_with_http_info} instead of this.
          #
          # @param grant_type [String] authorization_code
          # @param code [String] Authorization code received from the LINE Platform.
          # @param redirect_uri [String] Specify the redirect_uri specified in the URL for authentication and authorization.
          # @param code_verifier [String, nil] Specify when using PKCE (Proof Key for Code Exchange) defined in the OAuth 2.0 extension specification as a countermeasure against authorization code interception attacks.
          # @param client_id [String, nil] Instead of using Authorization header, you can use this parameter to specify the channel ID of the module channel. You can find the channel ID of the module channel in the LINE Developers Console. 
          # @param client_secret [String, nil] Instead of using Authorization header, you can use this parameter to specify the channel secret of the module channel. You can find the channel secret of the module channel in the LINE Developers Console. 
          # @param region [String, nil] If you specified a value for region in the URL for authentication and authorization, specify the same value. 
          # @param basic_search_id [String, nil] If you specified a value for basic_search_id in the URL for authentication and authorization, specify the same value.
          # @param scope [String, nil] If you specified a value for scope in the URL for authentication and authorization, specify the same value.
          # @param brand_type [String, nil] If you specified a value for brand_type in the URL for authentication and authorization, specify the same value.
          # @see https://developers.line.biz/en/reference/partner-docs/#link-attach-by-operation-module-channel-provider
          # @return [Line::Bot::V2::ModuleAttach::AttachModuleResponse] when HTTP status code is 200
          # @return [String, nil] when other HTTP status code is returned. This String is HTTP response body itself.
          def attach_module(
            grant_type:,
            code:,
            redirect_uri:,
            code_verifier: nil,
            client_id: nil,
            client_secret: nil,
            region: nil,
            basic_search_id: nil,
            scope: nil,
            brand_type: nil
          )
            response_body, _status_code, _headers = attach_module_with_http_info(
              grant_type: grant_type,
              code: code,
              redirect_uri: redirect_uri,
              code_verifier: code_verifier,
              client_id: client_id,
              client_secret: client_secret,
              region: region,
              basic_search_id: basic_search_id,
              scope: scope,
              brand_type: brand_type
            )

            response_body
          end
        end
      end
    end
  end
end
