class PDNS::HTTP

Class for connecting to the PowerDNS API.

Attributes

headers[RW]

@return [Hash] the headers used for requests.

version[R]

@return [Integer] the PowerDNS API version in use.

Public Class Methods

new(args) click to toggle source

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

api_version() click to toggle source

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
delete(uri) click to toggle source

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
get(uri) click to toggle source

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
http(net, body = nil) click to toggle source

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
patch(uri, body = nil) click to toggle source

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
post(uri, body = nil) click to toggle source

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
put(uri, body = nil) click to toggle source

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
response_decode(response) click to toggle source

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
uri(request = '') click to toggle source

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