/// <summary> /// Construct an StreamContent from a provided populated OFX object /// </summary> /// <param name="ofxObject">A populated OFX message with request or response data</param> /// <returns>Created StreamContent ready to send with HttpClient.Post</returns> public static StreamContent Create(Protocol.OFX ofxObject) { // Serialized data will be written to a MemoryStream var memoryStream = new System.IO.MemoryStream(); // XMLWriter will be used to encode the data using UTF8Encoding without a Byte Order Marker (BOM) var xmlWriter = new System.Xml.XmlTextWriter(memoryStream, new System.Text.UTF8Encoding(false)); // Write xml processing instruction xmlWriter.WriteStartDocument(); // Our OFX protocol uses a version 2.0.0 header with 2.1.1 protocol body xmlWriter.WriteProcessingInstruction("OFX", "OFXHEADER=\"200\" VERSION=\"211\" SECURITY=\"NONE\" OLDFILEUID=\"NONE\" NEWFILEUID=\"NONE\""); // Don't include namespaces in the root element XmlSerializerNamespaces ns = new XmlSerializerNamespaces(); ns.Add("", ""); // Generate XML to the stream m_serializer.Serialize(xmlWriter, ofxObject, ns); // Flush writer to stream xmlWriter.Flush(); // Position the stream back to the start for reading memoryStream.Position = 0; // Wrap in our HttpContent-derived class to provide headers and other HTTP encoding information return(new StreamContent(memoryStream)); }
/// <summary> /// Format and send a populated OFX request to the service. /// /// This is an asycronous call. /// </summary> /// <param name="request">Populated OFX request object</param> /// <returns>The returned task includes a populated OFX response object on successfull call</returns> public async Task <Protocol.OFX> sendRequestAsync(Protocol.OFX ofxRequest) { // Create a HttpContent object wrapping the OFX request object var request = StreamContent.Create(ofxRequest); // Send and await results return(await sendRequestAsync(request).ConfigureAwait(false)); }
/// <summary> /// Construct an StreamContent from a provided array of AbstractTopLevelMessageSet. /// Since all OFX messages are an OFX wrapper around an 1 or more AbstractTopLevelMessageSet entities, this /// convenience function saves the caller having to construct a wrapper OFX object on each call. /// /// Note that the AbstractTopLevelMessageSet order must obey the rules for the OFX protocol. See OFX specification v2.1.1 for details. /// </summary> /// <param name="messageSets">1 or more populated AbstractTopLevelMessageSet objects representing OFX requests or responses.</param> /// <returns>Created StreamContent ready to send with HttpClient.Post</returns> public static StreamContent Create(AbstractTopLevelMessageSet[] messageSets) { // Wrap message sets in OFX object Protocol.OFX ofx = new Protocol.OFX(); ofx.Items = messageSets; // Call through to OFX class-based factory method return(Create(ofx)); }
/// <summary> /// Construct an StreamContent from a provided array of AbstractTopLevelMessageSet. /// Since all OFX messages are an OFX wrapper around an 1 or more AbstractTopLevelMessageSet entities, this /// convenience function saves the caller having to construct a wrapper OFX object on each call. /// /// Note that the AbstractTopLevelMessageSet order must obey the rules for the OFX protocol. See OFX specification v2.1.1 for details. /// </summary> /// <param name="messageSets">1 or more populated AbstractTopLevelMessageSet objects representing OFX requests or responses.</param> /// <returns>Created StreamContent ready to send with HttpClient.Post</returns> public static StreamContent Create(AbstractTopLevelMessageSet[] messageSets) { // Wrap message sets in OFX object Protocol.OFX ofx = new Protocol.OFX(); ofx.Items = messageSets; // Call through to OFX class-based factory method return Create(ofx); }
/// <summary> /// Construct Statement objects for each statement responses included in the passed OFX response /// </summary> /// <param name="ofxResponse">OFX object populated with one or more statement responses</param> /// <returns></returns> public static IEnumerable <Statement> CreateFromOFXResponse(Protocol.OFX ofxResponse, out string errorMessage) { errorMessage = ""; List <Statement> statementList = new List <Statement>(); // Handle Credit Card responses foreach ( var responseMessageSet in ofxResponse.Items.Where(item => item.GetType() == typeof(CreditcardResponseMessageSetV1)) .Select(item => (CreditcardResponseMessageSetV1)item)) { foreach ( var transactionResponse in responseMessageSet.Items.Where( item => item.GetType() == typeof(CreditCardStatementTransactionResponse)) .Select(item => (CreditCardStatementTransactionResponse)item)) { statementList.Add(new Statement(transactionResponse.CCSTMTRS)); } } // Handle Bank responses foreach ( var responseMessageSet in ofxResponse.Items.Where(item => item.GetType() == typeof(BankResponseMessageSetV1)) .Select(item => (BankResponseMessageSetV1)item)) { foreach ( var transactionResponse in responseMessageSet.Items.Where( item => item.GetType() == typeof(StatementTransactionResponse)) .Select(item => (StatementTransactionResponse)item)) { if (transactionResponse.STMTRS != null) { statementList.Add(new Statement(transactionResponse.STMTRS)); } else { errorMessage = transactionResponse.STATUS.MESSAGE; } } } // Return populated list return(statementList); }