Class: ShopifyCLI::Theme::ThemeAdminAPI

Inherits:
Object
  • Object
show all
Defined in:
lib/shopify_cli/theme/theme_admin_api.rb

Constant Summary collapse

API_VERSION =
"unstable"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ctx, shop = nil) ⇒ ThemeAdminAPI

Returns a new instance of ThemeAdminAPI.



12
13
14
15
# File 'lib/shopify_cli/theme/theme_admin_api.rb', line 12

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

Instance Attribute Details

#shopObject (readonly)

Returns the value of attribute shop.



10
11
12
# File 'lib/shopify_cli/theme/theme_admin_api.rb', line 10

def shop
  @shop
end

Instance Method Details

#delete(path:, **args, &block) ⇒ Object



29
30
31
# File 'lib/shopify_cli/theme/theme_admin_api.rb', line 29

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

#get(path:, **args, &block) ⇒ Object



17
18
19
# File 'lib/shopify_cli/theme/theme_admin_api.rb', line 17

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

#get_shop_or_abortObject

rubocop:disable Naming/AccessorMethodName



33
34
35
# File 'lib/shopify_cli/theme/theme_admin_api.rb', line 33

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

#post(path:, **args, &block) ⇒ Object



25
26
27
# File 'lib/shopify_cli/theme/theme_admin_api.rb', line 25

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

#put(path:, **args, &block) ⇒ Object



21
22
23
# File 'lib/shopify_cli/theme/theme_admin_api.rb', line 21

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

#rest_request(**args) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/shopify_cli/theme/theme_admin_api.rb', line 37

def rest_request(**args)
  status, body, response = api_client.rest_request(
    @ctx,
    shop: @shop,
    api_version: API_VERSION,
    **args.compact
  )
  return yield(status, body, response) if block_given?

  [status, body, response]
rescue ShopifyCLI::API::APIRequestForbiddenError,
       ShopifyCLI::API::APIRequestUnauthorizedError => error

  ##
  # The Admin API returns 403 Forbidden responses on different
  # scenarios:
  #
  # * when a user doesn't have permissions for a request:
  #   - <APIRequestForbiddenError: 403 {}>
  #   - <APIRequestForbiddenError: 403 {"errors":"Unauthorized Access"}>
  #
  # * when an asset operation cannot be performed:
  #   - <APIRequestForbiddenError: 403 {"message":"templates/gift_card.liquid could not be deleted"}>
  #
  if empty_response?(error)
    return permission_error
  elsif unauthorized_response?(error)
    @ctx.debug("[#{self.class}] (#{error.class}) cause: #{response_body(error)}")
    raise ShopifyCLI::Abort, @ctx.message("theme.unauthorized_error", @shop)
  end

  raise error
end