Class: PDNS::HTTP
- Inherits:
-
Object
- Object
- PDNS::HTTP
- Defined in:
- lib/pdns_api/http.rb
Overview
Class for connecting to the PowerDNS API.
Instance Attribute Summary collapse
-
#headers ⇒ Hash
The headers used for requests.
-
#version ⇒ Integer
readonly
The PowerDNS API version in use.
Instance Method Summary collapse
-
#api_version ⇒ Integer
Get the version of the API.
-
#delete(uri) ⇒ Hash
Does an HTTP
DELETE
request touri
. -
#get(uri) ⇒ Hash
Does an HTTP
GET
request touri
. -
#http(net, body = nil) ⇒ Hash
Does an HTTP request and returns the response.
-
#initialize(args) ⇒ HTTP
constructor
Creates a PDNS connection.
-
#patch(uri, body = nil) ⇒ Hash
Does an HTTP
PATCH
request touri
. -
#post(uri, body = nil) ⇒ Hash
Does an HTTP
POST
request touri
. -
#put(uri, body = nil) ⇒ Hash
Does an HTTP
PUT
request touri
. -
#response_decode(response) ⇒ Hash
Decodes the response from the server.
-
#uri(request = '') ⇒ String
Returns the correct URI for a request.
Constructor Details
#initialize(args) ⇒ HTTP
Creates a PDNS connection.
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
#headers ⇒ Hash
Returns the headers used for requests.
26 27 28 |
# File 'lib/pdns_api/http.rb', line 26 def headers @headers end |
#version ⇒ Integer (readonly)
Returns 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_version ⇒ Integer
Get the version of the 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
.
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
.
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.
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
.
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
.
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
.
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.
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.
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 |