func CallCollectionsApiV2()

in controllers/util/solr_api/v2.go [41:98]


func CallCollectionsApiV2(ctx context.Context, cloud *solr.SolrCloud, urlMethod string, urlPath string, urlParams url.Values, body interface{}, response interface{}) (err error) {
	client := noVerifyTLSHttpClient
	if mTLSHttpClient != nil {
		client = mTLSHttpClient
	}

	cloudUrl := solr.InternalURLForCloud(cloud)
	if !strings.HasPrefix(urlPath, "/") {
		urlPath = "/" + urlPath
	}

	cloudUrl += urlPath
	if len(urlParams) > 0 {
		cloudUrl += "?" + urlParams.Encode()
	}

	resp := &http.Response{}

	var b *bytes.Buffer
	if body != nil {
		b = new(bytes.Buffer)
		if err = json.NewEncoder(b).Encode(body); err != nil {
			return
		}
	}
	var req *http.Request
	if req, err = http.NewRequestWithContext(ctx, urlMethod, cloudUrl, b); err != nil {
		return err
	}

	// Any custom HTTP headers passed through the Context
	if httpHeaders, hasHeaders := ctx.Value(HTTP_HEADERS_CONTEXT_KEY).(map[string]string); hasHeaders {
		for key, header := range httpHeaders {
			req.Header.Add(key, header)
		}
	}
	req.Header.Set("Content-Type", "application/json")
	req.Header.Set("Accept", "application/json")

	if resp, err = client.Do(req); err != nil {
		return err
	}

	defer resp.Body.Close()

	if err == nil && resp.StatusCode >= 400 {
		b, _ := io.ReadAll(resp.Body)
		err = errors.NewServiceUnavailable(fmt.Sprintf("Recieved bad response code of %d from solr with response: %s", resp.StatusCode, string(b)))
		// try to read the response, just in case Solr returned an error that we can read
		json.NewDecoder(bytes.NewReader(b)).Decode(&response)
	}

	if err == nil {
		err = json.NewDecoder(resp.Body).Decode(&response)
	}

	return err
}