Skip to content
This repository was archived by the owner on Jan 19, 2024. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 34 additions & 20 deletions src/main/java/com/exacttarget/fuelsdk/ETRestConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@
import java.net.ProtocolException;
import java.net.URL;

import java.nio.charset.Charset;

import com.google.gson.Gson;
import com.google.gson.JsonParser;

Expand Down Expand Up @@ -104,23 +106,7 @@ public Response get(String path)
public Response post(String path, String payload)
throws ETSdkException
{
HttpURLConnection connection = null;
try {
Response response = new Response();
connection = sendRequest(path, Method.POST, payload);
String json = receiveResponse(connection);
response.setRequestId(connection.getHeaderField("X-Mashery-Message-ID"));
response.setResponseCode(connection.getResponseCode());
response.setResponseMessage(connection.getResponseMessage());
response.setResponsePayload(json);
return response;
} catch (IOException ex) {
throw new ETSdkException(ex);
} finally {
if (connection != null) {
connection.disconnect();
}
}
return post(path,payload,null);
}

public Response patch(String path, String payload)
Expand Down Expand Up @@ -180,17 +166,23 @@ private HttpURLConnection sendRequest(String path, Method method)

private HttpURLConnection sendRequest(String path, Method method, String payload)
throws ETSdkException
{
return sendRequest(path, method, payload, null);
}

private HttpURLConnection sendRequest(String path, Method method, String payload, Charset charset)
throws ETSdkException
{
URL url = null;
try {
url = new URL(endpoint + path);
} catch (MalformedURLException ex) {
throw new ETSdkException(endpoint + path + ": bad URL", ex);
}
return sendRequest(url, method, payload);
return sendRequest(url, method, payload, charset);
}

private HttpURLConnection sendRequest(URL url, Method method, String payload)
private HttpURLConnection sendRequest(URL url, Method method, String payload, Charset charset)
throws ETSdkException
{
Gson gson = client.getGson();
Expand Down Expand Up @@ -246,7 +238,7 @@ private HttpURLConnection sendRequest(URL url, Method method, String payload)
OutputStream os = null;
try {
os = connection.getOutputStream();
os.write(payload.getBytes());
os.write(charset != null ? payload.getBytes(charset) : payload.getBytes());
os.flush();
} catch (IOException ex) {
throw new ETSdkException("error writing " + url, ex);
Expand Down Expand Up @@ -316,6 +308,28 @@ private String receiveResponse(HttpURLConnection connection)
return response;
}

public Response post(String path, String payload, Charset charset)
throws ETSdkException
{
HttpURLConnection connection = null;
try {
Response response = new Response();
connection = sendRequest(path, Method.POST, payload, charset);
String json = receiveResponse(connection);
response.setRequestId(connection.getHeaderField("X-Mashery-Message-ID"));
response.setResponseCode(connection.getResponseCode());
response.setResponseMessage(connection.getResponseMessage());
response.setResponsePayload(json);
return response;
} catch (IOException ex) {
throw new ETSdkException(ex);
} finally {
if (connection != null) {
connection.disconnect();
}
}
}

public enum Method {
GET, POST, PATCH, DELETE
}
Expand Down