Search in sources :

Example 1 with BaseMandrillStringResponse

use of com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse in project Java-Mandrill-Wrapper by cribbstechnologies.

the class UsersTest method testPing.

@Test
public void testPing() {
    BaseMandrillRequest baseRequest = new BaseMandrillRequest();
    try {
        BaseMandrillStringResponse response = usersRequest.performPing(baseRequest);
        assertEquals("\"PONG!\"", response.getResponse());
    } catch (RequestFailedException e) {
        fail(e.getMessage());
    }
}
Also used : BaseMandrillStringResponse(com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse) RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) BaseMandrillRequest(com.cribbstechnologies.clients.mandrill.model.BaseMandrillRequest) Test(org.junit.Test)

Example 2 with BaseMandrillStringResponse

use of com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse in project Java-Mandrill-Wrapper by cribbstechnologies.

the class MandrillUsersRequestTest method testPerformPing.

@Test
public void testPerformPing() throws RequestFailedException {
    usersRequest.setRequest(request);
    BaseMandrillStringResponse response = new BaseMandrillStringResponse();
    Mockito.when(request.postRequest(bmr, ServiceMethods.Users.PING, null)).thenReturn(response);
    usersRequest.performPing(bmr);
    Mockito.verify(request).postRequest(bmr, ServiceMethods.Users.PING, null);
}
Also used : BaseMandrillStringResponse(com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse) Test(org.junit.Test)

Example 3 with BaseMandrillStringResponse

use of com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse in project Java-Mandrill-Wrapper by cribbstechnologies.

the class MandrillRESTRequest method performPostRequest.

private BaseMandrillResponse performPostRequest(BaseMandrillRequest request, String serviceMethod, Object responseClass, TypeReference reference) throws RequestFailedException {
    try {
        request.setKey(config.getApiKey());
        HttpPost postRequest = new HttpPost(config.getServiceUrl() + serviceMethod);
        String postData = getPostData(request);
        StringEntity input = new StringEntity(postData, "UTF-8");
        input.setContentType("application/json");
        postRequest.setEntity(input);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
        StringBuffer sb = new StringBuffer();
        String output;
        // System.out.println("Output from Server .... \n");
        while ((output = br.readLine()) != null) {
            sb.append(output);
        // System.out.println(output);
        }
        String responseString = sb.toString();
        EntityUtils.consume(response.getEntity());
        if (response.getStatusLine().getStatusCode() != 200) {
            //throw new RequestFailedException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + " " + responseString);
            throw new RequestFailedException("Failed : HTTP error code : " + response.getStatusLine().getStatusCode() + " " + responseString, objectMapper.readValue(responseString, MandrillError.class));
        }
        // for whatever reason the ping response isn't well-formed
        if (ServiceMethods.Users.PING.equals(serviceMethod) && responseString.indexOf("PONG!") > -1) {
            return new BaseMandrillStringResponse(responseString);
        }
        if (reference == null) {
            return convertResponseData(responseString, responseClass);
        } else {
            return convertAnonymousListResponseData(responseString, responseClass, reference);
        }
    } catch (MalformedURLException mURLE) {
        throw new RequestFailedException("Malformed url", mURLE);
    } catch (JsonGenerationException jge) {
        throw new RequestFailedException("Json Generation Exception", jge);
    } catch (JsonMappingException jme) {
        throw new RequestFailedException("Json Mapping Exception", jme);
    } catch (IOException ioe) {
        throw new RequestFailedException("IOException", ioe);
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) MalformedURLException(java.net.MalformedURLException) InputStreamReader(java.io.InputStreamReader) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) BaseMandrillStringResponse(com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse) RequestFailedException(com.cribbstechnologies.clients.mandrill.exception.RequestFailedException) MandrillError(com.cribbstechnologies.clients.mandrill.model.MandrillError) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) BufferedReader(java.io.BufferedReader) JsonGenerationException(com.fasterxml.jackson.core.JsonGenerationException)

Aggregations

BaseMandrillStringResponse (com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillStringResponse)3 RequestFailedException (com.cribbstechnologies.clients.mandrill.exception.RequestFailedException)2 Test (org.junit.Test)2 BaseMandrillRequest (com.cribbstechnologies.clients.mandrill.model.BaseMandrillRequest)1 MandrillError (com.cribbstechnologies.clients.mandrill.model.MandrillError)1 JsonGenerationException (com.fasterxml.jackson.core.JsonGenerationException)1 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 MalformedURLException (java.net.MalformedURLException)1 HttpResponse (org.apache.http.HttpResponse)1 HttpPost (org.apache.http.client.methods.HttpPost)1 StringEntity (org.apache.http.entity.StringEntity)1