diff --git a/Examples/example_library.py b/Examples/example_library.py new file mode 100644 index 0000000..67cbf7f --- /dev/null +++ b/Examples/example_library.py @@ -0,0 +1,124 @@ +import json +from os.path import join +import os +from dotenv import load_dotenv +from iLibrary import Library + +#load ENV file and get the Connection Settings +dotenv_path = join('..', '.env') +load_dotenv(dotenv_path) +DB_DRIVER = os.environ.get("DB_DRIVER") +DB_USER = os.environ.get("DB_USER") +DB_PASSWORD = os.environ.get("DB_PASSWORD") +DB_SYSTEM = os.environ.get("DB_SYSTEM") + + + + +# ---------------------------------------------------- +# make a Savefile from a library +# ---------------------------------------------------- +def getSaveFile(): + + # Flag to enable/disable Mapepire connection mode + USE_MAPEPIRE = False + + + PORT = 22 #for the Pub400.com use the Port 2222 + LIBRARY = '' + SAVE_FILE_NAME = 'FOO' + LOCAL_PATH = 'YOUR_LOCAL_PATH' + DESCRIPTION = 'Saved from iLibrary' + try: + # Establish a connection to the IBM i system using the User class + # The context manager ensures the connection is properly opened and closed + with Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=USE_MAPEPIRE) as l: + + # Call the method to save the Library + # The result is returned as a JSON string + raw_result = l.saveLibrary( + library=LIBRARY, + saveFileName=SAVE_FILE_NAME, + description=DESCRIPTION, + localPath=LOCAL_PATH, + port=PORT + ) + + # Parse the JSON string into a Python object (list/dictionary) + data = json.loads(raw_result) + + # Pretty-print the parsed data with indentation for readability + print(json.dumps(data, indent=4)) + + # Handle any exceptions that occur during connection or data retrieval + except Exception as e: + # Print the error message for debugging + print(e) + + + +# ---------------------------------------------------- +# Get library information about all libraries from +# IBM i Server using iLibrary +# ---------------------------------------------------- +def getAllLibraries(): + # Flag to enable/disable Mapepire connection mode + USE_MAPEPIRE = False + + try: + # Establish a connection to the IBM i system using the User class + # The context manager ensures the connection is properly opened and closed + with Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=USE_MAPEPIRE) as l: + + # Call the method to get all libraries on the system + # The result is returned as a JSON string + raw_result = l.getAllLibraries() + + # Parse the JSON string into a Python object (list/dictionary) + data = json.loads(raw_result) + + # Pretty-print the parsed data with indentation for readability + print(json.dumps(data, indent=4)) + + # Handle any exceptions that occur during connection or data retrieval + except Exception as e: + # Print the error message for debugging + print(e) + + + + +# ---------------------------------------------------- +# Get single library information from IBM i Server +# using iLibrary +# ---------------------------------------------------- +def getSingleLibraryInfo(): + # Flag to enable/disable Mapepire connection mode + USE_MAPEPIRE = False + LIBRARY = '' + try: + # Establish a connection to the IBM i system using the User class + # The context manager ensures the connection is properly opened and closed + with Library(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=USE_MAPEPIRE) as l: + + # Call the method to show singe information about the Library + # The result is returned as a JSON string + raw_result = l.getLibraryInfo( + library=LIBRARY + ) + + # Parse the JSON string into a Python object (list/dictionary) + data = json.loads(raw_result) + + # Pretty-print the parsed data with indentation for readability + print(json.dumps(data, indent=4)) + + # Handle any exceptions that occur during connection or data retrieval + except Exception as e: + # Print the error message for debugging + print(e) + +if __name__ == '__main__': + getSaveFile() + getAllLibraries() + getSingleLibraryInfo() \ No newline at end of file diff --git a/Examples/example_user.py b/Examples/example_user.py index c2a6334..d1de08f 100644 --- a/Examples/example_user.py +++ b/Examples/example_user.py @@ -12,14 +12,12 @@ DB_PASSWORD = os.environ.get("DB_PASSWORD") DB_SYSTEM = os.environ.get("DB_SYSTEM") -if __name__ == "__main__": - # ---------------------------------------------------- # Get all Users information from IBM i Server using # iLibrary # ---------------------------------------------------- - +def getAllUsers(): # Flag to enable/disable Mapepire connection mode USE_MAPEPIRE = False @@ -43,11 +41,13 @@ # Print the error message for debugging print(e) + + # ---------------------------------------------------- # Get single Users information from IBM i Server # using iLibrary # ---------------------------------------------------- - +def getSingleUser(): # Flag to enable/disable Mapepire connection mode USE_MAPEPIRE = False @@ -77,6 +77,43 @@ # Output the error message for debugging purposes print(e) +# ---------------------------------------------------- +# Send a message to a IBM i User using iLibrary +# ---------------------------------------------------- +def sendMessage(): + # Flag to enable/disable Mapepire connection mode + USE_MAPEPIRE = False + + # Username to search for in the IBM i system + USERNAME_TO_SEARCH = '' + MESSAGE_TO_SEND = 'From iLibrary' + + try: + # Establish a connection to the IBM i system using the User class + # The context manager ensures proper connection handling (open/close) + with User(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=USE_MAPEPIRE) as u: + + # Retrieve detailed information for a single user + # - username: the user profile to look up + # Returns data as a JSON string + raw_result = u.send_message_to_user( + username=USERNAME_TO_SEARCH, + message=MESSAGE_TO_SEND, + ) + + # Convert the JSON string into a Python object (dict or list) + data = json.loads(raw_result) -#TODO --------------------------------------------- -#TODO Write the SendMessage Function to here \ No newline at end of file + # Pretty-print the result for better readability + print(json.dumps(data, indent=4)) + + # Handle any exceptions that occur during execution + except Exception as e: + # Output the error message for debugging purposes + print(e) + + +if __name__ == "__main__": + getAllUsers() + getSingleUser() + sendMessage() \ No newline at end of file diff --git a/app/iLibrary/src/iLibrary/Usr/sendMSG.py b/app/iLibrary/src/iLibrary/Usr/sendMSG.py index ce44b6f..bf55ad7 100644 --- a/app/iLibrary/src/iLibrary/Usr/sendMSG.py +++ b/app/iLibrary/src/iLibrary/Usr/sendMSG.py @@ -22,7 +22,7 @@ def send_message_to_user( # msgtype: str = None, # rpymsgq: str = None, ccsid: int = None - ): + ) -> dict[str, str]: """ Sends a message to a specified user on the system. This method interacts with the system to send a message by executing an SQL query. It validates the required diff --git a/dev_test.py b/dev_test.py new file mode 100644 index 0000000..00b3de5 --- /dev/null +++ b/dev_test.py @@ -0,0 +1,18 @@ +import json +from os.path import join, dirname +import os +from dotenv import load_dotenv +from iLibrary import Library, User, IFS +from os.path import dirname + +#load ENV file and get the Connection Settings +dotenv_path = join(dirname(__file__), '.env') +load_dotenv(dotenv_path) +DB_DRIVER = os.environ.get("DB_DRIVER") +DB_USER = os.environ.get("DB_USER") +DB_PASSWORD = os.environ.get("DB_PASSWORD") +DB_SYSTEM = os.environ.get("DB_SYSTEM") + + +if __name__ == "__main__": + print('Nothing to do') \ No newline at end of file diff --git a/test.py b/test.py deleted file mode 100644 index 4e550b7..0000000 --- a/test.py +++ /dev/null @@ -1,38 +0,0 @@ -import json -from os.path import join, dirname -import os -from dotenv import load_dotenv -from iLibrary import Library, User, IFS -from os.path import dirname - -#load ENV file and get the Connection Settings -dotenv_path = join(dirname(__file__), '.env') -load_dotenv(dotenv_path) -DB_DRIVER = os.environ.get("DB_DRIVER") -DB_USER = os.environ.get("DB_USER") -DB_PASSWORD = os.environ.get("DB_PASSWORD") -DB_SYSTEM = os.environ.get("DB_SYSTEM") - - -if __name__ == "__main__": - try: - #try to get a connection to the Servcer - with IFS(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=False) as i: - - path = '/home/ALBEER/' - raw_result = i.readIFS(path_to_read=path, subtrees=False) - data = json.loads(raw_result) - print(json.dumps(data, indent=2)) - except Exception as e: - print(f"An error occurred: {e}") - - # - # try: - # with User(DB_USER, DB_PASSWORD, DB_SYSTEM, DB_DRIVER, mapepire=False) as i: - # path = '/a' - # data = i.getSingleUserInformation('Test') - # # print(data) - # - # print(data) - # except Exception as e: - # print(f"An error occurred: {e}") \ No newline at end of file