Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding adhomf clients #5

Merged
merged 8 commits into from
Jan 22, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@
## 0.1.0_preview / 2023-11-09

- Initial release

## 0.1.2_preview / 2025-1-16

- Adding support for fanning to multiple clients (can be different endpoints)
33 changes: 21 additions & 12 deletions omf_sample_library_preview/Client/OMFClient.py
derekendres marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def OMFEndpoint(self) -> str:
"""
return self.__omf_endpoint

@staticmethod
def compressOMFMessage(omf_message) -> bytes:
omf_message_json = [obj.toDictionary() for obj in omf_message]
body = json.dumps(omf_message_json)
logging.debug(f"omf body: {body}")
compressed_body = gzip.compress(bytes(body, 'utf-8'))
gzip.open
return compressed_body

def verifySuccessfulResponse(
self, response, main_message: str, throw_on_bad: bool = True
):
Expand Down Expand Up @@ -137,16 +146,11 @@ def containerRequest(
):
self.omfRequest(OMFMessageType.Container, action, containers)

def containerRequest(
self, action: OMFMessageAction, containers: list[OMFContainer]
):
self.omfRequest(OMFMessageType.Container, action, containers)

def omfRequest(
self,
message_type: OMFMessageType,
action: OMFMessageAction,
omf_message: list[OMFType | OMFContainer | OMFData | OMFLinkData],
omf_message: list[OMFType | OMFContainer | OMFData | OMFLinkData] | bytes,
) -> requests.Response:
"""
Base OMF request function
Expand All @@ -156,13 +160,18 @@ def omfRequest(
:return: Http response
"""

if type(omf_message) is not list:
raise TypeError('Omf messages must be a list')
if type(omf_message) is not list and type(omf_message) is not bytes:
raise TypeError('Omf messages must be a list or bytes')

compressed_body = {}
if type(omf_message) is not bytes:
compressed_body = self.compressOMFMessage(omf_message)
else:
if omf_message[0] == 31 and omf_message[1] == 139:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this is a "magic number" check, adding a comment here or maybe even better moving the byte checking logic to a function like IsGzipFormat() would make this more readable

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw this comment was resolved but I don't see any change associated with it. Was this addressed?

compressed_body = omf_message
else:
raise TypeError('Omf messages must be gzip bytes')

omf_message_json = [obj.toDictionary() for obj in omf_message]
body = json.dumps(omf_message_json)
logging.debug(f"omf body: {body}")
compressed_body = gzip.compress(bytes(body, 'utf-8'))
headers = self.getHeaders(message_type, action)

return self.request(
Expand Down
66 changes: 66 additions & 0 deletions omf_sample_library_preview/Client/OMFClients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from __future__ import annotations

from ..Models.OMFMessageAction import OMFMessageAction
from ..Models.OMFMessageType import OMFMessageType
from ..Models.OMFData import OMFData

from ..Models.OMFMessageAction import OMFMessageAction
from ..Models.OMFMessageType import OMFMessageType
from ..Models.OMFContainer import OMFContainer
from ..Models.OMFData import OMFData
from ..Models.OMFLinkData import OMFLinkData
from ..Models.OMFMessageAction import OMFMessageAction
from ..Models.OMFMessageType import OMFMessageType
from ..Models.OMFType import OMFType

import gzip
import json
import logging

from .OMFClient import OMFClient

import requests


class OMFClients(object):
"""Handles communication with OMF Endpoints."""

def __init__(
self):
self.__OMFClient: list[OMFClient] = []


@property
def Clients(self) -> list[OMFClient]:
"""
Gets the base url
:return:
"""
return self.__OMFClient

def addClient(
self,
client: OMFClient,
) -> requests.Response:
"""
"""
self.__OMFClient.append(client)

def omfRequests(
self,
message_type: OMFMessageType,
action: OMFMessageAction,
omf_data: list[OMFType | OMFContainer | OMFData | OMFLinkData],
) -> requests.Response:
"""
Base OMF request function
:param message_type: OMF message type
:param action: OMF action
:param omf_data: OMF data
:return: Http response
"""
compressed_body = OMFClient.compressOMFMessage(omf_data)

for client in self.__OMFClient:
client.omfRequest(message_type,action,compressed_body)

1 change: 1 addition & 0 deletions omf_sample_library_preview/Client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from .ADHOMFClient import ADHOMFClient
from .OMFClients import OMFClients
from .Authentication import Authentication
from .OMFClient import OMFClient
from .OMFError import OMFError
8 changes: 4 additions & 4 deletions omf_sample_library_preview/Services/ContainerService.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, omf_client: OMFClient):
def OMFClient(self) -> OMFClient:
return self.__omf_client

def createContainers(self, omf_containers: list[OMFContainer]):
def createContainers(self, omf_containers: list[OMFContainer] | bytes):
"""
Creates OMF Containers and throws error on failure
:param omf_containers: List of OMF Containers
Expand All @@ -27,7 +27,7 @@ def createContainers(self, omf_containers: list[OMFContainer]):
response, 'Failed to create container'
)

