diff --git a/acctinfo.go b/acctinfo.go index d346e67..9863389 100644 --- a/acctinfo.go +++ b/acctinfo.go @@ -4,18 +4,18 @@ import ( "github.com/golang/go/src/encoding/xml" ) -type OfxAcctInfoRequest struct { +type AcctInfoRequest struct { XMLName xml.Name `xml:"ACCTINFOTRNRQ"` - TrnUID OfxUID `xml:"TRNUID"` - CltCookie OfxInt `xml:"CLTCOOKIE"` - DtAcctup OfxDate `xml:"ACCTINFORQ>DTACCTUP"` + TrnUID UID `xml:"TRNUID"` + CltCookie Int `xml:"CLTCOOKIE"` + DtAcctup Date `xml:"ACCTINFORQ>DTACCTUP"` } -func (r *OfxAcctInfoRequest) Name() string { +func (r *AcctInfoRequest) Name() string { return "ACCTINFOTRNRQ" } -func (r *OfxAcctInfoRequest) Valid() (bool, error) { +func (r *AcctInfoRequest) Valid() (bool, error) { if ok, err := r.TrnUID.Valid(); !ok { return false, err } diff --git a/ofx.go b/ofx.go index cb4fe79..bfcc5fd 100644 --- a/ofx.go +++ b/ofx.go @@ -12,16 +12,16 @@ import ( "time" ) -type OfxMessage interface { +type Message interface { Name() string Valid() (bool, error) } -type OfxRequest struct { +type Request struct { URL string - Version string // String for OFX header, defaults to 203 - Signon OfxSignonRequest // - Signup []OfxMessage // + Version string // String for OFX header, defaults to 203 + Signon SignonRequest // + Signup []Message // // // // @@ -33,11 +33,11 @@ type OfxRequest struct { // // // - Profile []OfxMessage // + Profile []Message // // } -func (oq *OfxRequest) marshalMessageSet(e *xml.Encoder, requests []OfxMessage, setname string) error { +func (oq *Request) marshalMessageSet(e *xml.Encoder, requests []Message, setname string) error { if len(requests) > 0 { messageSetElement := xml.StartElement{Name: xml.Name{Local: setname}} if err := e.EncodeToken(messageSetElement); err != nil { @@ -60,7 +60,7 @@ func (oq *OfxRequest) marshalMessageSet(e *xml.Encoder, requests []OfxMessage, s return nil } -func (oq *OfxRequest) Marshal() (*bytes.Buffer, error) { +func (oq *Request) Marshal() (*bytes.Buffer, error) { var b bytes.Buffer if len(oq.Version) == 0 { @@ -127,8 +127,8 @@ NEWFILEUID:NONE return &b, nil } -func (oq *OfxRequest) Request() (*OfxResponse, error) { - oq.Signon.Dtclient = OfxDate(time.Now()) +func (oq *Request) Request() (*Response, error) { + oq.Signon.Dtclient = Date(time.Now()) b, err := oq.Marshal() if err != nil { @@ -152,7 +152,7 @@ func (oq *OfxRequest) Request() (*OfxResponse, error) { xmlVersion = false } - var ofxresp OfxResponse + var ofxresp Response if err := ofxresp.Unmarshal(response.Body, xmlVersion); err != nil { return nil, err } @@ -160,10 +160,10 @@ func (oq *OfxRequest) Request() (*OfxResponse, error) { return &ofxresp, nil } -type OfxResponse struct { - Version string // String for OFX header, defaults to 203 - Signon OfxSignonResponse // - Signup []OfxMessage // +type Response struct { + Version string // String for OFX header, defaults to 203 + Signon SignonResponse // + Signup []Message // // // // @@ -175,11 +175,11 @@ type OfxResponse struct { // // // - Profile []OfxMessage // + Profile []Message // // } -func (or *OfxResponse) readSGMLHeaders(r *bufio.Reader) error { +func (or *Response) readSGMLHeaders(r *bufio.Reader) error { var seenHeader, seenVersion bool = false, false for { line, err := r.ReadString('\n') @@ -240,7 +240,7 @@ func (or *OfxResponse) readSGMLHeaders(r *bufio.Reader) error { return nil } -func (or *OfxResponse) readXMLHeaders(decoder *xml.Decoder) error { +func (or *Response) readXMLHeaders(decoder *xml.Decoder) error { tok, err := decoder.Token() if err != nil { return err @@ -308,7 +308,7 @@ func (or *OfxResponse) readXMLHeaders(decoder *xml.Decoder) error { return nil } -func (or *OfxResponse) Unmarshal(reader io.Reader, xmlVersion bool) error { +func (or *Response) Unmarshal(reader io.Reader, xmlVersion bool) error { r := bufio.NewReader(reader) // parse SGML headers before creating XML decoder diff --git a/profile.go b/profile.go index f4e103b..4a05534 100644 --- a/profile.go +++ b/profile.go @@ -4,18 +4,18 @@ import ( "github.com/golang/go/src/encoding/xml" ) -type OfxProfileRequest struct { - XMLName xml.Name `xml:"PROFTRNRQ"` - TrnUID OfxUID `xml:"TRNUID"` - ClientRouting OfxString `xml:"PROFRQ>CLIENTROUTING"` // Forced to NONE - DtProfup OfxDate `xml:"PROFRQ>DTPROFUP"` +type ProfileRequest struct { + XMLName xml.Name `xml:"PROFTRNRQ"` + TrnUID UID `xml:"TRNUID"` + ClientRouting String `xml:"PROFRQ>CLIENTROUTING"` // Forced to NONE + DtProfup Date `xml:"PROFRQ>DTPROFUP"` } -func (r *OfxProfileRequest) Name() string { +func (r *ProfileRequest) Name() string { return "PROFTRNRQ" } -func (r *OfxProfileRequest) Valid() (bool, error) { +func (r *ProfileRequest) Valid() (bool, error) { if ok, err := r.TrnUID.Valid(); !ok { return false, err } diff --git a/signon.go b/signon.go index d7abd35..a28c108 100644 --- a/signon.go +++ b/signon.go @@ -5,25 +5,25 @@ import ( "github.com/golang/go/src/encoding/xml" ) -type OfxSignonRequest struct { - XMLName xml.Name `xml:"SONRQ"` - Dtclient OfxDate `xml:"DTCLIENT"` // Overridden in OfxRequest.Request() - UserId OfxString `xml:"USERID"` - UserPass OfxString `xml:"USERPASS,omitempty"` - UserKey OfxString `xml:"USERKEY,omitempty"` - Language OfxString `xml:"LANGUAGE"` // Defaults to ENG - Org OfxString `xml:"FI>ORG"` - Fid OfxString `xml:"FI>FID"` - AppId OfxString `xml:"APPID"` // Defaults to OFXGO - AppVer OfxString `xml:"APPVER"` // Defaults to 0001 - ClientUID OfxUID `xml:"CLIENTUID,omitempty"` +type SignonRequest struct { + XMLName xml.Name `xml:"SONRQ"` + Dtclient Date `xml:"DTCLIENT"` // Overridden in Request.Request() + UserId String `xml:"USERID"` + UserPass String `xml:"USERPASS,omitempty"` + UserKey String `xml:"USERKEY,omitempty"` + Language String `xml:"LANGUAGE"` // Defaults to ENG + Org String `xml:"FI>ORG"` + Fid String `xml:"FI>FID"` + AppId String `xml:"APPID"` // Defaults to OFXGO + AppVer String `xml:"APPVER"` // Defaults to 0001 + ClientUID UID `xml:"CLIENTUID,omitempty"` } -func (r *OfxSignonRequest) Name() string { +func (r *SignonRequest) Name() string { return "SONRQ" } -func (r *OfxSignonRequest) Valid() (bool, error) { +func (r *SignonRequest) Valid() (bool, error) { if len(r.UserId) < 1 || len(r.UserId) > 32 { return false, errors.New("SONRQ>USERID invalid length") } @@ -59,14 +59,14 @@ func (r *OfxSignonRequest) Valid() (bool, error) { return true, nil } -type OfxStatus struct { - XMLName xml.Name `xml:"STATUS"` - Code OfxInt `xml:"CODE"` - Severity OfxString `xml:"SEVERITY"` - Message OfxString `xml:"MESSAGE,omitempty"` +type Status struct { + XMLName xml.Name `xml:"STATUS"` + Code Int `xml:"CODE"` + Severity String `xml:"SEVERITY"` + Message String `xml:"MESSAGE,omitempty"` } -func (s *OfxStatus) Valid() (bool, error) { +func (s *Status) Valid() (bool, error) { switch s.Severity { case "INFO", "WARN", "ERROR": return true, nil @@ -75,26 +75,26 @@ func (s *OfxStatus) Valid() (bool, error) { } } -type OfxSignonResponse struct { - XMLName xml.Name `xml:"SONRS"` - Status OfxStatus `xml:"STATUS"` - Dtserver OfxDate `xml:"DTSERVER"` - UserKey OfxString `xml:"USERKEY,omitempty"` - TsKeyExpire OfxDate `xml:"TSKEYEXPIRE,omitempty"` - Language OfxString `xml:"LANGUAGE"` - Dtprofup OfxDate `xml:"DTPROFUP,omitempty"` - Dtacctup OfxDate `xml:"DTACCTUP,omitempty"` - Org OfxString `xml:"FI>ORG"` - Fid OfxString `xml:"FI>FID"` - SessCookie OfxString `xml:"SESSCOOKIE,omitempty"` - AccessKey OfxString `xml:"ACCESSKEY,omitempty"` +type SignonResponse struct { + XMLName xml.Name `xml:"SONRS"` + Status Status `xml:"STATUS"` + Dtserver Date `xml:"DTSERVER"` + UserKey String `xml:"USERKEY,omitempty"` + TsKeyExpire Date `xml:"TSKEYEXPIRE,omitempty"` + Language String `xml:"LANGUAGE"` + Dtprofup Date `xml:"DTPROFUP,omitempty"` + Dtacctup Date `xml:"DTACCTUP,omitempty"` + Org String `xml:"FI>ORG"` + Fid String `xml:"FI>FID"` + SessCookie String `xml:"SESSCOOKIE,omitempty"` + AccessKey String `xml:"ACCESSKEY,omitempty"` } -func (r *OfxSignonResponse) Name() string { +func (r *SignonResponse) Name() string { return "SONRS" } -func (r *OfxSignonResponse) Valid() (bool, error) { +func (r *SignonResponse) Valid() (bool, error) { if len(r.Language) != 3 { return false, errors.New("SONRS>LANGUAGE invalid length: " + string(r.Language)) } diff --git a/types.go b/types.go index 22052fc..4314798 100644 --- a/types.go +++ b/types.go @@ -11,9 +11,9 @@ import ( "time" ) -type OfxInt int64 +type Int int64 -func (oi *OfxInt) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { +func (oi *Int) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { var value string err := d.DecodeElement(&value, &start) if err != nil { @@ -23,7 +23,7 @@ func (oi *OfxInt) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { if err != nil { return err } - *oi = (OfxInt)(i) + *oi = (Int)(i) return nil } @@ -37,9 +37,9 @@ var ofxDateFormats = []string{ var ofxDateZoneFormat = "20060102150405.000 -0700" var ofxDateZoneRegex = regexp.MustCompile(`^\[([+-]?[0-9]+)(\.([0-9]{2}))?(:([A-Z]+))?\]$`) -type OfxDate time.Time +type Date time.Time -func (od *OfxDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { +func (od *Date) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { var value string err := d.DecodeElement(&value, &start) if err != nil { @@ -68,7 +68,7 @@ func (od *OfxDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { value = value[:len(ofxDateFormats[0])] + " " + fmt.Sprintf("%+d%02d", zonehours, zoneminutes) t, err := time.Parse(ofxDateZoneFormat, value) if err == nil { - tmpod := OfxDate(t) + tmpod := Date(t) *od = tmpod return nil } @@ -77,7 +77,7 @@ func (od *OfxDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { for _, format := range ofxDateFormats { t, err := time.Parse(format, value) if err == nil { - tmpod := OfxDate(t) + tmpod := Date(t) *od = tmpod return nil } @@ -85,7 +85,7 @@ func (od *OfxDate) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { return errors.New("OFX: Couldn't parse date:" + value) } -func (od *OfxDate) String() string { +func (od *Date) String() string { t := time.Time(*od) format := t.Format(ofxDateFormats[0]) zonename, zoneoffset := t.Zone() @@ -99,25 +99,25 @@ func (od *OfxDate) String() string { return format + ":" + zonename + "]" } -func (od *OfxDate) MarshalXML(e *xml.Encoder, start xml.StartElement) error { +func (od *Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error { return e.EncodeElement(od.String(), start) } -type OfxString string +type String string -func (os *OfxString) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { +func (os *String) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { var value string err := d.DecodeElement(&value, &start) if err != nil { return err } - *os = OfxString(strings.TrimSpace(value)) + *os = String(strings.TrimSpace(value)) return nil } -type OfxBoolean bool +type Boolean bool -func (ob *OfxBoolean) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { +func (ob *Boolean) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { var value string err := d.DecodeElement(&value, &start) if err != nil { @@ -126,32 +126,32 @@ func (ob *OfxBoolean) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error tmpob := strings.TrimSpace(value) switch tmpob { case "Y": - *ob = OfxBoolean(true) + *ob = Boolean(true) case "N": - *ob = OfxBoolean(false) + *ob = Boolean(false) default: return errors.New("Invalid OFX Boolean") } return nil } -func (ob *OfxBoolean) MarshalXML(e *xml.Encoder, start xml.StartElement) error { +func (ob *Boolean) MarshalXML(e *xml.Encoder, start xml.StartElement) error { if *ob { return e.EncodeElement("Y", start) } return e.EncodeElement("N", start) } -type OfxUID string +type UID string -func (ou *OfxUID) Valid() (bool, error) { +func (ou *UID) Valid() (bool, error) { if len(*ou) != 36 { return false, errors.New("UID not 36 characters long") } return true, nil } -func RandomUID() (*OfxUID, error) { +func RandomUID() (*UID, error) { uidbytes := make([]byte, 16) n, err := rand.Read(uidbytes[:]) if err != nil { @@ -160,6 +160,6 @@ func RandomUID() (*OfxUID, error) { if n != 16 { return nil, errors.New("RandomUID failed to read 16 random bytes") } - uid := OfxUID(fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uidbytes[:4], uidbytes[4:6], uidbytes[6:8], uidbytes[8:10], uidbytes[10:])) + uid := UID(fmt.Sprintf("%08x-%04x-%04x-%04x-%012x", uidbytes[:4], uidbytes[4:6], uidbytes[6:8], uidbytes[8:10], uidbytes[10:])) return &uid, nil }