lib/deepl/requests/base.rb in deepl-rb-2.4.0 vs lib/deepl/requests/base.rb in deepl-rb-2.5.0

- old
+ new

@@ -22,35 +22,47 @@ def option(name) options[name.to_s] || options[name.to_sym] end + def delete_option(name) + options.delete(name.to_s) || options.delete(name.to_sym) + end + def set_option(name, value) if options.key?(name.to_sym) options[name.to_sym] = value else options[name.to_s] = value end end def post(payload) - request = Net::HTTP::Post.new(uri.request_uri) + request = Net::HTTP::Post.new(uri.request_uri, headers) request.set_form_data(payload.compact) response = http.request(request) validate_response!(request, response) [request, response] end def get - request = Net::HTTP::Get.new(uri.request_uri) + request = Net::HTTP::Get.new(uri.request_uri, headers) response = http.request(request) validate_response!(request, response) [request, response] end + def delete + request = Net::HTTP::Delete.new(uri.request_uri, headers) + response = http.request(request) + + validate_response!(request, response) + [request, response] + end + def http @http ||= begin http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = uri.scheme == 'https' http @@ -60,11 +72,12 @@ def validate_response!(request, response) return if response.is_a?(Net::HTTPSuccess) case response.code when '400' then raise Exceptions::BadRequest.new(request, response) - when '403' then raise Exceptions::AuthorizationFailed.new(request, response) + when '401', '403' then raise Exceptions::AuthorizationFailed.new(request, response) + when '404' then raise Exceptions::NotFound.new(request, response) when '429' then raise Exceptions::LimitExceeded.new(request, response) when '456' then raise Exceptions::QuotaExceeded.new(request, response) else raise Exceptions::RequestError.new(request, response) end end @@ -89,10 +102,14 @@ def host api.configuration.host end def query_params - { auth_key: api.configuration.auth_key }.merge(options) + options + end + + def headers + { 'Authorization' => "DeepL-Auth-Key #{api.configuration.auth_key}" } end end end end