Class: ShopifyCLI::API

Inherits:
Object
  • Object
show all
Includes:
SmartProperties
Defined in:
lib/shopify_cli/api.rb

Direct Known Subclasses

AdminAPI, PartnersAPI, Theme::ThemeAccessAPI

Defined Under Namespace

Classes: APIRequestClientError, APIRequestError, APIRequestForbiddenError, APIRequestNotFoundError, APIRequestRetriableError, APIRequestServerError, APIRequestThrottledError, APIRequestTimeoutError, APIRequestUnauthorizedError, APIRequestUnexpectedError

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.gid_to_id(gid) ⇒ Object



32
33
34
# File 'lib/shopify_cli/api.rb', line 32

def self.gid_to_id(gid)
  gid.split("/").last
end

Instance Method Details

#query(query_name, variables: {}) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
# File 'lib/shopify_cli/api.rb', line 36

def query(query_name, variables: {})
  _, resp = request(
    body: JSON.dump(query: load_query(query_name).tr("\n", ""), variables: variables),
    url: url,
  )
  ctx.debug(resp)
  resp
rescue API::APIRequestServerError, API::APIRequestUnexpectedError => e
  ctx.puts(ctx.message("core.api.error.internal_server_error"))
  ctx.debug(ctx.message("core.api.error.internal_server_error_debug", e.message))
end

#request(url:, body: nil, headers: {}, method: "POST") ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/shopify_cli/api.rb', line 48

def request(url:, body: nil, headers: {}, method: "POST")
  CLI::Kit::Util.begin do
    uri = URI.parse(url)
    unless uri.is_a?(URI::HTTP)
      ctx.abort(ctx.message("core.api.error.invalid_url", url))
    end

    # we delay this require so as to avoid a performance hit on starting the CLI
    require "shopify_cli/http_request"
    headers = default_headers.merge(headers)
    ctx.debug("#{method} #{uri} with X-Request-Id: #{headers["X-Request-Id"]}")
    response = if method == "POST"
      HttpRequest.post(uri, body, headers)
    elsif method == "PUT"
      HttpRequest.put(uri, body, headers)
    elsif method == "GET"
      HttpRequest.get(uri, body, headers)
    elsif method == "DELETE"
      HttpRequest.delete(uri, body, headers)
    end
    case response.code.to_i
    when 200..399
      [response.code.to_i, JSON.parse(response.body), response]
    when 401
      raise APIRequestUnauthorizedError.new("#{response.code}\n#{response.body}", response: response)
    when 403
      raise APIRequestForbiddenError.new("#{response.code}\n#{response.body}", response: response)
    when 404
      raise APIRequestNotFoundError.new("#{response.code}\n#{response.body}", response: response)
    when 429
      raise APIRequestThrottledError.new("#{response.code}\n#{response.body}", response: response)
    when 400..499
      raise APIRequestClientError.new("#{response.code}\n#{response.body}", response: response)
    when 500..599
      raise APIRequestServerError.new("#{response.code}\n#{response.body}", response: response)
    else
      raise APIRequestUnexpectedError.new("#{response.code}\n#{response.body}", response: response)
    end
  rescue Errno::ETIMEDOUT, Timeout::Error
    ctx.debug("timeout in #{method} #{uri} with X-Request-Id: #{headers["X-Request-Id"]}")
    raise APIRequestTimeoutError, "Timeout"
  end.retry_after(APIRequestRetriableError, retries: 3) do |e|
    sleep(1) if e.is_a?(APIRequestThrottledError)
  end
end