Module: ShopifyApp::AdminAPI::WithTokenRefetch

Included in:
TokenExchange
Defined in:
lib/shopify_app/admin_api/with_token_refetch.rb

Instance Method Summary collapse

Instance Method Details

#with_token_refetch(session, shopify_id_token) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/shopify_app/admin_api/with_token_refetch.rb', line 6

def with_token_refetch(session, shopify_id_token)
  retrying = false if retrying.nil?
  yield
rescue ShopifyAPI::Errors::HttpResponseError => error
  if error.code != 401
    ShopifyApp::Logger.debug("Encountered error: #{error.code} - #{error.response.inspect}, re-raising")
  elsif retrying
    ShopifyApp::Logger.debug("Shopify API returned a 401 Unauthorized error that was not corrected " \
      "with token exchange, re-raising error")
  else
    retrying = true
    ShopifyApp::Logger.debug("Shopify API returned a 401 Unauthorized error, exchanging token and " \
      "retrying with new session")
    new_session = ShopifyApp::Auth::TokenExchange.perform(shopify_id_token)
    session.copy_attributes_from(new_session)
    retry
  end
  raise
end