2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
module Mattermost
|
2021-09-04 01:27:46 +05:30
|
|
|
ClientError = Class.new(::Mattermost::Error)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
class Client
|
|
|
|
attr_reader :user
|
|
|
|
|
|
|
|
def initialize(user)
|
|
|
|
@user = user
|
|
|
|
end
|
|
|
|
|
|
|
|
def with_session(&blk)
|
2021-09-04 01:27:46 +05:30
|
|
|
::Mattermost::Session.new(user).with_session(&blk)
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# Should be used in a session manually
|
|
|
|
def get(session, path, options = {})
|
|
|
|
json_response session.get(path, options)
|
|
|
|
end
|
|
|
|
|
|
|
|
# Should be used in a session manually
|
|
|
|
def post(session, path, options = {})
|
|
|
|
json_response session.post(path, options)
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def delete(session, path, options)
|
|
|
|
json_response session.delete(path, options)
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def session_get(path, options = {})
|
|
|
|
with_session do |session|
|
|
|
|
get(session, path, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def session_post(path, options = {})
|
|
|
|
with_session do |session|
|
|
|
|
post(session, path, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
def session_delete(path, options = {})
|
|
|
|
with_session do |session|
|
|
|
|
delete(session, path, options)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-08-17 22:00:37 +05:30
|
|
|
def json_response(response)
|
2020-05-24 23:13:21 +05:30
|
|
|
json_response = Gitlab::Json.parse(response.body, legacy_mode: true)
|
2017-08-17 22:00:37 +05:30
|
|
|
|
|
|
|
unless response.success?
|
2021-09-04 01:27:46 +05:30
|
|
|
raise ::Mattermost::ClientError, json_response['message'] || 'Undefined error'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
|
|
|
|
json_response
|
|
|
|
rescue JSON::JSONError
|
2021-09-04 01:27:46 +05:30
|
|
|
raise ::Mattermost::ClientError, 'Cannot parse response'
|
2017-08-17 22:00:37 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|