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.

args is a a hash which 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.

TODO: retrieve endpoint from /api if version is not provided.



45
46
47
48
49
50
51
# File 'lib/pdns_api/http.rb', line 45

def initialize(args)
  @host    = args[:host]
  @key     = args[:key]
  @port    = args.key?(:port)    ? args[:port]    : 8081
  @version = args.key?(:version) ? args[:version] : 1
  @headers = { 'X-API-Key' => @key }
end

Instance Attribute Details

#headersObject

The headers used for requests.



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

def headers
  @headers
end

#versionObject (readonly)

The PowerDNS API version in use.



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

def version
  @version
end

Instance Method Details

#delete(uri) ⇒ Object

Does an HTTP DELETE request to uri. Returns the decoded response.



101
102
103
104
105
# File 'lib/pdns_api/http.rb', line 101

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

#get(uri) ⇒ Object

Does an HTTP GET request to uri. Returns the decoded response.



110
111
112
113
114
# File 'lib/pdns_api/http.rb', line 110

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

#http(net, body = nil) ⇒ Object

Does an HTTP request and returns the response. Parameters are:

  • net: Net::HTTP method object to use in request.

  • body: Optional body of the request.

Returns the decoded response.



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/pdns_api/http.rb', line 81

def http(net, body = nil)
  # Debug output
  puts 'Body: ' + 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
    abort("Error: #{e}")
  end

  response_decode(response)
end

#patch(uri, body = nil) ⇒ Object

Does an HTTP PATCH request to uri. Returns the decoded response.



119
120
121
122
123
# File 'lib/pdns_api/http.rb', line 119

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

#post(uri, body = nil) ⇒ Object

Does an HTTP POST request to uri. Returns the decoded response.



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

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

#put(uri, body = nil) ⇒ Object

Does an HTTP PUT request to uri. Returns the decoded response.



137
138
139
140
141
# File 'lib/pdns_api/http.rb', line 137

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

#response_decode(response) ⇒ Object

Decodes the response from the server.



64
65
66
67
68
69
70
71
72
73
# File 'lib/pdns_api/http.rb', line 64

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

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



56
57
58
59
60
# File 'lib/pdns_api/http.rb', line 56

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