class PDNS::HTTP
Class for connecting to the PowerDNS API
.
Attributes
@return [Hash] the headers used for requests.
@return [Integer] the PowerDNS API
version in use.
Public Class Methods
Creates a PDNS
connection.
@param args [Hash] should include:
- +:host+: hostname or IP address of the PowerDNS server. - +:key+: API key for the PowerDNS server. It may include: - +:port+: Port the server listens on. Defaults to +8081+. - +:version+: Version of the API to use. Defaults to +1+. - +:scheme+: Scheme - HTTP or HTTPS. Defaults to +http+. The version of the API depends on the version of PowerDNS.
# File lib/pdns_api/http.rb, line 44 def initialize(args) @host = args[:host] @headers = { 'X-API-Key' => args[:key] } @port = args.key?(:port) ? args[:port] : 8081 @version = args.key?(:version) ? args[:version] : api_version @scheme = args.key?(:scheme) ? args[:scheme] : 'http' end
Public Instance Methods
Get the version of the API
.
@return [Integer] version of the PowerDNS API
.
# File lib/pdns_api/http.rb, line 57 def api_version # Do a request for the API endpoints net = Net::HTTP::Get.new('/api', @headers) res = http(net) # APIv0 does not play nice. return 0 unless res.is_a? Array # Return the highest API version. res.map { |a| a[:version] }.max.to_i end
Does an HTTP
DELETE
request to uri
.
@param uri [String] URI for request. @return [Hash] the decoded response.
# File lib/pdns_api/http.rb, line 130 def delete(uri) uri = uri(uri) net = Net::HTTP::Delete.new(uri, @headers) http(net) end
Does an HTTP
GET
request to uri
.
@param uri [String] URI for request. @return [Hash] the decoded response.
# File lib/pdns_api/http.rb, line 142 def get(uri) uri = uri(uri) net = Net::HTTP::Get.new(uri, @headers) http(net) end
Does an HTTP
request and returns the response.
@param net [Net::HTTP] object to use in request. @param body [Hash] body of the request.
@return [Hash] decoded response from server.
# File lib/pdns_api/http.rb, line 107 def http(net, body = nil) # Debug output puts "#{net.method}: #{net.path}\nBody: #{body.to_json}" if ENV['DEBUG'] # Start an HTTP connection begin response = Net::HTTP.start(@host, @port, use_ssl: @scheme == 'https') do |http| # Do the request http.request(net, body.to_json) end rescue StandardError => e return { error: e.to_s } end response_decode(response) end
Does an HTTP
PATCH
request to uri
.
@param uri [String] URI for request. @param body [Hash] Body to include in request. @return [Hash] the decoded response.
# File lib/pdns_api/http.rb, line 155 def patch(uri, body = nil) uri = uri(uri) net = Net::HTTP::Patch.new(uri, @headers) http(net, body) end
Does an HTTP
POST
request to uri
.
@param uri [String] URI for request. @param body [Hash] Body to include in request. @return [Hash] the decoded response.
# File lib/pdns_api/http.rb, line 168 def post(uri, body = nil) uri = uri(uri) net = Net::HTTP::Post.new(uri, @headers) http(net, body) end
Does an HTTP
PUT
request to uri
.
@param uri [String] URI for request. @param body [Hash] Body to include in request. @return [Hash] the decoded response.
# File lib/pdns_api/http.rb, line 181 def put(uri, body = nil) uri = uri(uri) net = Net::HTTP::Put.new(uri, @headers) http(net, body) end
Decodes the response from the server.
@param response [Net::HTTPResponse] response to decode. @return [Hash] decoded response.
# File lib/pdns_api/http.rb, line 88 def response_decode(response) return {} if response.body.nil? # Parse and return JSON begin JSON.parse(response.body, symbolize_names: true) rescue JSON::ParserError { error: 'Non-JSON response', result: response.body } end end
Returns the correct URI for a request. This depends on the API
version.
@param request [String] Requested URI. @return [String] Correct URI for the API
version.
# File lib/pdns_api/http.rb, line 76 def uri(request = '') base = '' base = "/api/v#{@version}" unless @version.zero? || request[0..3] == '/api' base + request end