Migrate getKey to be apart of Envelope

We do not need to specify e.doc repeatedly.
This commit is contained in:
Spotlight 2022-01-09 05:29:55 -06:00
parent 97ce2212c6
commit ca35a37589
No known key found for this signature in database
GPG Key ID: 874AA355B3209BDC
4 changed files with 22 additions and 16 deletions

2
cas.go
View File

@ -1,7 +1,7 @@
package main package main
func listItems(e *Envelope) { func listItems(e *Envelope) {
titleId, err := getKey(e.doc, "TitleId") titleId, err := e.getKey("TitleId")
if err != nil { if err != nil {
e.Error(9, "Unable to obtain title.", err) e.Error(9, "Unable to obtain title.", err)
} }

8
ias.go
View File

@ -47,7 +47,7 @@ const (
) )
func checkRegistration(e *Envelope) { func checkRegistration(e *Envelope) {
serialNo, err := getKey(e.doc, "SerialNumber") serialNo, err := e.getKey("SerialNumber")
if err != nil { if err != nil {
e.Error(5, "missing serial number", err) e.Error(5, "missing serial number", err)
return return
@ -111,13 +111,13 @@ func syncRegistration(e *Envelope) {
} }
func register(e *Envelope) { func register(e *Envelope) {
deviceCode, err := getKey(e.doc, "DeviceCode") deviceCode, err := e.getKey("DeviceCode")
if err != nil { if err != nil {
e.Error(7, "missing device code", err) e.Error(7, "missing device code", err)
return return
} }
registerRegion, err := getKey(e.doc, "RegisterRegion") registerRegion, err := e.getKey("RegisterRegion")
if err != nil { if err != nil {
e.Error(7, "missing registration region", err) e.Error(7, "missing registration region", err)
return return
@ -127,7 +127,7 @@ func register(e *Envelope) {
return return
} }
serialNo, err := getKey(e.doc, "SerialNumber") serialNo, err := e.getKey("SerialNumber")
if err != nil { if err != nil {
e.Error(7, "missing serial number", err) e.Error(7, "missing serial number", err)
return return

View File

@ -156,7 +156,7 @@ func checkAuthentication(e *Envelope) (bool, error) {
} }
// Get necessary authentication identifiers. // Get necessary authentication identifiers.
deviceToken, err := getKey(e.doc, "DeviceToken") deviceToken, err := e.getKey("DeviceToken")
if err != nil { if err != nil {
return false, err return false, err
} }

View File

@ -113,7 +113,7 @@ func (e *Envelope) Language() string {
// If for whatever reason AccountId is not present (such as in SyncRegistration), // If for whatever reason AccountId is not present (such as in SyncRegistration),
// it will return an error. Please design in a way so that this is not an issue. // it will return an error. Please design in a way so that this is not an issue.
func (e *Envelope) AccountId() (int64, error) { func (e *Envelope) AccountId() (int64, error) {
accountId, err := getKey(e.doc, "AccountId") accountId, err := e.getKey("AccountId")
if err != nil { if err != nil {
return 0, nil return 0, nil
} }
@ -124,14 +124,13 @@ func (e *Envelope) AccountId() (int64, error) {
// ObtainCommon interprets a given node, and updates the envelope with common key values. // ObtainCommon interprets a given node, and updates the envelope with common key values.
func (e *Envelope) ObtainCommon() error { func (e *Envelope) ObtainCommon() error {
var err error var err error
doc := e.doc
// These fields are common across all requests. // These fields are common across all requests.
e.Body.Response.Version, err = getKey(doc, "Version") e.Body.Response.Version, err = e.getKey("Version")
if err != nil { if err != nil {
return err return err
} }
deviceIdString, err := getKey(doc, "DeviceId") deviceIdString, err := e.getKey("DeviceId")
if err != nil { if err != nil {
return err return err
} }
@ -139,21 +138,21 @@ func (e *Envelope) ObtainCommon() error {
if err != nil { if err != nil {
return err return err
} }
e.Body.Response.MessageId, err = getKey(doc, "MessageId") e.Body.Response.MessageId, err = e.getKey("MessageId")
if err != nil { if err != nil {
return err return err
} }
// These are as well, but we do not need to send them back in our response. // These are as well, but we do not need to send them back in our response.
e.region, err = getKey(doc, "Region") e.region, err = e.getKey("Region")
if err != nil { if err != nil {
return err return err
} }
e.country, err = getKey(doc, "Country") e.country, err = e.getKey("Country")
if err != nil { if err != nil {
return err return err
} }
e.language, err = getKey(doc, "Language") e.language, err = e.getKey("Language")
if err != nil { if err != nil {
return err return err
} }
@ -237,8 +236,8 @@ func stripNamespace(node *xmlquery.Node) {
} }
// getKey returns the value for a child key from a node, if documented. // getKey returns the value for a child key from a node, if documented.
func getKey(doc *xmlquery.Node, key string) (string, error) { func (e *Envelope) getKey(key string) (string, error) {
node := xmlquery.FindOne(doc, "//"+key) node := xmlquery.FindOne(e.doc, "//"+key)
if node == nil { if node == nil {
return "", errors.New("missing mandatory key named " + key) return "", errors.New("missing mandatory key named " + key)
@ -250,6 +249,7 @@ func getKey(doc *xmlquery.Node, key string) (string, error) {
// Derived from https://stackoverflow.com/a/31832326, adding numbers // Derived from https://stackoverflow.com/a/31832326, adding numbers
const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" const letterBytes = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
// RandString generates a random string with n length.
func RandString(n int) string { func RandString(n int) string {
b := make([]byte, n) b := make([]byte, n)
for i := range b { for i := range b {
@ -258,6 +258,7 @@ func RandString(n int) string {
return string(b) return string(b)
} }
// debugPrint logs a message only if this program is running in debug mode.
func debugPrint(v ...interface{}) { func debugPrint(v ...interface{}) {
if !isDebug { if !isDebug {
return return
@ -265,3 +266,8 @@ func debugPrint(v ...interface{}) {
log.Print(v...) log.Print(v...)
} }
// b64 returns a base64-encoded string of the given bytes.
func b64(b []byte) string {
return base64.StdEncoding.EncodeToString(b)
}