-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPIOMFClient.py
162 lines (137 loc) · 4.57 KB
/
PIOMFClient.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
from __future__ import annotations
from enum import Enum
import logging
import requests
import json
import gzip
class OMFMessageType(Enum):
"""
enum 0-2
"""
Type = 'Type'
Container = 'Container'
Data = 'Data'
class OMFMessageAction(Enum):
"""
enum 0-2
"""
Create = 'Create'
Update = 'Update'
Delete = 'Delete'
class PIOMFClient(object):
"""Handles communication with PI OMF Endpoint."""
def __init__(self, url: str, data_archive: str, username: str,
password: str, omf_version: str = '1.2', verify_ssl: bool = True, logging_enabled: bool = False):
self.__url = url
self.__data_archive = data_archive
self.__username = username
self.__password = password
self.__omf_version = omf_version
self.__verify_ssl = verify_ssl
self.__logging_enabled = logging_enabled
self.__omf_endpoint = f'{url}/omf'
self.__session = requests.Session()
@property
def Url(self) -> str:
"""
Gets the base url
:return:
"""
return self.__url
@property
def DataArchive(self) -> str:
"""
Gets the data archive
:return:
"""
return self.__data_archive
@property
def Username(self) -> str:
"""
Gets the username
:return:
"""
return self.__username
@property
def Password(self) -> str:
"""
Gets the password
:return:
"""
return self.__password
@property
def OMFVersion(self) -> str:
"""
Gets the omf version
:return:
"""
return self.__omf_version
@property
def VerifySSL(self) -> bool:
"""
Gets whether SSL should be verified
:return:
"""
return self.__verify_ssl
@property
def LoggingEnabled(self) -> bool:
"""
Whether logging is enabled (default False)
:return:
"""
return self.LoggingEnabled
@LoggingEnabled.setter
def logging_enabled(self, value: bool):
self.LoggingEnabled = value
@property
def OMFEndpoint(self) -> str:
"""
Gets the omf endpoint
:return:
"""
return self.__omf_endpoint
def verifySuccessfulResponse(self, response, main_message: str, throw_on_bad: bool = True):
"""
Verifies that a response was successful and optionally throws an exception on a bad response
:param response: Http response
:param main_message: Message to print in addition to response information
:param throw_on_bad: Optional parameter to throw an exception on a bad response
"""
# response code in 200s if the request was successful!
if response.status_code < 200 or response.status_code >= 300:
response.close()
if self.__logging_enabled:
logging.info(f'request executed in {response.elapsed.microseconds / 1000}ms - status code: {response.status_code}')
logging.debug(f'{main_message}. Response: {response.status_code} {response.text}.')
if throw_on_bad:
raise Exception(
f'{main_message}. Response: {response.status_code} {response.text}. ')
def omfRequest(self, message_type: OMFMessageType, action: OMFMessageAction, omf_message_json: dict[str, str]):
"""
Base OMF request function
:param message_type: OMF message type
:param action: OMF action
:param omf_message_json: OMF message
:return: Http response
"""
if not self.VerifySSL:
print('You are not verifying the certificate of the end point. This is not advised for any system as there are security issues with doing this.')
if self.__logging_enabled:
logging.warning(f'You are not verifying the certificate of the end point. This is not advised for any system as there are security issues with doing this.')
msg_body = gzip.compress(bytes(json.dumps(omf_message_json), 'utf-8'))
msg_headers = {
'messagetype': message_type.value,
'action': action.value,
'messageformat': 'JSON',
'omfversion': self.OMFVersion,
'compression': 'gzip',
'x-requested-with': 'xmlhttprequest'
}
return self.__session.post(
self.OMFEndpoint,
headers=msg_headers,
data=msg_body,
verify=self.VerifySSL,
timeout=10000,
auth=(self.Username, self.Password)
)