class AzureBlob::IdentityToken

def expired?

def expired?
  token.nil? || Time.now >= (expiration - EXPIRATION_BUFFER)
end

def exponential_backoff(error, attempt)

def exponential_backoff(error, attempt)
  EXPONENTIAL_BACKOFF[attempt -1] || raise(AzureBlob::Error.new("Exponential backoff out of bounds!"))
end

def initialize(principal_id: nil)

def initialize(principal_id: nil)
  @identity_uri = URI.parse(IDENTITY_ENDPOINT)
  params = {
    'api-version': API_VERSION,
    resource: RESOURCE_URI,
  }
  params[:principal_id] = principal_id if principal_id
  @identity_uri.query = URI.encode_www_form(params)
end

def refresh

def refresh
  headers =  { "Metadata" => "true" }
  headers["X-IDENTITY-HEADER"] = ENV["IDENTITY_HEADER"] if ENV["IDENTITY_HEADER"]
  attempt = 0
  begin
    attempt += 1
    response = JSON.parse(AzureBlob::Http.new(identity_uri, headers).get)
  rescue AzureBlob::Http::Error => error
    if should_retry?(error, attempt)
      attempt = 1 if error.status == 410
      delay = exponential_backoff(error, attempt)
      Kernel.sleep(delay)
      retry
    end
    raise
  end
  @token = response["access_token"]
  @expiration = Time.at(response["expires_on"].to_i)
end

def should_retry?(error, attempt)

def should_retry?(error, attempt)
  is_500 = error.status/500 == 1
  (is_500 || [ 404, 408, 410, 429 ].include?(error.status)) && attempt < 5
end

def to_s

def to_s
  refresh if expired?
  token
end