trying to enhance the 'library'
This commit is contained in:
parent
2b3c636d0f
commit
99c090fc62
1 changed files with 40 additions and 25 deletions
|
@ -1,6 +1,10 @@
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
|
||||||
class DnsRecord:
|
class DnsRecord:
|
||||||
|
"""
|
||||||
|
DNS record of a given domain
|
||||||
|
"""
|
||||||
def __init__(self, domain, name: str, record_type: str, content: str, session):
|
def __init__(self, domain, name: str, record_type: str, content: str, session):
|
||||||
self.domain = domain
|
self.domain = domain
|
||||||
self.name = name
|
self.name = name
|
||||||
|
@ -13,6 +17,9 @@ class DnsRecord:
|
||||||
|
|
||||||
|
|
||||||
def edit_content(self, content: str):
|
def edit_content(self, content: str):
|
||||||
|
"""
|
||||||
|
Edit the content of a DNS record
|
||||||
|
"""
|
||||||
URL = self.session['base_url'] + f"dns/editByNameType/{self.domain}/{self.record_type}/{self.record}"
|
URL = self.session['base_url'] + f"dns/editByNameType/{self.domain}/{self.record_type}/{self.record}"
|
||||||
DATA = {
|
DATA = {
|
||||||
'secretapikey': self.session['secret_api_key'],
|
'secretapikey': self.session['secret_api_key'],
|
||||||
|
@ -31,9 +38,8 @@ class Domain:
|
||||||
self.session = session
|
self.session = session
|
||||||
|
|
||||||
|
|
||||||
def get_records(self) -> list[DnsRecord]:
|
def get_records(self) -> list[DnsRecord] | None:
|
||||||
URL = self.session['base_url'] + f"dns/retrieve/{self.domain}"
|
URL = self.session['base_url'] + f"dns/retrieve/{self.domain}"
|
||||||
|
|
||||||
REQUEST_DATA = {
|
REQUEST_DATA = {
|
||||||
'secretapikey': self.session['secret_api_key'],
|
'secretapikey': self.session['secret_api_key'],
|
||||||
'apikey': self.session['api_key']
|
'apikey': self.session['api_key']
|
||||||
|
@ -42,24 +48,27 @@ class Domain:
|
||||||
response = requests.post(url=URL, json=REQUEST_DATA).json()
|
response = requests.post(url=URL, json=REQUEST_DATA).json()
|
||||||
|
|
||||||
records_to_return = []
|
records_to_return = []
|
||||||
|
if 'records' in response:
|
||||||
|
for record in response['records']:
|
||||||
|
records_to_return.append(DnsRecord(
|
||||||
|
domain=self.domain,
|
||||||
|
name=record['name'],
|
||||||
|
record_type=record['type'],
|
||||||
|
content=record['content'],
|
||||||
|
session=self.session
|
||||||
|
))
|
||||||
|
|
||||||
for record in response['records']:
|
return records_to_return
|
||||||
records_to_return.append(DnsRecord(
|
else:
|
||||||
domain=self.domain,
|
raise Exception("Could not retrieve records list" + response["message"])
|
||||||
name=record['name'],
|
|
||||||
record_type=record['type'],
|
|
||||||
content=record['content'],
|
|
||||||
session=self.session
|
|
||||||
))
|
|
||||||
|
|
||||||
return records_to_return
|
|
||||||
|
|
||||||
|
|
||||||
def get_record_by_name(self, name: str):
|
def get_record_by_name(self, name: str):
|
||||||
records = self.get_records()
|
records = self.get_records()
|
||||||
for record in records:
|
if records is not None:
|
||||||
if record.record == name:
|
for record in records:
|
||||||
return record
|
if record.record == name:
|
||||||
|
return record
|
||||||
|
|
||||||
|
|
||||||
class Porkbun:
|
class Porkbun:
|
||||||
|
@ -70,6 +79,9 @@ class Porkbun:
|
||||||
|
|
||||||
|
|
||||||
def get_domains(self) -> list[Domain]:
|
def get_domains(self) -> list[Domain]:
|
||||||
|
"""
|
||||||
|
Get domains by user
|
||||||
|
"""
|
||||||
DOMAINS_URL = self.URL + "domain/listAll"
|
DOMAINS_URL = self.URL + "domain/listAll"
|
||||||
|
|
||||||
REQUEST_DATA = {
|
REQUEST_DATA = {
|
||||||
|
@ -77,21 +89,24 @@ class Porkbun:
|
||||||
"apikey": self.api_key
|
"apikey": self.api_key
|
||||||
}
|
}
|
||||||
|
|
||||||
response = requests.post(url=DOMAINS_URL, json=REQUEST_DATA).json()
|
response: dict = requests.post(url=DOMAINS_URL, json=REQUEST_DATA).json()
|
||||||
|
|
||||||
domains_to_return = []
|
domains_to_return = []
|
||||||
|
|
||||||
for domain in response['domains']:
|
if "domains" in response:
|
||||||
domains_to_return.append(Domain(
|
for domain in response['domains']:
|
||||||
domain=domain['domain'],
|
domains_to_return.append(Domain(
|
||||||
tld=domain['tld'],
|
domain=domain['domain'],
|
||||||
session={'api_key': self.api_key, 'secret_api_key': self.secret_api_key, 'base_url': self.URL}
|
tld=domain['tld'],
|
||||||
))
|
session={'api_key': self.api_key, 'secret_api_key': self.secret_api_key, 'base_url': self.URL}
|
||||||
|
))
|
||||||
|
|
||||||
return domains_to_return
|
return domains_to_return
|
||||||
|
else:
|
||||||
|
raise Exception("Could not retrieve the domains list for the given credentials ")
|
||||||
|
|
||||||
|
|
||||||
def get_domain_by_name(self, name: str) -> Domain:
|
def get_domain_by_name(self, name: str) -> Domain | None:
|
||||||
domains = self.get_domains()
|
domains = self.get_domains()
|
||||||
for domain in domains:
|
for domain in domains:
|
||||||
if domain.domain == name:
|
if domain.domain == name:
|
||||||
|
|
Loading…
Reference in a new issue