Skip to content

Commit

Permalink
Add ZoneRecord and Subdomain classes with a few methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
anderspetersson committed Apr 15, 2014
1 parent ba915ea commit 9f3d6ae
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 8 deletions.
126 changes: 119 additions & 7 deletions loopia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def call(self, method=None, args=[]):
Makes the call to the Loopia RPC Server.
"""

client = xmlclient.ServerProxy(uri='https://test-api.loopia.se/RPCSERV', encoding='utf-8', allow_none=True)
client = xmlclient.ServerProxy(uri='https://api.loopia.se/RPCSERV', encoding='utf-8', allow_none=True)
response = getattr(client, method)(self.username, self.password, *args)

if response == 'OK':
Expand All @@ -33,7 +33,12 @@ def get_domains(self):
Get all domains for the current account.
"""

return self.call(method='getDomains')
response = self.call(method='getDomains')
domains = []
for d in response:
domain = self.domain(domain=d['domain'])
domains.append(domain)
return domains

def get_unpaid_invoices(self, with_vat=True):
"""
Expand All @@ -56,6 +61,19 @@ def domain(self, domain=None):

return Domain(apiobj=self, domainname=domain)

def subdomain(self, domain=None, subdomain=None):
"""
Calls the Subdomain class with an instance of the API
"""

return Subdomain(apiobj=self, domainname=domain, subdomain=subdomain)

def zonerecord(self, domain=None, subdomain=None, record_type='A', \
ttl=3600, priority=None, rdata=None):

return ZoneRecord(apiobj=self, domainname=domain, subdomain=subdomain, record_type=record_type, \
ttl=ttl, priority=priority, rdata=rdata)

def invoice(self, reference_no=None, with_vat=True):
"""
Calls the Invoice class with an instance of the API.
Expand All @@ -74,6 +92,9 @@ def __init__(self, apiobj, domainname=None):
self.username = apiobj.username
self.password = apiobj.password

def __str__(self):
return self.domainname

def is_free(self):
"""
Check if a domain is availble to register.
Expand All @@ -99,6 +120,7 @@ def order(self, customer_number='', has_accepted_terms_and_conditions=0):
def info(self):
"""
Get info such as expiration date and registration status for a domain.
Can only be used on domains in account.
"""

return self.call(method='getDomain', args=[self.domainname])
Expand All @@ -109,15 +131,19 @@ def get_subdomains(self):
Get all subdomains for a domain.
"""

return self.call(method='getSubdomains', args=[self.domainname])
response = self.call(method='getSubdomains', args=[self.domainname])
subdomains = []
for s in response:
subdomain = self.subdomain(domain=self.domainname, subdomain=s)
subdomains.append(subdomain)
return subdomains


def get_zonerecords(self, subdomain=None):
def add_subdomain(self, subdomain):
"""
Get zone records for a subdomain
Add a subdomain to a domain.
"""

return self.call(method='getZoneRecords', args=[self.domainname, subdomain])
self.subdomain(self.domainname, subdomain).create()

def add_zonerecord(self, subdomain=None, record_type=None, record_ttl=3600,
record_priority=0, record_data=None):
Expand All @@ -128,6 +154,92 @@ def add_zonerecord(self, subdomain=None, record_type=None, record_ttl=3600,
return self.call(method='addZoneRecord', args=[self.domainname, subdomain,
{'type': record_type, 'ttl': record_ttl, 'priority': record_priority, 'rdata': record_data}])

def remove(self, deactivate=False):
"""
Remove a domain from account, if deactivate is True, wait until the deactivation-date.
"""

return self.call(method='removeDomain', args=[self.domainname, deactivate])


class Subdomain(API):
"""
Handle subdomains.
"""

def __init__(self, apiobj, domainname=None, subdomain=None):
self.domainname = domainname
self.subdomain = subdomain
self.username = apiobj.username
self.password = apiobj.password

def __str__(self):
return '%s.%s' % (self.subdomain, self.domainname)

def create(self):
"""
Add a subdomain to a domain.
"""

return self.call(method='addSubdomain', args=[self.domainname, self.subdomain])

def remove(self):
"""
Remove a subdomain from a domain.
"""

return self.call(method='removeSubdomain', args=[self.domainname, self.subdomain])

def get_zonerecords(self):
"""
Get all DNS records for a domain.
"""

return self.call(method='getZoneRecords', args=[self.domainname, self.subdomain])

def add_zonerecord(self, record_type='A', ttl=3600, priority=None, rdata=None):
"""
Add a subdomain to a domain.
"""

self.zonerecord(self.domainname, self.subdomain, record_type, ttl, priority, rdata).create()


class ZoneRecord(API):
"""
Handle DNS records.
"""

def __init__(self, apiobj, domainname, subdomain, record_id=None, record_type='A', \
ttl=3600, priority=None, rdata=None):
self.domainname = domainname
self.subdomain = subdomain
self.record_id = record_id
self.type = record_type
self.ttl = ttl
self.priority = priority
self.rdata = rdata
self.username = apiobj.username
self.password = apiobj.password

def __str__(self):
return self.record_id

def create(self):
"""
Add a DNS entry to a (sub)domain.
"""

record = {
'type': self.type,
'ttl': self.ttl,
'priority': self.priority,
'rdata': self.rdata
}

self.call(method='addZoneRecord', args=[self.domainname, self.subdomain, record])


class Invoice(API):
"""
Handle invoices
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

setup(
name='loopia',
version='0.2.1',
version='0.3.0',
author='Anders Petersson',
author_email='me@anderspetersson.se',
url='http://github.com/anderspetersson/loopia-python-api',
Expand Down

0 comments on commit 9f3d6ae

Please sign in to comment.