func()

in kibana/fleet.go [162:205]


func (client *Client) CreateDownloadSource(ctx context.Context, source DownloadSource) (DownloadSourceResponse, error) {
	reqBody, err := json.Marshal(source)
	if err != nil {
		return DownloadSourceResponse{},
			fmt.Errorf("unable to marshal DownloadSource into JSON: %w", err)
	}

	resp, err := client.Connection.SendWithContext(
		ctx,
		http.MethodPost,
		fleetAgentDownloadSourcesAPI,
		nil,
		nil,
		bytes.NewReader(reqBody))
	if err != nil {
		return DownloadSourceResponse{},
			fmt.Errorf("error calling Agent Binary Download Sources API: %w", err)
	}
	defer resp.Body.Close()

	if resp.StatusCode != http.StatusOK {
		var respBody string
		if bs, err := io.ReadAll(resp.Body); err != nil {
			respBody = "could not read response body"
		} else {
			respBody = string(bs)
		}

		client.log.Errorw(
			"could not create download source, kibana returned "+resp.Status,
			"http.response.body.content", respBody)
		return DownloadSourceResponse{},
			fmt.Errorf("could not create download source, kibana returned %s. response body: %s: %w",
				resp.Status, respBody, err)
	}

	body := DownloadSourceResponse{}
	if err = json.NewDecoder(resp.Body).Decode(&body); err != nil {
		return DownloadSourceResponse{},
			fmt.Errorf("failed parsing download source response: %w", err)
	}

	return body, nil
}