Class: PDNS::HTTP

Inherits:
Object
  • Object
show all
Defined in:
lib/pdns_api/http.rb

Overview

Class for connecting to the PowerDNS API.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ HTTP

Creates a PDNS connection.

Parameters:

  • 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.

    The version of the API depends on the version of PowerDNS.


43
44
45
46
47
48
# File 'lib/pdns_api/http.rb', line 43

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
end

Instance Attribute Details

#headersHash

Returns the headers used for requests.

Returns:

  • (Hash)

    the headers used for requests.


26
27
28
# File 'lib/pdns_api/http.rb', line 26

def headers
  @headers
end

#versionInteger (readonly)

Returns the PowerDNS API version in use.

Returns:

  • (Integer)

    the PowerDNS API version in use.


30
31
32
# File 'lib/pdns_api/http.rb', line 30

def version
  @version
end

Instance Method Details

#api_versionInteger

Get the version of the API.

Returns:

  • (Integer)

    version of the PowerDNS API.


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/pdns_api/http.rb', line 55

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) ⇒ Hash

Does an HTTP DELETE request to uri.

Parameters:

  • uri (String)

    URI for request.

Returns:

  • (Hash)

    the decoded response.


128
129
130
131
132
# File 'lib/pdns_api/http.rb', line 128

def delete(uri)
  uri = uri(uri)
  net = Net::HTTP::Delete.new(uri, @headers)
  http(net)
end

#get(uri) ⇒ Hash

Does an HTTP GET request to uri.

Parameters:

  • uri (String)

    URI for request.

Returns:

  • (Hash)

    the decoded response.


140
141
142
143
144
# File 'lib/pdns_api/http.rb', line 140

def get(uri)
  uri = uri(uri)
  net = Net::HTTP::Get.new(uri, @headers)
  http(net)
end

#http(net, body = nil) ⇒ Hash

Does an HTTP request and returns the response.

Parameters:

  • net (Net::HTTP)

    object to use in request.

  • body (Hash) (defaults to: nil)

    body of the request.

Returns:

  • (Hash)

    decoded response from server.


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/pdns_api/http.rb', line 105

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) do |http|
      # Do the request
      http.request(net, body.to_json)
    end
  rescue StandardError, Timeout::Error => e
    return { error: e.to_s }
  end

  response_decode(response)
end

#patch(uri, body = nil) ⇒ Hash

Does an HTTP PATCH request to uri.

Parameters:

  • uri (String)

    URI for request.

  • body (Hash) (defaults to: nil)

    Body to include in request.

Returns:

  • (Hash)

    the decoded response.


153
154
155
156
157
# File 'lib/pdns_api/http.rb', line 153

def patch(uri, body = nil)
  uri = uri(uri)
  net = Net::HTTP::Patch.new(uri, @headers)
  http(net, body)
end

#post(uri, body = nil) ⇒ Hash

Does an HTTP POST request to uri.

Parameters:

  • uri (String)

    URI for request.

  • body (Hash) (defaults to: nil)

    Body to include in request.

Returns:

  • (Hash)

    the decoded response.


166
167
168
169
170
# File 'lib/pdns_api/http.rb', line 166

def post(uri, body = nil)
  uri = uri(uri)
  net = Net::HTTP::Post.new(uri, @headers)
  http(net, body)
end

#put(uri, body = nil) ⇒ Hash

Does an HTTP PUT request to uri.

Parameters:

  • uri (String)

    URI for request.

  • body (Hash) (defaults to: nil)

    Body to include in request.

Returns:

  • (Hash)

    the decoded response.


179
180
181
182
183
# File 'lib/pdns_api/http.rb', line 179

def put(uri, body = nil)
  uri = uri(uri)
  net = Net::HTTP::Put.new(uri, @headers)
  http(net, body)
end

#response_decode(response) ⇒ Hash

Decodes the response from the server.

Parameters:

  • response (Net::HTTPResponse)

    response to decode.

Returns:

  • (Hash)

    decoded response.


86
87
88
89
90
91
92
93
94
95
# File 'lib/pdns_api/http.rb', line 86

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 = '') ⇒ String

Returns the correct URI for a request. This depends on the API version.

Parameters:

  • request (String) (defaults to: '')

    Requested URI.

Returns:

  • (String)

    Correct URI for the API version.


74
75
76
77
78
# File 'lib/pdns_api/http.rb', line 74

def uri(request = '')
  base = ''
  base = "/api/v#{@version}" unless @version == 0 || request[0..3] == '/api'
  base + request
end