def _send_request(self, url, request_body): """Transmits the message to the server and checks the response for error status.""" request = urllib.request.Request(url, request_body.encode('utf-8'), { "Content-type": "application/x-ofx", "Accept": "*/*, application/x-ofx" }) stream = urllib.request.urlopen(request) response = stream.read() stream.close() response = Response(response) response.check_signon_status() parsed_ofx = response.as_dict() # FIXME: This needs to account for statement closing responses. if "BANKMSGSRSV1" in parsed_ofx: bank_status = \ parsed_ofx["BANKMSGSRSV1"]["STMTTRNRS"]["STATUS"] self._check_status(bank_status, "bank statement") elif "CREDITCARDMSGSRSV1" in parsed_ofx: creditcard_status = \ parsed_ofx["CREDITCARDMSGSRSV1"]["CCSTMTTRNRS"]["STATUS"] self._check_status(creditcard_status, "credit card statement") elif "SIGNUPMSGSRSV1" in parsed_ofx: acctinfo_status = \ parsed_ofx["SIGNUPMSGSRSV1"]["ACCTINFOTRNRS"]["STATUS"] self._check_status(acctinfo_status, "account information") return response
def to_xml(self): ofx102 = self.to_ofx102() if self.debug: sys.stderr.write(ofx102 + "\n") sys.stderr.write("Parsing OFX/1.02.\n") response = Response(ofx102, debug=self.debug) if self.debug: sys.stderr.write("Making OFX/2.0.\n") xml = response.as_xml(original_format="OFC") return xml
def to_xml(self): ofx102 = self.to_ofx102() if self.debug: sys.stderr.write(ofx102 + "\n") sys.stderr.write("Parsing OFX/1.02.\n") response = Response(ofx102) #, debug=self.debug) if self.debug: sys.stderr.write("Making OFX/2.0.\n") if self.dayfirst: date_format = "DD/MM/YY" else: date_format = "MM/DD/YY" xml = response.as_xml(original_format="QIF", date_format=date_format) return xml
def setUp(self): self.response_text = get_checking_stmt().decode('utf-8') self.response = Response(get_checking_stmt())
def convert(filecontent, filetype, verbose=False, fid="UNKNOWN", org="UNKNOWN", bankid="UNKNOWN", accttype="UNKNOWN", acctid="UNKNOWN", balance="UNKNOWN", curdef=None, lang="ENG", dayfirst=False, debug=False): text = os.linesep.join(s for s in filecontent.splitlines() if s) # This finishes a verbosity message started by the caller, where the # caller explains the source command-line option and this explains the # source format. if verbose: sys.stderr.write("Converting from %s format.\n" % filetype) if options.debug and (filetype in ["OFC", "QIF"] or filetype.startswith("OFX")): sys.stderr.write("Starting work on raw text:\n") sys.stderr.write(rawtext + "\n\n") if filetype.startswith("OFX/2"): if verbose: sys.stderr.write("No conversion needed; returning unmodified.\n") # The file is already OFX 2 -- return it unaltered, ignoring # any of the parameters passed to this method. return text elif filetype.startswith("OFX"): if verbose: sys.stderr.write("Converting to OFX/2.0...\n") # This will throw a ParseException if it is unable to recognize # the source format. response = Response(text, debug=debug) return response.as_xml(original_format=filetype) elif filetype == "OFC": if verbose: sys.stderr.write("Beginning OFC conversion...\n") converter = OfcConverter(text, fid=fid, org=org, curdef=curdef, lang=lang, debug=debug) # This will throw a ParseException if it is unable to recognize # the source format. if verbose: sys.stderr.write("Converting to OFX/1.02...\n\n%s\n\n" % converter.to_ofx102()) sys.stderr.write("Converting to OFX/2.0...\n") return converter.to_xml() elif filetype == "QIF": if verbose: sys.stderr.write("Beginning QIF conversion...\n") converter = QifConverter(text, fid=fid, org=org, bankid=bankid, accttype=accttype, acctid=acctid, balance=balance, curdef=curdef, lang=lang, dayfirst=dayfirst, debug=debug) # This will throw a ParseException if it is unable to recognize # the source format. if verbose: sys.stderr.write("Converting to OFX/1.02...\n\n%s\n\n" % converter.to_ofx102()) sys.stderr.write("Converting to OFX/2.0...\n") return converter.to_xml() else: raise TypeError("Unable to convert source format '%s'." % filetype)
def test_statement_as_xml(self): response = Response(self.checking) self.assertEqual('<?xml version="1.0"', response.as_xml()[:19])