module Asterisk
class Client
include HTTParty
base_uri 'https://asterisk.dev/'
def initialize(session_key = nil)
@session_key = session_key
end
def get_session_key(login, password)
self.class.put('/api/auth', query: { login: login, password: password })
end
def get_contacts
self.class.get("/api/#{@session_key}/contacts")
end
def get_contact(id)
self.class.get("/api/#{@session_key}/contact/#{id}")
end
def create_contact
self.class.put("/api/#{@session_key}/create")
end
def logout
self.class.delete("/api/#{@session_key}/logout")
end
end
end
现在它的工作原理如下
session_key = Asterisk::Client.new.get_session_key('login', 'pass')
client = Asterisk::Client.new(session_key)
client.get_contacts
我想使用单例获取和设置会话密钥。怎么做?