Jesse Jose
Jesse Jose

Reputation: 21

Download Twilio Recording as an InputStream using Twilio Java Sdk v7.x

I am working on upgrading our current Twilio Java SDK client from version 6.x to version 7.x . One of the problems that I have run into is in retrieving the InputStream for a recording. Below is version 6.x sample of the code that I had but could not find a way to retrieve the InputStream for the recording in version 7.x . (7.0.0-rc10 currently) Could you possibly guide me on what I am doing wrong ?

code snippet version = "6.x"

public InputStream retrieveRecording(String recordingSid) {
    Recording recordingToRetrieve = new Recording(twilioRestClient, recordingSid);
    recordingToRetrieve.setRequestAccountSid("xxxxxxxxx");
    return recordingToRetrieve.getMedia(".mp3");
}

code snippet version = "7.x"

public InputStream retrieveRecording(String recordingSid) {
    Recording recordingToRetrieve = Recording.fetch("xxxxxxxxx", recordingSid).execute();
    //How do I get the mp3 media as an Input stream ?
}

Upvotes: 1

Views: 256

Answers (1)

Jesse Jose
Jesse Jose

Reputation: 21

Unfortunately there is no easy way in the Twilio SDK V7 7.0.0-rc-10 at this point to retrieve an InputStream for a recording, it ideally should be built into the SDK but in the meantime here is how I solved the problem of retrieving the download

 private InputStream retrieveRecording(String accountSid, String authToken, String recordingSid) {
    Twilio.init(accountSid, authToken);
    Recording recordingToRetrieve = Recording.fetch(accountSid, recordingSid).execute();
    String uri = recordingToRetrieve.getUri();
    String mp3Uri = uri.replace(".json", ".mp3");
    Request request = new Request(
            HttpMethod.GET,
            TwilioRestClient.Domains.API,
            mp3Uri,
            accountSid);
    Response mp3response = Twilio.getRestClient().request(request);
    if (mp3response == null) {
        throw new ApiConnectionException("Recording media fetch failed: Unable to connect to server");
    } else if (!TwilioRestClient.SUCCESS.apply(mp3response.getStatusCode())) {
        RestException restException = RestException.fromJson(mp3response.getStream(), Twilio.getRestClient().getObjectMapper());
        if (restException == null) {
            throw new ApiException("Server Error, no content");
        }

        throw new ApiException(
                restException.getMessage(),
                restException.getCode(),
                restException.getMoreInfo(),
                restException.getStatus(),
                null);
    }
    return mp3response.getStream();
}

Upvotes: 1

Related Questions