lib/lifen/user_authenticated_client.rb in lifen-0.2.1 vs lib/lifen/user_authenticated_client.rb in lifen-1.0.0

- old
+ new

@@ -5,41 +5,31 @@ @token = token end attr_reader :token - def request(mode, url, params = {}) - response = faraday_client.send(mode) do |req| - req.url url + private - req.headers['x-auth-token'] = token - req.headers['Content-Type'] = "application/json" + def handle_status!(response) + super(response) - req.body = JSON.generate(params) + case response.status + when 400 + raise InvalidParamsError, "Invalid params" + when 401 + raise UnauthorizedError, "Token is not valid" + when 403 + raise Error, "Action is forbidden" + end + 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})" + def bearer + token.value 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 + def before_request + token.refresh_once_if_needed + end end end \ No newline at end of file