Skip to content

Commit

Permalink
Add REST API client implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalie committed Sep 4, 2023
1 parent 0cf7bff commit 560636a
Show file tree
Hide file tree
Showing 14 changed files with 357 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ require 'rake/testtask'
require 'rdoc/task'

desc 'Default: run tests.'
task :default => :test
task default: :test


desc 'Run luadns unit tests.'
Expand Down
13 changes: 12 additions & 1 deletion lib/luadns.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
# frozen_string_literal: true

module Luadns
BASE_URL = 'https://api.luadns.com/v1'
BASE_URL = 'https://api.luadns.com/v1'
JSON_MIME = 'application/json'
USER_AGENT = "luadns-ruby/#{VERSION}".freeze
end

require 'httparty'
require_relative 'luadns/errors'
require_relative 'luadns/schema'
require_relative 'luadns/schema/base'
require_relative 'luadns/schema/user'
require_relative 'luadns/schema/zone'
require_relative 'luadns/schema/record'
require_relative 'luadns/response'
require_relative 'luadns/http_client'
require_relative 'luadns/client'
90 changes: 88 additions & 2 deletions lib/luadns/client.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,99 @@
module Luadns
class Client
@@options = {
format: :json,
headers: {
'Accept' => JSON_MIME,
'User-Agent' => USER_AGENT
}
}

attr_reader :base_url
attr_reader :username
attr_reader :password

def initialize(username, password, base_url = Luadns::BASE_URL)
@base_url = base_url
def initialize(username, password, base_url = BASE_URL)
@http_client = HttpClient.new

@username = username
@password = password
@base_url = base_url
end

def me
@http_client.get(endpoint('/users/me'), options) do |body|
JSON.parse(body, object_class: Schema::User)
end
end

def list_zones
@http_client.get(endpoint('/zones'), options) do |body|
JSON.parse(body, object_class: Schema::Zone)
end
end

def create_zone(attrs)
@http_client.post(endpoint('/zones'), attrs, options) do |body|
JSON.parse(body, object_class: Schema::Zone)
end
end

def get_zone(zone_id)
@http_client.get(endpoint("/zones/#{zone_id}"), options) do |body|
JSON.parse(body, object_class: Schema::Zone)
end
end

def update_zone(zone_id, attrs)
@http_client.put(endpoint("/zones/#{zone_id}"), attrs, options) do |body|
JSON.parse(body, object_class: Schema::Zone)
end
end

def delete_zone(zone_id)
@http_client.delete(endpoint("/zones/#{zone_id}"), options) do |body|
JSON.parse(body, object_class: Schema::Zone)
end
end

def list_records(zone)
@http_client.get(endpoint("/zones/#{zone.id}/records"), options) do |body|
JSON.parse(body, object_class: Schema::Record)
end
end

def create_record(zone, attrs)
@http_client.post(endpoint("/zones/#{zone.id}/records"), attrs, options) do |body|
JSON.parse(body, object_class: Schema::Record)
end
end

def get_record(zone, record_id)
@http_client.get(endpoint("/zones/#{zone.id}/records/#{record_id}"), options) do |body|
JSON.parse(body, object_class: Schema::Record)
end
end

def update_record(zone, record_id, attrs)
@http_client.put(endpoint("/zones/#{zone.id}/records/#{record_id}"), attrs, options) do |body|
JSON.parse(body, object_class: Schema::Record)
end
end

def delete_record(zone, record_id)
@http_client.delete(endpoint("/zones/#{zone.id}/records/#{record_id}"), options) do |body|
JSON.parse(body, object_class: Schema::Record)
end
end

private

def endpoint(path)
"#{@base_url}#{path}"
end

def options
@@options.merge({ basic_auth: { username: @username, password: @password } })
end
end
end
21 changes: 20 additions & 1 deletion lib/luadns/errors.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
module Luadns
# class LuadnsError < RuntimeError; end
class Error < StandardError; end
class NotFoundError < Error; end

class TooManyRequestsError < Error
attr_reader :response
attr_reader :reset

def initialize(response)
@response = response
@reset = response.headers['X-Ratelimit-Reset'].to_i
end
end

class RequestError < Error
attr_reader :response

def initialize(response)
@response = response
end
end
end
46 changes: 46 additions & 0 deletions lib/luadns/http_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
module Luadns
class HttpClient
# Make a HTTP GET request.
def get(url, options = {})
yield http_request(:get, url, nil, options)
end

# Make a HTTP POST request.
def post(url, data = nil, options = {})
yield http_request(:post, url, data, options)
end

# Make a HTTP PUT request.
def put(url, data = nil, options = {})
yield http_request(:put, url, data, options)
end

# Make a HTTP DELETE request.
def delete(url, options = {})
yield http_request(:delete, url, nil, options)
end

private

def http_request(method, url, data = nil, options = {})
content_type = options.dig(:headers, 'Content-Type')
options.merge!(body: encode_body(data, content_type)) if data
response = HTTParty.send(method, url, options)

case response.code
when 200
response.body
when 404
raise NotFoundError, response
when 429
raise TooManyRequestsError, response
else
raise RequestError, response
end
end

def encode_body(data, content_type)
content_type == JSON_MIME ? JSON.dump(data) : data
end
end
end
6 changes: 6 additions & 0 deletions lib/luadns/response.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Luadns
class Response
attr_reader :status_code
attr_reader :body
end
end
4 changes: 0 additions & 4 deletions lib/luadns/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,3 @@ module Luadns
module Schema
end
end

require_relative 'schema/user'
require_relative 'schema/zone'
require_relative 'schema/record'
15 changes: 15 additions & 0 deletions lib/luadns/schema/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Luadns
module Schema
class Base
def initialize(options = {})
options.each do |name, value|
send("#{name}=", value)
end
end

def []=(name, value)
send("#{name}=", value) if respond_to?(name)
end
end
end
end
2 changes: 1 addition & 1 deletion lib/luadns/schema/record.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Luadns
module Schema
class Record
class Record < Base
attr_accessor :id
attr_accessor :name
attr_accessor :type
Expand Down
2 changes: 1 addition & 1 deletion lib/luadns/schema/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Luadns
module Schema
class User
class User < Base
attr_accessor :email
attr_accessor :name
attr_accessor :repo_uri
Expand Down
2 changes: 1 addition & 1 deletion lib/luadns/schema/zone.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Luadns
module Schema
class Zone
class Zone < Base
attr_accessor :id
attr_accessor :name
attr_accessor :created_at
Expand Down
Loading

0 comments on commit 560636a

Please sign in to comment.