Example #1
0
File: yfws.go Project: IMQS/yfws
func parseResponse(m *mxj.Map, response *[]mxj.Map, responsename, idmapname string) error {
	path := fmt.Sprintf("Envelope.Body.%sResponse.%sReturn.-href", responsename, responsename)
	mainid, err := m.ValueForPathString(path)
	if err != nil {
		return err
	}
	value, err := m.ValuesForKey("multiRef", fmt.Sprintf("-id:%s", mainid[1:]))
	if err != nil {
		return err
	}
	if len(value) == 0 {
		return errors.New("Main reponse not found")
	}

	valuemap := mxj.Map(value[0].(map[string]interface{}))

	statuscode, err := valuemap.ValueForPathString("statusCode.#text")
	if err != nil {
		return err
	}

	if statuscode != "SUCCESS" {
		errcode, _ := valuemap.ValueForPathString("errorCode.#text")
		return YFErrors[errcode]
	}

	if idmapname != "" {
		ids, err := valuemap.ValuesForPath(idmapname + "." + idmapname)
		if err != nil {
			return fmt.Errorf("Could not find %s.%s %v", idmapname, idmapname, err)
		}
		for _, id := range ids {
			idmap := mxj.Map(id.(map[string]interface{}))
			idvalue, err := idmap.ValueForPathString("-href")
			if err != nil {
				return err
			}

			multiref, err := m.ValuesForKey("multiRef", "-id:"+idvalue[1:])
			if err != nil {
				return fmt.Errorf("Could not find multiref for id %s, %v", idvalue[1:], err)
			}
			if len(multiref) == 0 {
				return fmt.Errorf("mutilRef element not found for id : %s", idvalue[1:])
			}
			*response = append(*response, mxj.Map(multiref[0].(map[string]interface{})))
		}
	} else {
		// Add the original back, this is to get responses such as sessionid etc.
		*response = append(*response, valuemap)
	}
	return nil

}