Method: ShopifyCLI::AdminAPI.rest_request

Defined in:
lib/shopify_cli/admin_api.rb

.rest_request(ctx, shop:, path:, query: nil, body: nil, method: "GET", api_version: nil, token: nil) ⇒ Object

#### Parameters

  • ‘ctx`: running context from your command

  • ‘shop`: shop domain string for shop whose admin you are calling

  • ‘path`: path string (excluding prefixes and API version) for specific JSON that you are requesting

    ex. "data.json" instead of "/admin/api/unstable/data.json"
    
  • ‘body`: data string for corresponding REST request types

  • ‘method`: REST request string for the type of request; if nil, will perform GET request

  • ‘api_version`: API version string to specify version; if nil, latest will be used

  • ‘token`: shop password string for authentication to shop

#### Raises

  • http 404 will raise a ShopifyCLI::API::APIRequestNotFoundError

  • http 400..499 will raise a ShopifyCLI::API::APIRequestClientError

  • http 500..599 will raise a ShopifyCLI::API::APIRequestServerError

  • All other codes will raise ShopifyCLI::API::APIRequestUnexpectedError

#### Returns

  • ‘resp` - JSON response array

#### Example

ShopifyCLI::AdminAPI.rest_request(@ctx,
                                  shop: 'shop.myshopify.com',
                                  path: 'data.json',
                                  token: 'password')


84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/shopify_cli/admin_api.rb', line 84

def rest_request(ctx, shop:, path:, query: nil, body: nil, method: "GET", api_version: nil, token: nil)
  CLI::Kit::Util.begin do
    ShopifyCLI::DB.set(shopify_exchange_token: token) unless token.nil?
    url = URI::HTTPS.build(
      host: shop,
      path: "/admin/api/#{fetch_api_version(ctx, api_version, shop)}/#{path}",
      query: query,
    )
    resp = api_client(ctx, api_version, shop, path: path).request(url: url.to_s, body: body, method: method)
    ShopifyCLI::DB.set(shopify_exchange_token: nil) unless token.nil?
    resp
  end.retry_after(API::APIRequestUnauthorizedError) do
    ShopifyCLI::IdentityAuth.new(ctx: ctx).reauthenticate
  end
end