func()

in wstl1/mapping_engine/harmonization/harmonizecode/remote_code_harmonizer.go [47:110]


func (h *RemoteCodeHarmonizer) HarmonizeBySearch(sourceCode, sourceSystem, sourceValueset, targetValueset, version string) ([]HarmonizedCode, error) {
	key := CodeLookupKey{
		Code:    sourceCode,
		System:  sourceSystem,
		Version: version,
	}

	if sourceValueset == "" && targetValueset == "" {
		return nil, fmt.Errorf("source and target value sets cannot both be empty")
	}

	// Check cache before making http request.
	if v, ok := h.cache.Get(key); ok {
		return v.value, nil
	}

	u, err := url.Parse(h.address)
	if err != nil {
		return nil, fmt.Errorf("url is invalid %v", err)
	}
	u.Path = path.Join(u.Path, "fhir/ConceptMap/$translate")
	addr := u.String()

	req, err := http.NewRequest(http.MethodGet, addr, nil)
	if err != nil {
		return nil, fmt.Errorf("error building new request %v", err)
	}
	q := req.URL.Query()
	q.Add("code", sourceCode)
	q.Add("system", sourceSystem)
	if sourceValueset != "" {
		q.Add("source", sourceValueset)
	}
	if targetValueset != "" {
		q.Add("target", targetValueset)
	}
	if version != "" {
		q.Add("version", version)
	}

	req.URL.RawQuery = q.Encode()
	raw, err := h.client.ExecuteRequest(context.Background(), req, "translate code", true)
	if err != nil {
		return nil, fmt.Errorf("error calling remote endpoint to harmonize code, %v", err)
	}

	res, err := rawToCodes(raw)
	if err != nil {
		return nil, fmt.Errorf("error unmarshalling translate result %v", err)
	}

	if len(res) == 0 {
		res = append(res, HarmonizedCode{
			Code:    sourceCode,
			System:  "unharmonized",
			Version: version,
		})
	}

	// Add result to cache.
	h.cache.Put(key, res)

	return res, nil
}