Created
September 25, 2024 11:26
-
-
Save YisusChrist/6297a20161c43506a07ca48ed8249010 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from typing import Any, Callable | |
from creatio_api_py.api import CreatioODataAPI | |
from creatio_api_py.utils import print_exception | |
from requests import Response | |
from rich import print | |
def login(url: str, username: str, password: str) -> CreatioODataAPI: | |
""" | |
Authenticate with the Creatio API and return the API object. | |
Args: | |
url (str): The URL of the Creatio instance. | |
username (str): The username to authenticate with. | |
password (str): The password to authenticate with. | |
Returns: | |
CreatioODataAPI: The API object. | |
""" | |
api = CreatioODataAPI(base_url=url) | |
try: | |
# Authenticate with the API | |
api.authenticate(username=username, password=password) | |
except Exception as e: | |
print_exception(e, f"Unable to authenticate on {url}") | |
return api | |
def perform_request(request: Callable, *args: Any, **kwargs: Any) -> Response: | |
""" | |
Perform a request to the Creatio API. | |
Args: | |
request (Callable): The request method to perform. | |
*args (Any): Positional arguments for the request method. | |
**kwargs (Any): Keyword arguments for the request method. | |
Returns: | |
Response: The response from the API. | |
""" | |
try: | |
return request(*args, **kwargs) | |
except Exception as e: | |
print_exception(e, "Unable to perform the request") | |
def create_case(api: CreatioODataAPI) -> Response: | |
""" | |
Modify a case in Creatio. | |
Args: | |
api (CreatioODataAPI): The API object. | |
""" | |
data: dict[str, str] = { | |
"YuCentroCliente": "510c3a39-83c1-47d7-a7ff-faf1c820d336", | |
"YuNombreCliente": "John Doe", | |
"YuTipoPosicion": "42dd85b3-9479-49dc-97e4-14298f61759b", | |
"YuFechaInicioFormacion": "2021-09-01T00:00:00Z", | |
"YuFechaFinFormacion": "2021-09-30T00:00:00Z", | |
"YuTipoTurno": "4651114a-5824-4441-9d03-2251c16d3189", | |
"YuDepartamentoDestino": "9141df91-d3c6-42b1-a5b8-8f11d6b23ccd", | |
"YuTipologiaC1": "070a9fd3-3ac0-4b3a-83d6-b1495f9e75e7", | |
"YuTipologiaC2": "4819136f-5bf9-48a3-b1f7-0c09f3d7909f", | |
"YuTipodepeticion": "03a3059f-5060-4922-b358-977b5b4e7341", | |
} | |
return perform_request(api.add_collection_data, collection="Case", data=data) | |
def modify_case(api: CreatioODataAPI, case_id: str) -> Response: | |
""" | |
Modify a case in Creatio. | |
Args: | |
api (CreatioODataAPI): The API object. | |
case_id (str): The ID of the case to modify. | |
""" | |
data: dict[str, str] = { | |
"YuCentroCliente": "510c3a39-83c1-47d7-a7ff-faf1c820d336", | |
"YuNombreCliente": "John Doe", | |
"YuTipoPosicion": "42dd85b3-9479-49dc-97e4-14298f61759b", | |
"YuFechaInicioFormacion": "2021-09-01T00:00:00Z", | |
"YuFechaFinFormacion": "2021-09-30T00:00:00Z", | |
"YuTipoTurno": "4651114a-5824-4441-9d03-2251c16d3189", | |
"YuDepartamentoDestino": "9141df91-d3c6-42b1-a5b8-8f11d6b23ccd", | |
"YuTipologiaC1": "070a9fd3-3ac0-4b3a-83d6-b1495f9e75e7", | |
"YuTipologiaC2": "4819136f-5bf9-48a3-b1f7-0c09f3d7909f", | |
"YuTipodepeticion": "03a3059f-5060-4922-b358-977b5b4e7341", | |
} | |
return perform_request( | |
api.modify_collection_data, collection="Case", record_id=case_id, data=data | |
) | |
def get_case(api: CreatioODataAPI, case_id: str = "") -> Response: | |
""" | |
Get a case from Creatio. | |
Args: | |
api (CreatioODataAPI): The API object. | |
case_id (str): The ID of the case to get. | |
""" | |
params: dict[str, str] = { | |
"$select": "Id,Number,CreatedOn", | |
"$top": "10", | |
} | |
return perform_request( | |
api.get_collection_data, "Case", record_id=case_id, params=params | |
) | |
def main() -> None: | |
api: CreatioODataAPI = login( | |
url="...", | |
username="...", | |
password="...", | |
) | |
response: Response = get_case(api) | |
print(response.json()) | |
lenght = len(response.json()["value"]) | |
print(f"Number of cases: {lenght}") | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment