Class: Dnsimple::Client

Inherits:
Object
  • Object
show all
Includes:
Compatibility
Defined in:
lib/dnsimple/client.rb,
lib/dnsimple/client/users.rb,
lib/dnsimple/client/clients.rb,
lib/dnsimple/client/domains.rb,
lib/dnsimple/client/contacts.rb,
lib/dnsimple/client/services.rb,
lib/dnsimple/client/registrar.rb,
lib/dnsimple/client/templates.rb,
lib/dnsimple/client/certificates.rb,
lib/dnsimple/client/name_servers.rb,
lib/dnsimple/client/domains_zones.rb,
lib/dnsimple/client/domains_privacy.rb,
lib/dnsimple/client/domains_records.rb,
lib/dnsimple/client/domains_sharing.rb,
lib/dnsimple/client/domains_forwards.rb,
lib/dnsimple/client/services_domains.rb,
lib/dnsimple/client/templates_domains.rb,
lib/dnsimple/client/templates_records.rb,
lib/dnsimple/client/domains_autorenewal.rb,
lib/dnsimple/client/vanity_name_servers.rb

Overview

Client for the DNSimple API

Defined Under Namespace

Modules: Certificates, Contacts, Domains, DomainsAutorenewal, DomainsForwards, DomainsPrivacy, DomainsRecords, DomainsSharing, DomainsZones, NameServers, Registrar, Services, ServicesDomains, Templates, TemplatesDomains, TemplatesRecords, Users, VanityNameServers Classes: CertificatesService, ClientService, ContactsService, DomainsService, NameServersService, RegistrarService, ServicesService, TemplatesService, UsersService

Constant Summary collapse

HEADER_2FA_STRICT =
"X-DNSimple-2FA-Strict"
HEADER_API_TOKEN =
"X-DNSimple-Token"
HEADER_DOMAIN_API_TOKEN =
"X-DNSimple-Domain-Token"
HEADER_OTP_TOKEN =
"X-DNSimple-OTP"
HEADER_EXCHANGE_TOKEN =
"X-DNSimple-OTP-Token"
API_VERSION =

Returns The current API version.

Returns:

  • (String)

    The current API version.

"v1"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Compatibility

included

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



58
59
60
61
62
63
64
65
66
# File 'lib/dnsimple/client.rb', line 58

def initialize(options = {})
  defaults = Dnsimple::Default.options

  Dnsimple::Default.keys.each do |key|
    instance_variable_set(:"@#{key}", options[key] || defaults[key])
  end

  @services = {}
end

Instance Attribute Details

#api_endpointString

Returns Base URL for API requests.

Returns:

  • (String)

    Base URL for API requests.



54
55
56
# File 'lib/dnsimple/client.rb', line 54

def api_endpoint
  @api_endpoint
end

#api_tokenString

Returns API access token for authentication.

Returns:

  • (String)

    API access token for authentication

See Also:



54
55
# File 'lib/dnsimple/client.rb', line 54

attr_accessor :api_endpoint, :username, :password, :exchange_token, :api_token, :domain_api_token,
:user_agent, :proxy

#domain_api_tokenString

Returns Domain API access token for authentication.

Returns:

  • (String)

    Domain API access token for authentication

See Also:



54
55
# File 'lib/dnsimple/client.rb', line 54

attr_accessor :api_endpoint, :username, :password, :exchange_token, :api_token, :domain_api_token,
:user_agent, :proxy

#exchange_tokenString

Returns Exchange Token for Basic Authentication with 2FA.

Returns:

  • (String)

    Exchange Token for Basic Authentication with 2FA

See Also:



54
55
# File 'lib/dnsimple/client.rb', line 54

attr_accessor :api_endpoint, :username, :password, :exchange_token, :api_token, :domain_api_token,
:user_agent, :proxy

#passwordString

Returns DNSimple password for Basic Authentication.

Returns:

  • (String)

    DNSimple password for Basic Authentication

See Also:



54
55
# File 'lib/dnsimple/client.rb', line 54

attr_accessor :api_endpoint, :username, :password, :exchange_token, :api_token, :domain_api_token,
:user_agent, :proxy

#proxyString?

Returns Configure address:port values for proxy server.

Returns:

  • (String, nil)

    Configure address:port values for proxy server



54
55
# File 'lib/dnsimple/client.rb', line 54

attr_accessor :api_endpoint, :username, :password, :exchange_token, :api_token, :domain_api_token,
:user_agent, :proxy

#user_agentString

Returns Configure User-Agent header for requests.

Returns:

  • (String)

    Configure User-Agent header for requests.



54
55
# File 'lib/dnsimple/client.rb', line 54

attr_accessor :api_endpoint, :username, :password, :exchange_token, :api_token, :domain_api_token,
:user_agent, :proxy

#usernameString

Returns DNSimple username for Basic Authentication.

Returns:

  • (String)

    DNSimple username for Basic Authentication



54
55
# File 'lib/dnsimple/client.rb', line 54

attr_accessor :api_endpoint, :username, :password, :exchange_token, :api_token, :domain_api_token,
:user_agent, :proxy

