-
Notifications
You must be signed in to change notification settings - Fork 0
/
ncert_core.py
66 lines (52 loc) · 2.13 KB
/
ncert_core.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# test oop
import urllib.parse
import urllib.request
import yaml
from XMLpars import XMLparser
class Requester(object):
"""Call/Response operations"""
def __init__(self, url, params):
self.url = url
self.params = params
def param_ready(self):
"""Prepare params for the call"""
self.params = urllib.parse.urlencode(self.params) # params should be url encoded
self.params = self.params.encode('ascii') # data should be bytes for POST
return self.params
def api_call(self):
"""Make a call to NC api"""
self.req = urllib.request.Request(self.url, self.param_ready())
return self.req
# this one shows response of NC api
def show_response(self):
with urllib.request.urlopen(self.api_call()) as response:
the_page = response.read()
return the_page
class GlobalParams(object):
"""Working with global API params"""
def __init__(self):
self.url = "https://api.sandbox.namecheap.com/xml.response"
self.params = self.get_global_params()
def get_global_params(self):
with open('nc_api_config.yaml', 'r') as stream:
config = yaml.load(stream)
return config['GlobalParams']
class Interactor(object):
"""Interaction with user and action"""
def __init__(self):
self.interact()
def interact(self):
print("Hi. This is ncert. Want to know what I can? (y/n)")
action = input("> ")
if action == 'y':
# this is the instance of the Requester class with url and params as the arguments
call_request = Requester(GlobalParams().url,
GlobalParams().params)
# this is calling a function call_request() to get the data from request
api_response = call_request.show_response() # .decode('utf-8') - for XML tree response
cert_list = XMLparser(api_response).get_cert_available()
print(cert_list)
print(f"I have {len(cert_list)} new certs that I can use, and here are the IDs: \n", cert_list)
else:
exit(1)
Interactor()