func()

in go-example-logs-api-extension/logsapi/client.go [117:153]


func (c *Client) Subscribe(types []EventType, bufferingCfg BufferingCfg, destination Destination, extensionId string) (*SubscribeResponse, error) {

	data, err := json.Marshal(
		&SubscribeRequest{
			SchemaVersion: SchemaVersionLatest,
			EventTypes:    types,
			BufferingCfg:  bufferingCfg,
			Destination:   destination,
		})
	if err != nil {
		return nil, errors.WithMessage(err, "failed to marshal SubscribeRequest")
	}

	headers := make(map[string]string)
	headers[lambdaAgentIdentifierHeaderKey] = extensionId
	url := fmt.Sprintf("%s/2020-08-15/logs", c.logsApiBaseUrl)
	resp, err := httpPutWithHeaders(c.httpClient, url, data, &headers)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()

	if resp.StatusCode == http.StatusAccepted {
		fmt.Println("WARNING!!! Logs API is not supported! Is this extension running in a local sandbox?")
	} else if resp.StatusCode != http.StatusOK {
		body, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return nil, errors.Errorf("%s failed: %d[%s]", url, resp.StatusCode, resp.Status)
		}

		return nil, errors.Errorf("%s failed: %d[%s] %s", url, resp.StatusCode, resp.Status, string(body))
	}

	body, _ := ioutil.ReadAll(resp.Body)

	return &SubscribeResponse{string(body)}, nil
}