Class Method Details

.versioned(path) ⇒ String

Prepends the correct API version to path.

Returns:

  • (String)

    The versioned path.



28
29
30
# File 'lib/dnsimple/client.rb', line 28

def self.versioned(path)
  File.join(API_VERSION, path)
end

Instance Method Details

#certificatesDnsimple::Client::CertificatesService

Returns The certificate-related API proxy.

Returns:



5
6
7
# File 'lib/dnsimple/client/clients.rb', line 5

def certificates
  @services[:certificates] ||= Client::CertificatesService.new(self)
end

#contactsDnsimple::Client::ContactsService

Returns The contact-related API proxy.

Returns:



10
11
12
# File 'lib/dnsimple/client/clients.rb', line 10

def contacts
  @services[:contacts] ||= Client::ContactsService.new(self)
end

#delete(path, options = {}) ⇒ HTTParty::Response

Make a HTTP DELETE request.

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

Returns:

  • (HTTParty::Response)


101
102
103
# File 'lib/dnsimple/client.rb', line 101

def delete(path, options = {})
  execute :delete, path, options
end

#domainsDnsimple::Client::DomainsService

Returns The domain-related API proxy.

Returns:



15
16
17
# File 'lib/dnsimple/client/clients.rb', line 15

def domains
  @services[:domains] ||= Client::DomainsService.new(self)
end

#execute(method, path, data, options = {}) ⇒ HTTParty::Response

Executes a request, validates and returns the response.

Parameters:

  • method (String)

    The HTTP method

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

Returns:

  • (HTTParty::Response)

Raises:



116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dnsimple/client.rb', line 116

def execute(method, path, data, options = {})
  response = request(method, path, data, options)

  case response.code
  when 200..299
    response
  when 401
    raise (response.headers[HEADER_OTP_TOKEN] == "required" ? TwoFactorAuthenticationRequired : AuthenticationFailed), response["message"]
  when 404
    raise NotFoundError.new(response)
  else
    raise RequestError.new(response)
  end
end

#get(path, options = {}) ⇒ HTTParty::Response

Make a HTTP GET request.

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

Returns:

  • (HTTParty::Response)


74
75
76
# File 'lib/dnsimple/client.rb', line 74

def get(path, options = {})
  execute :get, path, options
end

#name_serversDnsimple::Client::NameServersService

Returns The name server-related API proxy.

Returns:



20
21
22
# File 'lib/dnsimple/client/clients.rb', line 20

def name_servers
  @services[:name_servers] ||= Client::NameServersService.new(self)
end

#post(path, options = {}) ⇒ HTTParty::Response

Make a HTTP POST request.

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Body and header params for request

Returns:

  • (HTTParty::Response)


83
84
85
# File 'lib/dnsimple/client.rb', line 83

def post(path, options = {})
  execute :post, path, options
end

#put(path, options = {}) ⇒ HTTParty::Response

Make a HTTP PUT request.

Parameters:

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Body and header params for request

Returns:

  • (HTTParty::Response)


92
93
94
# File 'lib/dnsimple/client.rb', line 92

def put(path, options = {})
  execute :put, path, options
end

#registrarDnsimple::Client::RegistrarService

Returns The registrar-related API proxy.

Returns:



25
26
27
# File 'lib/dnsimple/client/clients.rb', line 25

def registrar
  @services[:registrar] ||= Client::RegistrarService.new(self)
end

#request(method, path, data, options = {}) ⇒ HTTParty::Response

Make a HTTP request.

This method doesn’t validate the response and never raise errors even in case of HTTP error codes, except for connection errors raised by the underlying HTTP client.

Therefore, it’s up to the caller to properly handle and validate the response.

Parameters:

  • method (String)

    The HTTP method

  • path (String)

    The path, relative to #api_endpoint

  • options (Hash) (defaults to: {})

    Query and header params for request

Returns:

  • (HTTParty::Response)


144
145
146
147
148
149
150
151
152
153
154
# File 'lib/dnsimple/client.rb', line 144

def request(method, path, data, options = {})
  if data.is_a?(Hash)
    options[:query]   = data.delete(:query)   if data.key?(:query)
    options[:headers] = data.delete(:headers) if data.key?(:headers)
  end
  if !data.empty?
    options[:body] = data
  end

  HTTParty.send(method, api_endpoint + path, Extra.deep_merge!(base_options, options))
end

#servicesDnsimple::Client::ServicesService

Returns The service-related API proxy.

Returns:



30
31
32
# File 'lib/dnsimple/client/clients.rb', line 30

def services
  @services[:services] ||= Client::ServicesService.new(self)
end

#templatesDnsimple::Client::TemplatesService

Returns The template-related API proxy.

Returns:



35
36
37
# File 'lib/dnsimple/client/clients.rb', line 35

def templates
  @services[:templates] ||= Client::TemplatesService.new(self)
end

#usersDnsimple::Client::UsersService

Returns The user-related API proxy.

Returns:



40
41
42
# File 'lib/dnsimple/client/clients.rb', line 40

def users
  @services[:users] ||= Client::UsersService.new(self)
end