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());
}
}
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());
}
}
Aggregations