Sha256: 7a39c2e6204140720899122d174db9fff27bcc4869292361e5f0c4f2fdbf9ec4

Contents?: true

Size: 1.23 KB

Versions: 11

Compression:

Stored size: 1.23 KB

Contents

# frozen_string_literal: true

require "forwardable"

require_relative "theme_admin_api_throttler/bulk"
require_relative "theme_admin_api_throttler/put_request"

module ShopifyCLI
  module Theme
    class ThemeAdminAPIThrottler
      extend Forwardable

      attr_reader :bulk, :admin_api

      def_delegators :@admin_api, :get, :post, :delete

      def initialize(ctx, admin_api, active = true)
        @ctx = ctx
        @admin_api = admin_api
        @active = active
        @bulk = Bulk.new(ctx, admin_api)
      end

      def put(path:, **args, &block)
        request = PutRequest.new(path, args[:body], &block)
        if active?
          bulk_request(request)
        else
          rest_request(request)
        end
      end

      def activate_throttler!
        @active = true
      end

      def deactivate_throttler!
        @active = false
      end

      def active?
        @active
      end

      def shutdown
        bulk.shutdown
      end

      private

      def rest_request(request)
        request.block.call(admin_api.rest_request(**request.to_h))
      rescue StandardError => error
        request.block.call(500, {}, error)
      end

      def bulk_request(request)
        bulk.enqueue(request)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
shopify-cli-2.29.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.28.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.27.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.26.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.25.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.24.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.23.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.22.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.21.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.20.1 lib/shopify_cli/theme/theme_admin_api_throttler.rb
shopify-cli-2.20.0 lib/shopify_cli/theme/theme_admin_api_throttler.rb