Search in sources :

Example 1 with RequestResult

use of org.talend.components.marketo.runtime.client.rest.response.RequestResult in project components by Talend.

the class MarketoBaseRESTClient method executeGetRequest.

public RequestResult executeGetRequest(Class<?> resultClass) throws MarketoException {
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod(QUERY_METHOD_GET);
        urlConn.setDoOutput(true);
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            Reader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            return (RequestResult) gson.fromJson(reader, resultClass);
        } else {
            LOG.error("GET request failed: {}.", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("GET request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : RequestResult(org.talend.components.marketo.runtime.client.rest.response.RequestResult) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) Gson(com.google.gson.Gson) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 2 with RequestResult

use of org.talend.components.marketo.runtime.client.rest.response.RequestResult in project components by Talend.

the class MarketoBaseRESTClient method executePostRequest.

public RequestResult executePostRequest(Class<?> resultClass, JsonObject inputJson) throws MarketoException {
    try {
        URL url = new URL(current_uri.toString());
        HttpsURLConnection urlConn = (HttpsURLConnection) url.openConnection();
        urlConn.setRequestMethod(QUERY_METHOD_POST);
        urlConn.setRequestProperty(REQUEST_PROPERTY_CONTENT_TYPE, REQUEST_VALUE_APPLICATION_JSON);
        urlConn.setRequestProperty(REQUEST_PROPERTY_ACCEPT, REQUEST_VALUE_TEXT_JSON);
        urlConn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(urlConn.getOutputStream());
        wr.write(inputJson.toString());
        wr.flush();
        wr.close();
        int responseCode = urlConn.getResponseCode();
        if (responseCode == 200) {
            InputStream inStream = urlConn.getInputStream();
            InputStreamReader reader = new InputStreamReader(inStream);
            Gson gson = new Gson();
            // LOG.error("{}", convertStreamToString(inStream));
            return (RequestResult) gson.fromJson(reader, resultClass);
        } else {
            LOG.error("POST request failed: {}", responseCode);
            throw new MarketoException(REST, responseCode, "Request failed! Please check your request setting!");
        }
    } catch (IOException e) {
        LOG.error("GET request failed: {}", e.getMessage());
        throw new MarketoException(REST, e.getMessage());
    }
}
Also used : RequestResult(org.talend.components.marketo.runtime.client.rest.response.RequestResult) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Gson(com.google.gson.Gson) OutputStreamWriter(java.io.OutputStreamWriter) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

Gson (com.google.gson.Gson)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 InputStreamReader (java.io.InputStreamReader)2 URL (java.net.URL)2 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)2 RequestResult (org.talend.components.marketo.runtime.client.rest.response.RequestResult)2 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)2 OutputStreamWriter (java.io.OutputStreamWriter)1 Reader (java.io.Reader)1