Sha256: f8fc48366eb73a638f0bf813ad86d871ee14638236ac374011103195aa09adc1

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module ShopifyCLI
  module Theme
    class ThemeAdminAPI
      API_VERSION = "unstable"

      attr_reader :shop

      def initialize(ctx, shop = nil)
        @ctx = ctx
        @shop = shop || get_shop_or_abort
      end

      def get(path:, **args)
        rest_request(
          method: "GET",
          path: path,
          **args
        )
      end

      def put(path:, **args)
        rest_request(
          method: "PUT",
          path: path,
          **args
        )
      end

      def post(path:, **args)
        rest_request(
          method: "POST",
          path: path,
          **args
        )
      end

      def delete(path:, **args)
        rest_request(
          method: "DELETE",
          path: path,
          **args
        )
      end

      def get_shop_or_abort # rubocop:disable Naming/AccessorMethodName
        ShopifyCLI::AdminAPI.get_shop_or_abort(@ctx)
      end

      private

      def rest_request(**args)
        ShopifyCLI::AdminAPI.rest_request(
          @ctx,
          shop: @shop,
          api_version: API_VERSION,
          **args.compact
        )
      rescue ShopifyCLI::API::APIRequestForbiddenError,
             ShopifyCLI::API::APIRequestUnauthorizedError
        handle_permissions_error
      end

      def handle_permissions_error
        ensure_user_error = @ctx.message("theme.ensure_user_error", get_shop_or_abort)
        ensure_user_try_this = @ctx.message("theme.ensure_user_try_this")

        @ctx.abort(ensure_user_error, ensure_user_try_this)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
shopify-cli-2.14.0 lib/shopify_cli/theme/theme_admin_api.rb