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.

    • :scheme: Scheme - HTTP or HTTPS. Defaults to http.

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



44
45
46
47
48
49
50
# 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

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.



57
58
59
60
61
62
63
64
65
66
67
# 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) ⇒ Hash

Does an HTTP DELETE request to uri.

Parameters:

  • uri (String)

    URI for request.

Returns:

  • (Hash)

    the decoded response.



130
131
132
133
134
# 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) ⇒ Hash

Does an HTTP GET request to uri.

Parameters:

  • uri (String)

    URI for request.

Returns:

  • (Hash)

    the decoded response.



142
143
144
145
146
# 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) ⇒ 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.



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# 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) ⇒ 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.



155
156
157
158
159
# 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) ⇒ 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.



168
169
170
171
172
# 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) ⇒ 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.



181
182
183
184
185
# 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) ⇒ Hash

Decodes the response from the server.

Parameters:

  • response (Net::HTTPResponse)

    response to decode.

Returns:

  • (Hash)

    decoded response.



88
89
90
91
92
93
94
95
96
97
# 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 = '') ⇒ 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.



76
77
78
79
80
# 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