BaseApi (Basic APIs)

asJSONPrettyString

Transform an object into a neatly formatted JSON string.

BaseApi.asJSONPrettyString(obj)

Parameters

obj: the object that needs to be converted to JSON string (can be a dict)

Sample Code

# Create a dictionary to hold product information
product = {"productId": "11"}
str_json = BaseApi.asJSONPrettyString(product)

Returns

This endpoint will return the formatted string of the dictionary, with indentation         and sorted keys, for better readability and understanding.

asJSONString

Transform an object into a non-formatted JSON string.

BaseApi.asJSONString(obj)

Parameters

obj:  the object that needs to be converted to JSON string (can be a dict)

Sample Code

product = {
    "productId": "11"
}
str_json = BaseApi.asJSONString(product)

Returns

This method returns the string representation of the object in JSON format, without formatting, for improved readability and comprehension.

fromJSONString

Parse a JSON-formatted string and convert it into a Map object.

BaseApi.fromJSONString(jsonString)

Parameters

jsonString:     A string representing a valid JSON structure.

Sample Code

jsonString = "{\n" + \
            "    \"productId\": \"abc\"\n" + \
            "}"
map = BaseApi.fromJSONString(jsonString)

Returns

A constructed dict represents the key-value pairs extracted from the provided JSON string.

fromJSONStringAsArray

Convert a json string​ as a dictionary.

BaseApi.fromJSONStringAsArray(jsonString)

Parameters

jsonString:    string representation of json

Sample Code

json_string = '[\n' + \
    '    {\n' + \
    '    "product": "abc"\n' + \
    '    }\n' + \
    ']'

mapList = BaseApi.fromJSONStringAsArray(jsonString)

Returns

the array of json objects

readJSON

Read the JSON from a file name​ located in a specific directory. It is converted into a dictionary

BaseApi.readJSON(dir, fileName)

Parameters

dir:    the directory as str
fileName:  name of the file as str

Sample Code

map = BaseApi.readJSON('/tmp/folder1', 'file.json')

Returns

json document object​ as dictionary

writeJSON

Write a dictionary object into a file​ located on a specific directory.

BaseApi.writeJSON(dir, fileName, obj)

Parameters

dir:         directory as str
fileName:    name of the file as str
obj:         json document object 

Sample Code

json_string = '[\n' + \
    '    {\n' + \
    '    "product": "abc"\n' + \
    '    }\n' + \
    ']'

BaseApi.writeJSON("/tmp/folder1", "file.json", json_string)

Returns

result of the operation​

writePrettyJSON

Write a dictionary object into a file​ located on a specific directory. The JSON information is a formatted inside the file.

BaseApi.writePrettyJSON(dir, fileName, obj)

Parameters

dir:          directory as str
fileName:     name of the file as str
obj:          json formatted document

Sample Code

json_string = '[\n' + \
    '    {\n' + \
    '    "product": "abc"\n' + \
    '    }\n' + \
    ']'

BaseApi.writePrettyJSON('/tmp/folder1', 'file.json', jsonString)

Returns

result of the operation​

successResult

Build and return a successful result dictionary object.​

BaseApi.successResult()
or
BaseApi.successResult(msg)
or
BaseApi.successResult(msg, data)

Parameters

msg  :  message to be sent as str
data :  Object

Sample Code

BaseApi.successResult('Successfully added records')

Returns

This method returns a success Result object. If provided, the message and data are encapsulated within the result.

errorResult

Build and return an error result dictionary object.​

BaseApi.errorResult(msg)
or
BaseApi.errorResult(msg, code)

Parameters

msg:     message to be sent (str)
code:    return code (int)

Sample Code

BaseApi.errorResult('Failed to process records')

Returns

This method returns an error Result object with the specified message and, optionally, a code.

extractMessage

Extract the message from the result object​. The result object has a detailed message in it therefore it will be returned.