def updateContainers(self, omf_containers: list[OMFContainer]):
def updateContainers(self, omf_containers: list[OMFContainer] | bytes):
"""
Updates OMF Containers and throws error on failure
:param omf_containers: List of OMF Containers
Expand All @@ -42,7 +42,7 @@ def updateContainers(self, omf_containers: list[OMFContainer]):
response, 'Failed to update container'
)

def deleteContainers(self, omf_containers: list[OMFContainer]):
def deleteContainers(self, omf_containers: list[OMFContainer] | bytes):
"""
Deletes OMF Containers and throws error on failure
:param omf_containers: List of OMF Containers
Expand All @@ -55,4 +55,4 @@ def deleteContainers(self, omf_containers: list[OMFContainer]):
)
self.__omf_client.verifySuccessfulResponse(
response, 'Failed to delete container'
)
)
6 changes: 3 additions & 3 deletions omf_sample_library_preview/Services/DataService.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, omf_client: OMFClient):
def OMFClient(self) -> OMFClient:
return self.__omf_client

def createData(self, omf_data: list[OMFData]):
def createData(self, omf_data: list[OMFData] | bytes):
"""
Creates OMF Data and throws error on failure
:param omf_data: List of OMF Data
Expand All @@ -25,7 +25,7 @@ def createData(self, omf_data: list[OMFData]):
)
self.__omf_client.verifySuccessfulResponse(response, 'Failed to create data')

def updateData(self, omf_data: list[OMFData]):
def updateData(self, omf_data: list[OMFData] | bytes):
"""
Updates OMF Data and throws error on failure
:param omf_data: List of OMF Data
Expand All @@ -38,7 +38,7 @@ def updateData(self, omf_data: list[OMFData]):
)
self.__omf_client.verifySuccessfulResponse(response, 'Failed to update data')

def deleteData(self, omf_data: list[OMFData]):
def deleteData(self, omf_data: list[OMFData] | bytes):
"""
Deletes OMF Data and throws error on failure
:param omf_data: List of OMF Data
Expand Down
8 changes: 4 additions & 4 deletions omf_sample_library_preview/Services/TypeService.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def __init__(self, omf_client: OMFClient):
def OMFClient(self) -> OMFClient:
return self.__omf_client

def createTypes(self, omf_types: list[OMFType]):
def createTypes(self, omf_types: list[OMFType] | bytes):
"""
Creates OMF Types and throws error on failure
:param omf_types: List of OMF Types
Expand All @@ -25,7 +25,7 @@ def createTypes(self, omf_types: list[OMFType]):
)
self.__omf_client.verifySuccessfulResponse(response, 'Failed to create types')

def updateTypes(self, omf_types: list[OMFType]):
def updateTypes(self, omf_types: list[OMFType] | bytes):
"""
Updates OMF Types and throws error on failure
:param omf_types: List of OMF Types
Expand All @@ -38,7 +38,7 @@ def updateTypes(self, omf_types: list[OMFType]):
)
self.__omf_client.verifySuccessfulResponse(response, 'Failed to update types')

def deleteTypes(self, omf_types: list[OMFType]):
def deleteTypes(self, omf_types: list[OMFType] | bytes):
"""
Deletes OMF Types and throws error on failure
:param omf_types: List of OMF Types
Expand All @@ -49,4 +49,4 @@ def deleteTypes(self, omf_types: list[OMFType]):
OMFMessageAction.Delete,
omf_types,
)
self.__omf_client.verifySuccessfulResponse(response, 'Failed to delete types')
self.__omf_client.verifySuccessfulResponse(response, 'Failed to delete types')
6 changes: 3 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@

setuptools.setup(
name='omf_sample_library_preview',
version='0.1.1_preview',
version='0.1.2_preview',
author='AVEVA',
license='Apache 2.0',
author_email='samples@osisoft.com',
author_email='samples@aveva.com',
description='A preview of an OMF client library',
long_description=long_description,
long_description_content_type='text/markdown',
url='https://github.com/osisoft/sample-omf_library_preview-python',
url='https://github.com/aveva/sample-omf_library_preview-python',
packages=setuptools.find_packages(),
install_requires=['requests>=2.28.2', 'python-dateutil>=2.8.2'],
tests_require=[
Expand Down