module Lifen class UserAuthenticatedClient < Client def initialize(token) @token = token end attr_reader :token def request(mode, url, params = {}) response = faraday_client.send(mode) do |req| req.url url req.headers['x-auth-token'] = token req.headers['Content-Type'] = "application/json" req.body = JSON.generate(params) end if response.status == 500 json = JSON.parse response.body trace_id = json.fetch("X-B3-TraceId", "unknown") raise Error, "Error 500, Internal server error (trace ID: #{trace_id})" end raise UnauthorizedError, "Token is not valid" if response.status == 401 raise Error, "Action is forbidden" if response.status == 403 raise InvalidParamsError, "Invalid params" if response.status == 400 json = JSON.parse response.body json end def post(url, params = {}) request(:post, url, params) end def get(url, params = {}) request(:get, url, params) end end end