class Lark::Api

Attributes

app_id[R]
app_secret[R]
isv[R]
options[R]
tenant_key[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/lark/api.rb, line 27
def initialize(options = {})
  @app_id = options.delete(:app_id) || Lark.config.default_app_id
  @app_secret = options.delete(:app_secret) || Lark.config.default_app_secret
  raise AppNotConfigException if @app_id.nil? || @app_id.empty?

  @tenant_key = options.delete(:tenant_key)
  @isv = options.delete(:isv) || Lark.config.default_isv
  @options = options
end

Private Class Methods

default() click to toggle source
# File lib/lark/api.rb, line 108
def default
  @default ||= new(
    app_id: Lark.config.default_app_id,
    app_secret: Lark.config.default_app_secret,
    isv: Lark.config.default_isv
  )
end

Public Instance Methods

app_access_token() click to toggle source
# File lib/lark/api.rb, line 71
def app_access_token
  app_token_store.token
end
app_ticket() click to toggle source
# File lib/lark/api.rb, line 67
def app_ticket
  Lark.redis.get "APP_TICKET_#{app_id}"
end
app_ticket=(new_ticket) click to toggle source
# File lib/lark/api.rb, line 63
def app_ticket=(new_ticket)
  Lark.redis.set "APP_TICKET_#{app_id}", new_ticket
end
get(path, headers = {}) click to toggle source
# File lib/lark/api.rb, line 45
def get(path, headers = {})
  with_token(headers) do |headers_with_token|
    request.get path, headers_with_token
  end
end
post(path, payload, headers = {}) click to toggle source
# File lib/lark/api.rb, line 51
def post(path, payload, headers = {})
  with_token(headers) do |headers_with_token|
    request.post path, payload, headers_with_token
  end
end
post_file(path, file, headers = {}) click to toggle source
# File lib/lark/api.rb, line 57
def post_file(path, file, headers = {})
  with_token(headers) do |headers_with_token|
    request.post_file path, file, headers_with_token
  end
end
request() click to toggle source
# File lib/lark/api.rb, line 41
def request
  @request ||= Lark::Request.new
end
tenant_access_token() click to toggle source
# File lib/lark/api.rb, line 75
def tenant_access_token
  tenant_token_store.token
end
valid?() click to toggle source
# File lib/lark/api.rb, line 37
def valid?
  app_token_store.valid? && tenant_token_store.valid?
end

Private Instance Methods

app_token_store() click to toggle source
# File lib/lark/api.rb, line 81
def app_token_store
  return @app_token_store if defined?(@app_token_store)

  klass = isv ? TokenStore::IsvAppToken : TokenStore::AppToken
  @app_token_store = klass.new(self)
end
tenant_token_store() click to toggle source
# File lib/lark/api.rb, line 88
def tenant_token_store
  return @tenant_token_store if defined?(@tenant_token_store)

  klass = isv ? TokenStore::IsvTenantToken : TokenStore::TenantToken
  @tenant_token_store = klass.new(self)
end
with_token(headers, tries = 2) { |merge(authorization: "Bearer #{token}")| ... } click to toggle source
# File lib/lark/api.rb, line 95
def with_token(headers, tries = 2)
  token = headers[:access_token]
  if token.nil?
    via = headers[:via] || 'tenant'
    token = send("#{via}_access_token")
  end
  yield headers.merge(authorization: "Bearer #{token}")
rescue AccessTokenExpiredError
  send("#{via}_token_store").fetch_token
  retry unless (tries -= 1).zero?
end