lib/habitica_cli/api.rb in habitica_cli-0.0.1 vs lib/habitica_cli/api.rb in habitica_cli-0.1.0

- old
+ new

@@ -1,24 +1,55 @@ module HabiticaCli # responsible for communicating with habit at a low level class Api + # A simple wrapper for API errors + class ApiError < StandardError + def initialize(response) + @response = response + set_backtrace([]) + end + + def to_s + errors = @response.body['errors'] || [] + path = @response.url.path + error_messages = errors.map { |e| e['message'] } + %( +Habitica Error (#{path}): #{@response.body['message']} +==== +#{error_messages.join("\n")} +) + end + end + + # generic handling for habitica responses + class HabiticaResponseMiddleware < Faraday::Middleware + def call(request_env) + @app.call(request_env).on_complete do |response_env| + fail ApiError.new(response_env) unless response_env.success? # rubocop:disable Style/RaiseArgs, Style/LineLength + response_env + end + end + end + def initialize(user_id, api_token) @debug = ENV['DEBUG_HABITICA'] == 'true' - - @connection = create_connection(user_id, api_token) + @user_id = user_id + @api_token = api_token end - def get(url) - @connection.get(url) + def get(url, query = nil) + connection do |faraday| + faraday.request :url_encoded + end.get(url, query) end def post(url, body = nil) - @connection.post(url, body) + connection.post(url, body) end def put(url, body) - @connection.put(url, body) + connection.put(url, body) end private def default_headers(user_id, api_token) @@ -27,20 +58,27 @@ 'x-api-user' => user_id, 'Content-Type' => 'application/json' } end - def create_connection(user_id, api_token) - Faraday.new( - url: 'https://habitica.com/api/v2/', - headers: default_headers(user_id, api_token) - ) do |faraday| - faraday.request :json + def configure_defaults(faraday) + faraday.request :json - faraday.response :json, content_type: /\bjson$/ - faraday.response :logger if @debug + faraday.use HabiticaResponseMiddleware + faraday.response :json, content_type: /\bjson$/ + faraday.response :logger if @debug - faraday.adapter Faraday.default_adapter + faraday.adapter Faraday.default_adapter + end + + def connection + Faraday.new( + url: 'https://habitica.com/api/v3/', + headers: default_headers(@user_id, @api_token) + ) do |faraday| + configure_defaults(faraday) + yield faraday if block_given? + faraday end end end end