Search in sources :

Example 1 with VotoResponseDto

use of org.motechproject.mots.dto.VotoResponseDto in project mots by motech-implementations.

the class IvrService method sendModuleAssignedMessage.

private void sendModuleAssignedMessage(String subscriberId) throws IvrException {
    IvrConfig ivrConfig = ivrConfigService.getConfig();
    String messageId = ivrConfig.getModuleAssignedMessageId();
    String sendSmsIfVoiceFails = boolToIntAsString(ivrConfig.getSendSmsIfVoiceFails());
    String detectVoiceMailAction = boolToIntAsString(ivrConfig.getDetectVoicemailAction());
    String retryAttemptsShort = Integer.toString(ivrConfig.getRetryAttemptsShort());
    String retryDelayShort = Integer.toString(ivrConfig.getRetryDelayShort());
    String retryAttemptsLong = Integer.toString(ivrConfig.getRetryAttemptsLong());
    String retryDelayLong = Integer.toString(ivrConfig.getRetryDelayLong());
    MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
    params.add(SEND_TO_SUBSCRIBERS, subscriberId);
    params.add(MESSAGE_ID, messageId);
    params.add(SEND_SMS_IF_VOICE_FAILS, sendSmsIfVoiceFails);
    params.add(DETECT_VOICEMAIL_ACTION, detectVoiceMailAction);
    params.add(RETRY_ATTEMPTS_SHORT, retryAttemptsShort);
    params.add(RETRY_DELAY_SHORT, retryDelayShort);
    params.add(RETRY_ATTEMPTS_LONG, retryAttemptsLong);
    params.add(RETRY_DELAY_LONG, retryDelayLong);
    sendVotoRequest(getAbsoluteUrl(SEND_MESSAGE_URL), params, new ParameterizedTypeReference<VotoResponseDto<String>>() {
    }, HttpMethod.POST);
}
Also used : VotoResponseDto(org.motechproject.mots.dto.VotoResponseDto) LinkedMultiValueMap(org.springframework.util.LinkedMultiValueMap) IvrConfig(org.motechproject.mots.domain.IvrConfig)

Example 2 with VotoResponseDto

use of org.motechproject.mots.dto.VotoResponseDto in project mots by motech-implementations.

the class IvrService method sendVotoRequest.

private <T> T sendVotoRequest(String url, MultiValueMap<String, String> params, ParameterizedTypeReference<T> responseType, HttpMethod method) throws IvrException {
    params.add(API_KEY, ivrApiKey);
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(url).queryParams(params);
    HttpEntity<?> request = new HttpEntity<>(headers);
    try {
        ResponseEntity<T> responseEntity = restTemplate.exchange(builder.build().toString(), method, request, responseType);
        return responseEntity.getBody();
    } catch (RestClientResponseException ex) {
        String responseBodyJson = ex.getResponseBodyAsString();
        String responseMessage = responseBodyJson;
        String clearVotoInfo = "";
        try {
            VotoResponseDto<String> votoResponse = mapper.readValue(responseBodyJson, new TypeReference<VotoResponseDto<String>>() {
            });
            if (votoResponse.getMessage() != null) {
                responseMessage = votoResponse.getMessage();
                clearVotoInfo = "Invalid IVR service response: " + responseMessage;
            }
        } catch (IOException e) {
            responseMessage = responseBodyJson;
        }
        String message = "Invalid IVR service response: " + ex.getRawStatusCode() + " " + ex.getStatusText() + ", Response body: " + responseMessage;
        throw new IvrException(message, ex, clearVotoInfo);
    } catch (RestClientException ex) {
        String message = "Error occurred when sending request to IVR service: " + ex.getMessage();
        throw new IvrException(message, ex);
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) IvrException(org.motechproject.mots.exception.IvrException) HttpEntity(org.springframework.http.HttpEntity) VotoResponseDto(org.motechproject.mots.dto.VotoResponseDto) IOException(java.io.IOException) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) RestClientException(org.springframework.web.client.RestClientException) RestClientResponseException(org.springframework.web.client.RestClientResponseException) ParameterizedTypeReference(org.springframework.core.ParameterizedTypeReference) TypeReference(com.fasterxml.jackson.core.type.TypeReference)

Aggregations

VotoResponseDto (org.motechproject.mots.dto.VotoResponseDto)2 TypeReference (com.fasterxml.jackson.core.type.TypeReference)1 IOException (java.io.IOException)1 IvrConfig (org.motechproject.mots.domain.IvrConfig)1 IvrException (org.motechproject.mots.exception.IvrException)1 ParameterizedTypeReference (org.springframework.core.ParameterizedTypeReference)1 HttpEntity (org.springframework.http.HttpEntity)1 HttpHeaders (org.springframework.http.HttpHeaders)1 LinkedMultiValueMap (org.springframework.util.LinkedMultiValueMap)1 RestClientException (org.springframework.web.client.RestClientException)1 RestClientResponseException (org.springframework.web.client.RestClientResponseException)1 UriComponentsBuilder (org.springframework.web.util.UriComponentsBuilder)1