Example #1
0
// ListVoices retrieves list of voices from the api
func (client *Ivona) ListVoices(options Voice) (*ListResponse, error) {
	b, err := json.Marshal(options)
	if err != nil {
		return nil, err
	}

	r, _ := http.NewRequest("POST", listVoicesAPI, bytes.NewReader(b))
	r.Header.Set("Content-Type", "application/json")

	awsClient := aws4.Client{Keys: &aws4.Keys{
		AccessKey: client.AccessKey,
		SecretKey: client.SecretKey,
	}}

	resp, err := awsClient.Do(r)
	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	data, err := ioutil.ReadAll(resp.Body)
	if err != nil {
		return nil, err
	}

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("Got non 200 status code: %s %q", resp.Status, data)
	}

	list := new(ListResponse)
	err = json.Unmarshal(data, list)
	if err != nil {
		return nil, err
	}

	list.RequestID = resp.Header["X-Amzn-Ivonattsrequestid"][0]
	list.ContentType = resp.Header["Content-Type"][0]

	return list, nil
}
Example #2
0
// CreateSpeech performs a synthesis of the requested text and returns the audio stream containing the speech.
func (client *Ivona) CreateSpeech(options SpeechOptions) (*SpeechResponse, error) {
	b, err := json.Marshal(options)

	if err != nil {
		return nil, err
	}

	r, _ := http.NewRequest("POST", createSpeechAPI, bytes.NewReader(b))
	r.Header.Set("Content-Type", "application/json")

	awsClient := aws4.Client{Keys: &aws4.Keys{
		AccessKey: client.AccessKey,
		SecretKey: client.SecretKey,
	}}

	resp, err := awsClient.Do(r)

	if err != nil {
		return nil, err
	}

	defer resp.Body.Close()

	data, err := ioutil.ReadAll(resp.Body)

	if err != nil {
		return nil, err
	}

	if resp.StatusCode != 200 {
		return nil, fmt.Errorf("Got non 200 status code: %s %q", resp.Status, data)
	}

	return &SpeechResponse{
		Audio:       data,
		RequestID:   resp.Header["X-Amzn-Ivonattsrequestid"][0],
		ContentType: resp.Header["Content-Type"][0],
	}, nil
}