func()

in client/ekmclient/confidentialekmclient.go [85:130]


func (c ConfidentialEKMClient) post(ctx context.Context, url string, protoReq, protoResp proto.Message) error {
	marshaled, err := protojson.Marshal(protoReq)
	if err != nil {
		return fmt.Errorf("error marshaling request: %w", err)
	}

	httpReq, err := http.NewRequestWithContext(ctx, "POST", url, bytes.NewBuffer(marshaled))
	if err != nil {
		return fmt.Errorf("error creating HTTP request: %w", err)
	}

	httpReq.Header.Set("Content-Type", "application/json")

	if c.AuthToken != "" {
		httpReq.Header.Set("Authorization", "Bearer "+c.AuthToken)
	}

	client := http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{
				RootCAs: c.CertPool,
			},
		},
	}

	httpResp, err := client.Do(httpReq)
	if err != nil {
		return fmt.Errorf("HTTP call returned with error: %w", err)
	}

	defer httpResp.Body.Close()
	respBody, err := ioutil.ReadAll(httpResp.Body)
	if err != nil {
		return fmt.Errorf("error reading HTTP response body: %w", err)
	}

	if httpResp.StatusCode != http.StatusOK {
		return fmt.Errorf("non-OK status returned: %s - %s", httpResp.Status, string(respBody))
	}

	if err = protojson.Unmarshal(respBody, protoResp); err != nil {
		return fmt.Errorf("error unmarshaling response: %w", err)
	}

	return nil
}