11import datetime
22import hashlib
33import json
4- from typing import Optional , Dict , List
4+ from typing import Dict , List , Optional
55
66import requests
77
88from lexicon .exceptions import AuthenticationError , LexiconError
99from lexicon .providers .base import Provider as BaseProvider
1010
11- NAMESERVER_DOMAINS = [
12- "wedos.net" ,
13- "wedos.eu" ,
14- "wedos.cz" ,
15- "wedos.com" ]
11+ NAMESERVER_DOMAINS = ["wedos.net" , "wedos.eu" , "wedos.cz" , "wedos.com" ]
1612
1713
1814def provider_parser (subparser ):
@@ -55,13 +51,12 @@ def __init__(self, config):
5551
5652 @staticmethod
5753 def _auth_hash (login , password ):
58- passhash = hashlib .sha1 (password .encode (' utf8' )).hexdigest ()
59- phrase = login + passhash + datetime .datetime .now ().strftime ('%H' )
60- return hashlib .sha1 (phrase .encode (' utf8' )).hexdigest ()
54+ passhash = hashlib .sha1 (password .encode (" utf8" )).hexdigest ()
55+ phrase = login + passhash + datetime .datetime .now ().strftime ("%H" )
56+ return hashlib .sha1 (phrase .encode (" utf8" )).hexdigest ()
6157
6258 def _authenticate (self ):
63-
64- payload = self ._post (data = self ._create_payload ('dns-domains-list' , '' ))
59+ payload = self ._post (data = self ._create_payload ("dns-domains-list" , "" ))
6560 domains = payload ["response" ]["data" ]["domain" ]
6661 for record in domains :
6762 if record ["name" ] == self .domain :
@@ -73,28 +68,33 @@ def _authenticate(self):
7368 self .domain_id = self .domain
7469
7570 def _create_payload (self , command , payload_data ):
76- data = {'request' : {'user' : self ._get_provider_option ("auth_username" ),
77- 'auth' : self ._auth_hash (self ._get_provider_option ("auth_username" ),
78- self ._get_provider_option ("auth_pass" )),
79- 'command' : command ,
80- 'data' : payload_data
81- }}
82- return {'request' : json .dumps (data )}
71+ data = {
72+ "request" : {
73+ "user" : self ._get_provider_option ("auth_username" ),
74+ "auth" : self ._auth_hash (
75+ self ._get_provider_option ("auth_username" ),
76+ self ._get_provider_option ("auth_pass" ),
77+ ),
78+ "command" : command ,
79+ "data" : payload_data ,
80+ }
81+ }
82+ return {"request" : json .dumps (data )}
8383
8484 def _create_record (self , rtype : str , name : str , content : str ) -> bool :
8585 records = self ._list_records (rtype , name , content )
8686 if len (records ) == 1 :
8787 return True
8888 data = {
89- ' type' : rtype ,
90- ' name' : self ._full_name (name ),
91- ' rdata' : content ,
92- ' domain' : self .domain_id
89+ " type" : rtype ,
90+ " name" : self ._full_name (name ),
91+ " rdata" : content ,
92+ " domain" : self .domain_id ,
9393 }
9494 if self ._get_lexicon_option ("ttl" ):
9595 data ["ttl" ] = self ._get_lexicon_option ("ttl" )
9696
97- payload = self ._post (data = self ._create_payload (' dns-row-add' , data ))
97+ payload = self ._post (data = self ._create_payload (" dns-row-add" , data ))
9898 code = payload ["response" ]["code" ]
9999 if code == 1000 :
100100 validation = self ._commit_changes ()
@@ -105,9 +105,13 @@ def _create_record(self, rtype: str, name: str, content: str) -> bool:
105105 else :
106106 raise LexiconError ("Cannot create records" )
107107
108- def _list_records (self , rtype : Optional [str ] = None , name : Optional [str ] = None , content : Optional [str ] = None ) -> \
109- List [Dict ]:
110- data = self ._create_payload ('dns-rows-list' , {'domain' : self .domain_id })
108+ def _list_records (
109+ self ,
110+ rtype : Optional [str ] = None ,
111+ name : Optional [str ] = None ,
112+ content : Optional [str ] = None ,
113+ ) -> List [Dict ]:
114+ data = self ._create_payload ("dns-rows-list" , {"domain" : self .domain_id })
111115 payload = self ._post (data = data )
112116 records = []
113117 dns_records = payload ["response" ]["data" ]["row" ]
@@ -123,14 +127,21 @@ def _list_records(self, rtype: Optional[str] = None, name: Optional[str] = None,
123127 if rtype is not None :
124128 records = list (rec for rec in records if _filter_rtype (rtype , rec ))
125129 if name is not None :
126- records = list (rec for rec in records if _filter_name (self ._full_name (name ), rec ))
130+ records = list (
131+ rec for rec in records if _filter_name (self ._full_name (name ), rec )
132+ )
127133 if content is not None :
128134 records = list (rec for rec in records if _filter_content (content , rec ))
129135
130136 return records
131137
132- def _update_record (self , identifier : Optional [str ] = None , rtype : Optional [str ] = None , name : Optional [str ] = None ,
133- content : Optional [str ] = None ) -> bool :
138+ def _update_record (
139+ self ,
140+ identifier : Optional [str ] = None ,
141+ rtype : Optional [str ] = None ,
142+ name : Optional [str ] = None ,
143+ content : Optional [str ] = None ,
144+ ) -> bool :
134145 if not identifier :
135146 records = self ._list_records (rtype , name , content )
136147 identifiers = [record ["id" ] for record in records ]
@@ -142,17 +153,18 @@ def _update_record(self, identifier: Optional[str] = None, rtype: Optional[str]
142153 payloads = []
143154 for record_id in identifiers :
144155 data = {
145- 'type' : rtype ,
146-
147- 'rdata' : content ,
148- 'domain' : self .domain_id ,
149- 'row_id' : record_id
156+ "type" : rtype ,
157+ "rdata" : content ,
158+ "domain" : self .domain_id ,
159+ "row_id" : record_id ,
150160 }
151161 if name :
152- data [' name' ] = self ._full_name (name )
162+ data [" name" ] = self ._full_name (name )
153163 if self ._get_lexicon_option ("ttl" ):
154164 data ["ttl" ] = self ._get_lexicon_option ("ttl" )
155- payloads .append (self ._post (data = self ._create_payload ('dns-row-update' , data )))
165+ payloads .append (
166+ self ._post (data = self ._create_payload ("dns-row-update" , data ))
167+ )
156168
157169 if all (payload ["response" ]["code" ] == 1000 for payload in payloads ):
158170 validation = self ._commit_changes ()
@@ -163,8 +175,13 @@ def _update_record(self, identifier: Optional[str] = None, rtype: Optional[str]
163175 else :
164176 raise LexiconError ("Cannot update records" )
165177
166- def _delete_record (self , identifier : Optional [str ] = None , rtype : Optional [str ] = None , name : Optional [str ] = None ,
167- content : Optional [str ] = None ) -> bool :
178+ def _delete_record (
179+ self ,
180+ identifier : Optional [str ] = None ,
181+ rtype : Optional [str ] = None ,
182+ name : Optional [str ] = None ,
183+ content : Optional [str ] = None ,
184+ ) -> bool :
168185 if not identifier :
169186 records = self ._list_records (rtype , name , content )
170187 identifiers = [record ["id" ] for record in records ]
@@ -175,11 +192,10 @@ def _delete_record(self, identifier: Optional[str] = None, rtype: Optional[str]
175192 return True
176193 payloads = []
177194 for record_id in identifiers :
178- data = {
179- 'domain' : self .domain_id ,
180- 'row_id' : record_id
181- }
182- payloads .append (self ._post (data = self ._create_payload ('dns-row-delete' , data )))
195+ data = {"domain" : self .domain_id , "row_id" : record_id }
196+ payloads .append (
197+ self ._post (data = self ._create_payload ("dns-row-delete" , data ))
198+ )
183199
184200 if all (payload ["response" ]["code" ] == 1000 for payload in payloads ):
185201 validation = self ._commit_changes ()
@@ -191,11 +207,9 @@ def _delete_record(self, identifier: Optional[str] = None, rtype: Optional[str]
191207 raise LexiconError ("Cannot delete records" )
192208
193209 def _commit_changes (self ) -> bool :
194- data = {
195- 'name' : self .domain_id
196- }
210+ data = {"name" : self .domain_id }
197211
198- payload = self ._post (data = self ._create_payload (' dns-domain-commit' , data ))
212+ payload = self ._post (data = self ._create_payload (" dns-domain-commit" , data ))
199213 code = payload ["response" ]["code" ]
200214 if code == 1000 :
201215 return True
0 commit comments