1
0
mirror of https://github.com/aclindsa/ofxgo.git synced 2024-10-30 05:20:05 -04:00

Pull out writing the OFX header to its own function

This is in preparation for also allowing responses to be written via the
library (as opposed to only read).
This commit is contained in:
Aaron Lindsay 2019-03-01 05:51:36 -05:00
parent 9dd9c3bd3f
commit 286e619071
2 changed files with 29 additions and 22 deletions

View File

@ -3,10 +3,36 @@ package ofxgo
//go:generate ./generate_constants.py
import (
"bytes"
"errors"
"fmt"
"github.com/aclindsa/xml"
)
func writeHeader(b *bytes.Buffer, v ofxVersion) error {
// Write the header appropriate to our version
switch v {
case OfxVersion102, OfxVersion103, OfxVersion151, OfxVersion160:
b.WriteString(`OFXHEADER:100
DATA:OFXSGML
VERSION:` + v.String() + `
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
`)
case OfxVersion200, OfxVersion201, OfxVersion202, OfxVersion203, OfxVersion210, OfxVersion211, OfxVersion220:
b.WriteString(`<?xml version="1.0" encoding="UTF-8" standalone="no"?>` + "\n")
b.WriteString(`<?OFX OFXHEADER="200" VERSION="` + v.String() + `" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE"?>` + "\n")
default:
return fmt.Errorf("%d is not a valid OFX version string", v)
}
return nil
}
// Message represents an OFX message in a message set. it is used to ease
// marshalling and unmarshalling.
type Message interface {

View File

@ -3,7 +3,6 @@ package ofxgo
import (
"bytes"
"errors"
"fmt"
"github.com/aclindsa/xml"
"time"
)
@ -35,7 +34,7 @@ type Request struct {
indent bool // Whether to indent the marshaled XML
}
func marshalMessageSet(e *xml.Encoder, requests []Message, set messageType, version ofxVersion) error {
func encodeMessageSet(e *xml.Encoder, requests []Message, set messageType, version ofxVersion) error {
if len(requests) > 0 {
messageSetElement := xml.StartElement{Name: xml.Name{Local: set.String()}}
if err := e.EncodeToken(messageSetElement); err != nil {
@ -80,25 +79,7 @@ func (oq *Request) Marshal() (*bytes.Buffer, error) {
var b bytes.Buffer
// Write the header appropriate to our version
switch oq.Version {
case OfxVersion102, OfxVersion103, OfxVersion151, OfxVersion160:
b.WriteString(`OFXHEADER:100
DATA:OFXSGML
VERSION:` + oq.Version.String() + `
SECURITY:NONE
ENCODING:USASCII
CHARSET:1252
COMPRESSION:NONE
OLDFILEUID:NONE
NEWFILEUID:NONE
`)
case OfxVersion200, OfxVersion201, OfxVersion202, OfxVersion203, OfxVersion210, OfxVersion211, OfxVersion220:
b.WriteString(`<?xml version="1.0" encoding="UTF-8" standalone="no"?>` + "\n")
b.WriteString(`<?OFX OFXHEADER="200" VERSION="` + oq.Version.String() + `" SECURITY="NONE" OLDFILEUID="NONE" NEWFILEUID="NONE"?>` + "\n")
default:
return nil, fmt.Errorf("%d is not a valid OFX version string", oq.Version)
}
writeHeader(&b, oq.Version)
encoder := xml.NewEncoder(&b)
if oq.indent {
@ -145,7 +126,7 @@ NEWFILEUID:NONE
{oq.Image, ImageRq},
}
for _, set := range messageSets {
if err := marshalMessageSet(encoder, set.Messages, set.Type, oq.Version); err != nil {
if err := encodeMessageSet(encoder, set.Messages, set.Type, oq.Version); err != nil {
return nil, err
}
}