Skip to content

Commit

Permalink
move support id msg logic to stream class
Browse files Browse the repository at this point in the history
  • Loading branch information
pnadolny13 committed Nov 8, 2024
1 parent 555e7b6 commit 4e881c7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 50 deletions.
49 changes: 0 additions & 49 deletions tap_intacct/sage.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import datetime as dt
import json
import logging
import re
import uuid
from http.client import RemoteDisconnected
from typing import Union
from urllib.parse import unquote

import backoff
import requests
Expand Down Expand Up @@ -278,53 +276,6 @@ def _post_request(self, dict_body: dict, api_url: str) -> dict:

raise SageIntacctSDKError("Error: {0}".format(parsed_response))

def support_id_msg(self, errormessages) -> Union[list, dict]:
"""
Finds whether the error messages is list / dict and assign type and error assignment.
Parameters:
errormessages (dict / list): error message received from Sage Intacct.
Returns:
Error message assignment and type.
"""
error = {}
if isinstance(errormessages["error"], list):
error["error"] = errormessages["error"][0]
error["type"] = "list"
elif isinstance(errormessages["error"], dict):
error["error"] = errormessages["error"]
error["type"] = "dict"

return error

def decode_support_id(self, errormessages: Union[list, dict]) -> Union[list, dict]:
"""
Decodes Support ID.
Parameters:
errormessages (dict / list): error message received from Sage Intacct.
Returns:
Same error message with decoded Support ID.
"""
support_id_msg = self.support_id_msg(errormessages)
data_type = support_id_msg["type"]
error = support_id_msg["error"]
if error and error["description2"]:
message = error["description2"]
support_id = re.search("Support ID: (.*)]", message)
if support_id and support_id.group(1):
decoded_support_id = unquote(support_id.group(1))
message = message.replace(support_id.group(1), decoded_support_id)

if data_type == "list":
errormessages["error"][0]["description2"] = message if message else None
elif data_type == "dict":
errormessages["error"]["description2"] = message if message else None

return errormessages

def format_and_send_request(self, data: dict) -> Union[list, dict]:
"""
Format data accordingly to convert them to xml.
Expand Down
33 changes: 32 additions & 1 deletion tap_intacct/streams.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from __future__ import annotations

import json
import re
import typing as t
import uuid
from datetime import datetime, timezone
from urllib.parse import unquote

import xmltodict
from singer_sdk import typing as th # JSON schema typing helpers
Expand Down Expand Up @@ -224,6 +226,35 @@ def prepare_request_payload(
}
return xmltodict.unparse(dict_body)

def _support_id_msg(self, errormessages: list | dict) -> list | dict:
error = {}
if isinstance(errormessages["error"], list):
error["error"] = errormessages["error"][0]
error["type"] = "list"
elif isinstance(errormessages["error"], dict):
error["error"] = errormessages["error"]
error["type"] = "dict"

return error

def _decode_support_id(self, errormessages: list | dict) -> list | dict:
support_id_msg = self._support_id_msg(errormessages)
data_type = support_id_msg["type"]
error = support_id_msg["error"]
if error and error["description2"]:
message = error["description2"]
support_id = re.search("Support ID: (.*)]", message)
if support_id and support_id.group(1):
decoded_support_id = unquote(support_id.group(1))
message = message.replace(support_id.group(1), decoded_support_id)

if data_type == "list":
errormessages["error"][0]["description2"] = message if message else None
elif data_type == "dict":
errormessages["error"]["description2"] = message if message else None

return errormessages

def parse_response(self, response: requests.Response) -> t.Iterable[dict]:
"""Parse the response and return an iterator of result records.
Expand Down Expand Up @@ -258,7 +289,7 @@ def parse_response(self, response: requests.Response) -> t.Iterable[dict]:
api_response = parsed_response["response"]["operation"]

if parsed_response["response"]["control"]["status"] == "failure":
exception_msg = self.sage_client.decode_support_id(
exception_msg = self._decode_support_id(
parsed_response["response"]["errormessage"]
)
raise WrongParamsError(
Expand Down

0 comments on commit 4e881c7

Please sign in to comment.