2016-12-19 23:22:10 +00:00
|
|
|
module Mattermost
|
2016-12-20 11:02:37 +00:00
|
|
|
class ClientError < Mattermost::Error; end
|
|
|
|
|
2016-12-19 23:22:10 +00:00
|
|
|
class Client
|
|
|
|
attr_reader :user
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def with_session(&blk)
|
2016-12-20 10:32:42 +00:00
|
|
|
Mattermost::Session.new(user).with_session(&blk)
|
2016-12-19 23:22:10 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def json_get(path, options = {})
|
|
|
|
with_session do |session|
|
|
|
|
json_response session.get(path, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def json_post(path, options = {})
|
|
|
|
with_session do |session|
|
|
|
|
json_response session.post(path, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def json_response(response)
|
|
|
|
json_response = JSON.parse(response.body)
|
|
|
|
|
2016-12-20 18:11:53 +00:00
|
|
|
unless response.success?
|
|
|
|
raise ClientError(json_response['message'] || 'Undefined error')
|
2016-12-19 23:22:10 +00:00
|
|
|
end
|
2016-12-20 18:11:53 +00:00
|
|
|
|
|
|
|
json_response
|
2016-12-20 19:11:02 +00:00
|
|
|
rescue JSON::JSONError
|
2016-12-20 18:11:53 +00:00
|
|
|
raise ClientError('Cannot parse response')
|
2016-12-19 23:22:10 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|