diff --git a/ChangeLog.md b/ChangeLog.md index 0a5b597..ea40d82 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,11 @@ # Change Log +## v0.0.2.11 + +### New Features + +- Core: add function decode_api_id and encode_api_id to convert between nodeID and normal ID + ## v0.0.2.10 ### New Features diff --git a/README.md b/README.md index 9759f5d..1ee24c2 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,10 @@ This module is used to interact with the Xurrent API. It provides a set of class uri = "/requests?subject=Example Subject" connection_object.api_call(uri, 'GET') + # Convert node ID + helper.decode_api_id('ZmFiaWFuc3RlaW5lci4yNDEyMTAxMDE0MTJANG1lLWRlbW8uY29tL1JlcS83MDU3NTU') # fabiansteiner.241210101412@4me-demo.com/Req/705755 + # this can be used to derive the ID from the nodeID + ``` #### People diff --git a/pyproject.toml b/pyproject.toml index f832ae7..045c819 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "xurrent" -version = "0.0.2.10" +version = "0.0.2.11" authors = [ { name="Fabian Steiner", email="fabian@stei-ner.net" }, ] @@ -18,7 +18,7 @@ Homepage = "https://github.com/fasteiner/xurrent-python" Issues = "https://github.com/fasteiner/xurrent-python/issues" [tool.poetry] name = "xurrent" -version = "0.0.2.10" +version = "0.0.2.11" description = "A python module to interact with the Xurrent API." authors = ["Ing. Fabian Franz Steiner BSc. "] readme = "README.md" diff --git a/src/xurrent/core.py b/src/xurrent/core.py index 34aeefc..28449e1 100644 --- a/src/xurrent/core.py +++ b/src/xurrent/core.py @@ -6,6 +6,7 @@ import logging import json import re +import base64 class LogLevel(Enum): DEBUG = logging.DEBUG @@ -245,3 +246,31 @@ def create_filter_string(self, filter: dict): for key, value in filter.items(): filter_string += f'{key}={value}&' return filter_string[:-1] + + def decode_api_id(self, id: str): + """ + API resource IDs are base64-encoded strings with the padding bytes stripped off. + Ensure approproate padding and decode. + :param id: Encoded Xurrent resource ID + :return: String containing the decoded ID + >>> helper = XurrentApiHelper('https://api.example.com', 'api_key', 'account', False) + >>> helper.decode_api_id('SGVsbG8sIHdvcmxkIQ') + 'Hello, world!' + """ + #Get the length remainder of 4, fill the remainder to be a power of 4, but only if it is not already 4 + padding_count = (4 - (len(id) % 4)) % 4 + padding = "=" * padding_count + value = id + padding + return base64.decodebytes(value.encode()).decode() + + def encode_api_id(self, id: str): + """ + API resource IDs are base64-encoded strings with the padding bytes stripped off. + Encode and strip padding. + :param id: Xurrent resource ID to encode + :return: String containing the encoded ID + >>> helper = XurrentApiHelper('https://api.example.com', 'api_key', 'account', False) + >>> helper.encode_api_id('Hello, world!') + 'SGVsbG8sIHdvcmxkIQ' + """ + return base64.encodebytes(id.encode()).decode().strip().rstrip("=")