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
|
||||
|
||||
|
||||
class DnsRecord:
|
||||
"""
|
||||
DNS record of a given domain
|
||||
"""
|
||||
def __init__(self, domain, name: str, record_type: str, content: str, session):
|
||||
self.domain = domain
|
||||
self.name = name
|
||||
|
@ -13,6 +17,9 @@ class DnsRecord:
|
|||
|
||||
|
||||
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}"
|
||||
DATA = {
|
||||
'secretapikey': self.session['secret_api_key'],
|
||||
|
@ -31,9 +38,8 @@ class Domain:
|
|||
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}"
|
||||
|
||||
REQUEST_DATA = {
|
||||
'secretapikey': self.session['secret_api_key'],
|
||||
'apikey': self.session['api_key']
|
||||
|
@ -42,24 +48,27 @@ class Domain:
|
|||
response = requests.post(url=URL, json=REQUEST_DATA).json()
|
||||
|
||||
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']:
|
||||
records_to_return.append(DnsRecord(
|
||||
domain=self.domain,
|
||||
name=record['name'],
|
||||
record_type=record['type'],
|
||||
content=record['content'],
|
||||
session=self.session
|
||||
))
|
||||
|
||||
return records_to_return
|
||||
return records_to_return
|
||||
else:
|
||||
raise Exception("Could not retrieve records list" + response["message"])
|
||||
|
||||
|
||||
def get_record_by_name(self, name: str):
|
||||
records = self.get_records()
|
||||
for record in records:
|
||||
if record.record == name:
|
||||
return record
|
||||
if records is not None:
|
||||
for record in records:
|
||||
if record.record == name:
|
||||
return record
|
||||
|
||||
|
||||
class Porkbun:
|
||||
|
@ -70,6 +79,9 @@ class Porkbun:
|
|||
|
||||
|
||||
def get_domains(self) -> list[Domain]:
|
||||
"""
|
||||
Get domains by user
|
||||
"""
|
||||
DOMAINS_URL = self.URL + "domain/listAll"
|
||||
|
||||
REQUEST_DATA = {
|
||||
|
@ -77,21 +89,24 @@ class Porkbun:
|
|||
"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 = []
|
||||
|
||||
for domain in response['domains']:
|
||||
domains_to_return.append(Domain(
|
||||
domain=domain['domain'],
|
||||
tld=domain['tld'],
|
||||
session={'api_key': self.api_key, 'secret_api_key': self.secret_api_key, 'base_url': self.URL}
|
||||
))
|
||||
|
||||
return domains_to_return
|
||||
if "domains" in response:
|
||||
for domain in response['domains']:
|
||||
domains_to_return.append(Domain(
|
||||
domain=domain['domain'],
|
||||
tld=domain['tld'],
|
||||
session={'api_key': self.api_key, 'secret_api_key': self.secret_api_key, 'base_url': self.URL}
|
||||
))
|
||||
|
||||
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()
|
||||
for domain in domains:
|
||||
if domain.domain == name:
|
||||
|
|
Loading…
Reference in a new issue