Improve cas support

This commit is contained in:
Sketch 2022-10-18 13:33:15 -04:00
parent a943188958
commit 479edf4534
5 changed files with 67 additions and 8 deletions

24
cas.go
View File

@ -6,6 +6,26 @@ func listItems(e *Envelope) {
e.Error(9, "Unable to obtain title.", err)
}
attrs, err := e.getKeys("AttributeFilters")
if err != nil {
e.Error(5, "AttributeFilters key did not exist!", err)
}
var licenceStr string
for _, attr := range attrs {
name, value := parseNameValue(attr.InnerText())
if name == "TitleKind" {
licenceStr = value
}
}
// Now validate
licenceKind, err := GetLicenceKind(licenceStr)
if err != nil {
e.Error(5, "Invalid TitleKind was passed by SOAP", err)
}
// TODO(SketchMaster2001): Query database for items
e.AddKVNode("ListResultTotalSize", "1")
e.AddCustomType(Items{
TitleId: titleId,
@ -35,8 +55,8 @@ func listItems(e *Envelope) {
Amount: 0,
Currency: "POINTS",
},
Limits: LimitStruct(DR),
LicenseKind: RENTAL,
Limits: LimitStruct(PR),
LicenseKind: *licenceKind,
},
})
}

View File

@ -1,5 +1,7 @@
package main
import "errors"
// LimitKinds represents various limits applied to the current ticket.
type LimitKinds int
@ -25,6 +27,23 @@ const (
SERVICE LicenceKinds = "SERVICE"
)
func GetLicenceKind(kind string) (*LicenceKinds, error) {
names := map[string]LicenceKinds{
"PERMANENT": PERMANENT,
"DEMO": DEMO,
"TRIAL": TRIAL,
"RENTAL": RENTAL,
"SUBSCRIPT": SUBSCRIPT,
"SERVICE": SERVICE,
}
if value, exists := names[kind]; exists {
return &value, nil
} else {
return nil, errors.New("invalid LicenceKind")
}
}
// LimitStruct returns a Limits struct filled for the given kind.
func LimitStruct(kind LimitKinds) Limits {
names := map[LimitKinds]string{

10
ecs.go
View File

@ -169,18 +169,18 @@ func purchaseTitle(e *Envelope) {
func listPurchaseHistory(e *Envelope) {
e.AddCustomType([]Transactions{
{
TransactionId: "12345678",
TransactionId: "00000000",
Date: e.Timestamp(),
Type: "SERVICE",
TotalPaid: 7,
Type: string(SERVICE),
TotalPaid: 0,
Currency: "POINTS",
ItemId: 0,
TitleId: "000100014843494A",
ItemPricing: []Limits{
LimitStruct(DR),
},
ReferenceId: 1,
ReferenceValue: 19224,
ReferenceId: "01234567890123456789012345678912",
ReferenceValue: 1,
},
})

View File

@ -121,7 +121,7 @@ type Transactions struct {
ItemPricing []Limits `xml:"ItemPricing"`
TitleId string `xml:"TitleId,omitempty"`
ItemCode int `xml:"ItemCode,omitempty"`
ReferenceId int `xml:"ReferenceId,omitempty"`
ReferenceId string `xml:"ReferenceId,omitempty"`
ReferenceValue int `xml:"ReferenceValue,omitempty"`
}

View File

@ -209,6 +209,15 @@ func (e *Envelope) Error(errorCode int, reason string, err error) {
e.AddKVNode("ErrorMessage", fmt.Sprintf("%s: %v", reason, err))
}
// parseNameValue parses the output of *xmlquery.Node.InnerText when it is a nested Name and Value node.
func parseNameValue(s string) (string, string) {
s = strings.TrimSpace(s)
s = strings.Replace(s, " ", "", strings.Count(s, " ")-1)
decoded := strings.Split(s, " ")
return strings.TrimSuffix(decoded[0], "\n"), decoded[1]
}
// normalise parses a document, returning a document with only the request type's child nodes, stripped of prefix.
func normalise(service string, action string, reader io.Reader) (*xmlquery.Node, error) {
doc, err := xmlquery.Parse(reader)
@ -247,6 +256,17 @@ func (e *Envelope) getKey(key string) (string, error) {
}
}
// getKeys returns a list of xmlquery.Node, if documented.
func (e *Envelope) getKeys(key string) ([]*xmlquery.Node, error) {
node := xmlquery.Find(e.doc, "//"+key)
if node == nil {
return nil, errors.New("missing mandatory key named " + key)
} else {
return node, nil
}
}
// Derived from https://stackoverflow.com/a/31832326, adding numbers
const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"