BaseApi.extractMessage(obj)

Parameters

obj: object as a dictionary

Returns

This method extracts the message from a Result object if the object is a Result; otherwise, it returns "Unknown".

isResult

Checking whether the object is a result object​.

BaseApi.isResult(obj)

Parameters

obj:    the object as a dictionary

Sample Code

res = Result.getSuccessResult('success')
isResult = BaseApi.isResult(res)

Returns

This method returns True if the provided object is an instance of Result; otherwise, it returns False.

isResultOrNull

Checking if result object is a valid one or a just a NuLL

BaseApi.isResultOrNull(obj)

Parameters

obj:    the object as a dictionary

Sample Code

res = Result.getSuccessResult('success')
isResult = BaseApi.isResultOrNull(res)

Returns

This method returns True if the provided object is either an instance of Result or None; otherwise, it returns False.

UUID

Generate a universally unique identifier (UUID) string.

BaseApi.UUID()

Parameters

none​

Sample Code

uuid = BaseApi.UUID()

Returns

This method returns a string representing a randomly generated UUID.

uidToClassName

Extract class name​ from an input string.

BaseApi.uidToClassName(uid)

Parameters

uid:    the className id as str

Returns

This method returns a class name string derived from the provided UID.

uidToId

Extract an long id number from a string UID.

BaseApi.uidToId(uid)

Parameters

uid: str

Returns

This method returns an integer ID extracted from the provided UID string.

uidToIdStr

Retrieves​ the ID as a string from uid.

BaseApi.uidToIdStr(uid) 

Parameters

uid: str

Returns

This method returns a string representation of the ID extracted from the provided UID.

waitForMillis

Pause execution for the specified number of milliseconds.

BaseApi.waitForMillis(tm) 

Parameters

tm: long

Sample Code

res = BaseApi.waitForMillis(10)

Returns

This method returns a Result indicating the success or failure of the wait operation.

remoteCall

Perform a remote method call.

BaseApi.remoteCall(javaClassName, javaMethodName, *args)

Parameters

javaClassName: str
javaMethodName: str

Sample Code

res = remoteCall('DSApi', 'get', 'shared.common.product', '12')

Returns

This method returns the result of the remote method call.

remoteCallAsResult

Perform a remote method call and return the result as a Result object.

BaseApi.remoteCallAsResult(javaClassName, javaMethodName, *args)

Parameters

javaClassName: str
javaMethodName: str

Sample Code

res = remoteCallAsResult('BigQueryApi', 'executeQuery', 'SELECT ID FROM product_tbl')

Returns

This method returns the result of the remote method call encapsulated within a Result object.

remoteCallAsMap

Perform a remote method call and return the result as a dictionary.

BaseApi.remoteCallAsMap(javaClassName, javaMethodName, *args)

Parameters

javaClassName: str
javaMethodName: str

Sample Code

res =  remoteCallAsMap('UMApi', 'getCurrentUser')

Returns

This method returns the result of the remote method call as a dictionary.

remoteCallAsString

Perform a remote method call and return the result as a string.

BaseApi.remoteCallAsString(javaClassName, javaMethodName, *args)

Parameters

javaClassName: str
javaMethodName: str

Sample Code

res = remoteCallAsString('GCPRestApi', 'getProjectId')

Returns

This method returns the result of the remote method call as a string.

remoteCallAsList

Perform a remote method call and return the result as a list.

BaseApi.remoteCallAsList(javaClassName, javaMethodName, *args)

Parameters

javaClassName: str
javaMethodName: str

Returns

This method returns the result of the remote method call as a list.

remoteCallAsListOfMaps

Perform a remote method call and return the result as a list of dictionaries.

BaseApi.remoteCallAsListOfMaps(javaClassName, javaMethodName, *args)

Parameters

javaClassName: str
javaMethodName: str

Returns

This method returns the result of the remote method call as a list of dictionaries.

Last updated