Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 69 additions & 0 deletions namecheap/namecheap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1348,3 +1348,72 @@ def domains_getInfo(self, domain: str, hostname: str = None, **user_payload) ->
return DomainDetails.from_dict(info_result)

get_domain_info = domains_getInfo

# https://www.namecheap.com/support/api/methods/domains-ns/create/
def domains_ns_create(self, domain: str, nameserver: str, ip: str):
"""Creates a new nameserver

**Example Usage**::

>>> api = Api('user', 'key', '12.34.56.78')
>>> api.domains_ns_create('example.com', 'ns1.example.com', '98.76.54.32')
{'Domain': 'example.com', 'Nameserver': 'ns1.example.com', 'IP': '98.76.54.32', 'IsSuccess': 'true'}
"""
sld, tld = domain.split(".")
payload = {"SLD": sld, "TLD": tld, "Nameserver": nameserver, "IP": ip}

log.debug(f"Create Nameserver: {payload}")

return self.get_element_dict(self.call("namecheap.domains.ns.create", payload), 'DomainNSCreateResult')

# https://www.namecheap.com/support/api/methods/domains-ns/getInfo/
def domains_ns_getInfo(self, domain: str, nameserver: str):
"""Retrieves information about a registered nameserver

**Example Usage**::

>>> api = Api('user', 'key', '12.34.56.78')
>>> api.domains_ns_getInfo('example.com', 'ns1.example.com')
{'Domain': 'example.com', 'Nameserver': 'example.com', 'IP': '98.76.54.32'}
"""
sld, tld = domain.split(".")
payload = {"SLD": sld, "TLD": tld, "Nameserver": nameserver}

log.debug(f"Get Nameserver Info: {payload}")

return self.get_element_dict(self.call("namecheap.domains.ns.getInfo", payload), 'DomainNSInfoResult')

# https://www.namecheap.com/support/api/methods/domains-ns/update/
def domains_ns_update(self, domain: str, nameserver: str, old_ip: str, ip: str):
"""Updates the IP for a nameserver

**Example Usage**::

>>> api = Api('user', 'key', '12.34.56.78')
>>> api.domains_ns_update('example.com', 'ns1.example.com', '98.76.54.32', '11.22.33.44')
{'Domain': '.example.com', 'Nameserver': 'ns1.example.com', 'IsSuccess': 'true'}
"""
sld, tld = domain.split(".")
payload = {"SLD": sld, "TLD": tld, "Nameserver": nameserver, "OldIP": old_ip, "IP": ip}

log.debug(f"Update Nameserver: {payload}")

return self.get_element_dict(self.call("namecheap.domains.ns.update", payload), 'DomainNSUpdateResult')

# https://www.namecheap.com/support/api/methods/domains-ns/delete/
def domains_ns_delete(self, domain, nameserver: str):
"""Deletes a nameserver

**Example Usage**::

>>> api = Api('user', 'key', '12.34.56.78')
>>> api.domains_ns_delete('example.com', 'ns1.example.com')
{'Domain': 'example.com', 'Nameserver': 'example.com', 'IsSuccess': 'true'}

"""
sld, tld = domain.split(".")
payload = {"SLD": sld, "TLD": tld, "Nameserver": nameserver}

log.debug(f"Delete Nameserver: {payload}")

return self.get_element_dict(self.call("namecheap.domains.ns.delete", payload), 'DomainNSDeleteResult')
34 changes: 34 additions & 0 deletions namecheap_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,3 +260,37 @@ def test_list_of_dictionaries_to_numbered_payload():
}

assert_equal(result, expected_result)


def test_domains_nameserver():
api = get_api()
domain_name = test_register_domain()

nameserver = f'ns1.{domain_name}'
orig_ip = '1.2.3.4'
new_ip = '11.22.33.44'

res = api.domains_ns_create(domain_name, nameserver=nameserver, ip=orig_ip)

assert_equal(res['Domain'], domain_name)
assert_equal(res['Nameserver'], nameserver)
assert_equal(res['IP'], orig_ip)
ok_(is_true(res['IsSuccess']))

res = api.domains_ns_update(domain_name, nameserver=nameserver, old_ip=orig_ip, ip=new_ip)

assert_equal(res['Domain'], domain_name)
assert_equal(res['Nameserver'], nameserver)
ok_(is_true(res['IsSuccess']))

res = api.domains_ns_getInfo(domain_name, nameserver=nameserver)

assert_equal(res['Domain'], domain_name)
assert_equal(res['Nameserver'], nameserver)
assert_equal(res['IP'], new_ip)

res = api.domains_ns_delete(domain_name, nameserver=nameserver)

assert_equal(res['Domain'], domain_name)
assert_equal(res['Nameserver'], nameserver)
ok_(is_true(res['IsSuccess']))