-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
104 lines (80 loc) · 2.84 KB
/
example.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import logging
import os
from datetime import datetime
import names
from secrets import API_KEY
from swagger_client import Configuration, ApiClient, ApiApi, Customer
HOST = "http://localhost:8000/"
BASE_DIR = os.path.dirname(__file__)
DOWNLOADS_DIRECTORY = os.path.join(BASE_DIR, "downloads")
CONSOLE_LOGGING_FORMAT = (
'%(asctime)s %(levelname)-8s %(name)s.%(funcName)s: %(message)s'
)
logger = logging.getLogger(__name__)
def create_customer_example():
"""
In this example:
- An API is instantiated
- A randome name is generated and a Customer is created with that name
:return:
"""
logger.info("Creating a Customer")
# Instantiate an API
api = ApiApi(ApiClient(get_config()))
# Generate a random person's name using the names library
name = names.get_full_name()
# Create a Customer with the random name
customer = api.create_customer(
body=Customer(name=name, email="fake@example.com", password="password")
)
# Write the Customer to the logger
logger.info(f"Customer: {customer}")
def list_customers_example():
"""
In this example:
- An API is instantiated
- A list of Customers is retrieved and written to the logger
:return:
"""
logger.info("Listing Customers")
# Instantiate an API
api = ApiApi(ApiClient(get_config()))
# Get a list of Customers
customers = api.list_customers()
# Write the list of Customers to the logger
for customer in customers:
logger.info(f"Customer: {customer}")
def get_config():
"""
This is an example of how one might get a config.
Feel free - and encouraged - to copy and modify this function in your application!
In this code, API_KEY is a constant that is provided to the running code in a
secure way. The way in which you provide the API_KEY is defined by your
implementation, and it is your responsibility to secure this secret!
"""
if not os.path.exists(DOWNLOADS_DIRECTORY):
os.makedirs(DOWNLOADS_DIRECTORY)
configuration = Configuration()
configuration.api_key["Authorization"] = API_KEY
configuration.api_key_prefix["Authorization"] = "Token "
configuration.host = HOST
configuration.temp_folder_path = DOWNLOADS_DIRECTORY
return configuration
def configure_logging():
logging.basicConfig(
level=logging.INFO,
handlers=[
logging.FileHandler(filename="saspg_compare.log"),
logging.StreamHandler()
],
format=CONSOLE_LOGGING_FORMAT
)
if __name__ == "__main__":
configure_logging()
start_time = datetime.now()
logger.info("Starting example.py at %s", start_time)
create_customer_example()
list_customers_example()
end_time = datetime.now()
duration = end_time - start_time
logger.info("Finished example.py at %s. Duration: %s", end_time, duration)