Skip to content

Commit

Permalink
Merge pull request #23 from gscriver/feature/pagination
Browse files Browse the repository at this point in the history
Fixed #22 Get methods only return the first 10 objects
  • Loading branch information
alexanderwe authored Jun 22, 2018
2 parents a8d21d7 + d1710ff commit 5377715
Show file tree
Hide file tree
Showing 10 changed files with 585 additions and 307 deletions.
186 changes: 118 additions & 68 deletions src/main/java/com/github/alexanderwe/bananaj/connection/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,31 @@
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import com.github.alexanderwe.bananaj.exceptions.TransportException;

/**
* Created by Alexander on 10.08.2016.
*/
public class Connection {


public String do_Get(URL url, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url.toURI());
httpget.addHeader("Authorization", authorization);
CloseableHttpResponse response = httpclient. execute(httpget);

public String do_Get(URL url, String authorization) throws TransportException {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
InputStream entityStream = null;

try {
httpclient = HttpClients.createDefault();
HttpGet httpget = new HttpGet(url.toURI());
httpget.addHeader("Authorization", authorization);
response = httpclient.execute(httpget);

int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode + "\n");
//System.out.println("\nSending 'GET' request to URL : " + url);
//System.out.println("Response Code : " + responseCode + "\n");
if (responseCode >= 400) {
throw new TransportException("Error: " + responseCode + " GET " + url.toExternalForm() + " " + response.getStatusLine().getReasonPhrase());
}

HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
Expand All @@ -49,25 +57,33 @@ public String do_Get(URL url, String authorization) throws Exception{
}
}
return strbuilder.toString();
} catch (Exception e) {
throw new TransportException("GET " + url.toExternalForm() + " failed", e);
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
if (entityStream != null) {try {entityStream.close();} catch (Exception e) { }}
if (response != null) {try {response.close();} catch (Exception e) { }}
}
}

public String do_Post(URL url, String post_string, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url.toURI());
httppost.addHeader("Authorization", authorization);
httppost.addHeader("Content-Type", "application/json; charset=UTF-8");
httppost.setEntity(EntityBuilder.create().setText(post_string).build());
CloseableHttpResponse response = httpclient. execute(httppost);

public String do_Post(URL url, String post_string, String authorization) throws TransportException {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
InputStream entityStream = null;

try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url.toURI());
httppost.addHeader("Authorization", authorization);
httppost.addHeader("Content-Type", "application/json; charset=UTF-8");
httppost.setEntity(EntityBuilder.create().setText(post_string).build());
response = httpclient.execute(httppost);

int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + url + System.lineSeparator() + "Send data: " + (post_string.length() > 500 ? post_string.substring(0, 500)+"..." : post_string));
System.out.println("Response Code : " + responseCode + "\n");
//System.out.println("\nSending 'POST' request to URL : " + url + System.lineSeparator() + "Send data: " + (post_string.length() > 500 ? post_string.substring(0, 500)+"..." : post_string));
//System.out.println("Response Code : " + responseCode + "\n");
if (responseCode >= 400) {
throw new TransportException("Error: " + responseCode + " POST " + url.toExternalForm() + " " + response.getStatusLine().getReasonPhrase());
}

HttpEntity entity = response.getEntity();
if (entity != null) {
Expand All @@ -84,25 +100,33 @@ public String do_Post(URL url, String post_string, String authorization) throws
return strbuilder.toString();
}
return null;
} catch (Exception e) {
throw new TransportException("POST " + post_string.length() + " bytes to " + url.toExternalForm() + " failed", e);
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
if (entityStream != null) {try {entityStream.close();} catch (Exception e) { }}
if (response != null) {try {response.close();} catch (Exception e) { }}
}
}

public String do_Patch(URL url, String patch_string, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPatch httppatch = new HttpPatch(url.toURI());
httppatch.addHeader("Authorization", authorization);
httppatch.addHeader("Content-Type", "application/json; charset=UTF-8");
httppatch.setEntity(EntityBuilder.create().setText(patch_string).build());
CloseableHttpResponse response = httpclient. execute(httppatch);

public String do_Patch(URL url, String patch_string, String authorization) throws TransportException {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
InputStream entityStream = null;

try {
httpclient = HttpClients.createDefault();
HttpPatch httppatch = new HttpPatch(url.toURI());
httppatch.addHeader("Authorization", authorization);
httppatch.addHeader("Content-Type", "application/json; charset=UTF-8");
httppatch.setEntity(EntityBuilder.create().setText(patch_string).build());
response = httpclient.execute(httppatch);

int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'PATCH' request to URL : " + url + System.lineSeparator() + "Send data: " + (patch_string.length() > 500 ? patch_string.substring(0, 500)+"..." : patch_string));
System.out.println("Response Code : " + responseCode + "\n");
//System.out.println("\nSending 'PATCH' request to URL : " + url + System.lineSeparator() + "Send data: " + (patch_string.length() > 500 ? patch_string.substring(0, 500)+"..." : patch_string));
//System.out.println("Response Code : " + responseCode + "\n");
if (responseCode >= 400) {
throw new TransportException("Error: " + responseCode + " PATCH " + url.toExternalForm() + " " + response.getStatusLine().getReasonPhrase());
}

HttpEntity entity = response.getEntity();
if (entity != null) {
Expand All @@ -119,25 +143,33 @@ public String do_Patch(URL url, String patch_string, String authorization) throw
return strbuilder.toString();
}
return null;
} catch (Exception e) {
throw new TransportException("PATCH " + patch_string.length() + " bytes to " + url.toExternalForm() + " failed", e);
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
if (entityStream != null) {try {entityStream.close();} catch (Exception e) { }}
if (response != null) {try {response.close();} catch (Exception e) { }}
}
}

public String do_Put(URL url, String put_string, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPut httpput = new HttpPut(url.toURI());
httpput.addHeader("Authorization", authorization);
httpput.addHeader("Content-Type", "application/json; charset=UTF-8");
httpput.setEntity(EntityBuilder.create().setText(put_string).build());
CloseableHttpResponse response = httpclient. execute(httpput);

public String do_Put(URL url, String put_string, String authorization) throws TransportException {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
InputStream entityStream = null;

try {
httpclient = HttpClients.createDefault();
HttpPut httpput = new HttpPut(url.toURI());
httpput.addHeader("Authorization", authorization);
httpput.addHeader("Content-Type", "application/json; charset=UTF-8");
httpput.setEntity(EntityBuilder.create().setText(put_string).build());
response = httpclient.execute(httpput);

int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'PUT' request to URL : " + url + System.lineSeparator() + "Send data: " + (put_string.length() > 500 ? put_string.substring(0, 500)+"..." : put_string));
System.out.println("Response Code : " + responseCode + "\n");
//System.out.println("\nSending 'PUT' request to URL : " + url + System.lineSeparator() + "Send data: " + (put_string.length() > 500 ? put_string.substring(0, 500)+"..." : put_string));
//System.out.println("Response Code : " + responseCode + "\n");
if (responseCode >= 400) {
throw new TransportException("Error: " + responseCode + " PUT " + url.toExternalForm() + " " + response.getStatusLine().getReasonPhrase());
}

HttpEntity entity = response.getEntity();
if (entity != null) {
Expand All @@ -154,24 +186,32 @@ public String do_Put(URL url, String put_string, String authorization) throws Ex
return strbuilder.toString();
}
return null;
} catch (Exception e) {
throw new TransportException("PUT " + put_string.length() + " bytes to " + url.toExternalForm() + " failed", e);
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
if (entityStream != null) {try {entityStream.close();} catch (Exception e) { }}
if (response != null) {try {response.close();} catch (Exception e) { }}
}
}

public String do_Post(URL url, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url.toURI());
httppost.addHeader("Authorization", authorization);
httppost.addHeader("Content-Type", "application/json; charset=UTF-8");
CloseableHttpResponse response = httpclient. execute(httppost);

public String do_Post(URL url, String authorization) throws TransportException {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
InputStream entityStream = null;

try {
httpclient = HttpClients.createDefault();
HttpPost httppost = new HttpPost(url.toURI());
httppost.addHeader("Authorization", authorization);
httppost.addHeader("Content-Type", "application/json; charset=UTF-8");
response = httpclient.execute(httppost);

int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Response Code : " + responseCode + "\n");
//System.out.println("\nSending 'POST' request to URL : " + url);
//System.out.println("Response Code : " + responseCode + "\n");
if (responseCode >= 400) {
throw new TransportException("Error: " + responseCode + " POST " + url.toExternalForm() + " " + response.getStatusLine().getReasonPhrase());
}

HttpEntity entity = response.getEntity();
if (entity != null) {
Expand All @@ -188,24 +228,32 @@ public String do_Post(URL url, String authorization) throws Exception{
return strbuilder.toString();
}
return null;
} catch (Exception e) {
throw new TransportException("POST " + url.toExternalForm() + " failed", e);
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
if (entityStream != null) {try {entityStream.close();} catch (Exception e) { }}
if (response != null) {try {response.close();} catch (Exception e) { }}
}
}

public String do_Delete(URL url, String authorization) throws Exception{
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpDelete httpdelete = new HttpDelete(url.toURI());
httpdelete.addHeader("Authorization", authorization);
httpdelete.addHeader("Content-Type", "application/json; charset=UTF-8");
CloseableHttpResponse response = httpclient. execute(httpdelete);

public String do_Delete(URL url, String authorization) throws TransportException {
CloseableHttpClient httpclient = null;
CloseableHttpResponse response = null;
InputStream entityStream = null;

try {
httpclient = HttpClients.createDefault();
HttpDelete httpdelete = new HttpDelete(url.toURI());
httpdelete.addHeader("Authorization", authorization);
httpdelete.addHeader("Content-Type", "application/json; charset=UTF-8");
response = httpclient.execute(httpdelete);

int responseCode = response.getStatusLine().getStatusCode();
System.out.println("\nSending 'DELETE' request to URL : " + url);
System.out.println("Response Code : " + responseCode + "\n");
//System.out.println("\nSending 'DELETE' request to URL : " + url);
//System.out.println("Response Code : " + responseCode + "\n");
if (responseCode >= 400) {
throw new TransportException("Error: " + responseCode + " DELETE " + url.toExternalForm() + " " + response.getStatusLine().getReasonPhrase());
}

HttpEntity entity = response.getEntity();
if (entity != null) {
Expand All @@ -222,9 +270,11 @@ public String do_Delete(URL url, String authorization) throws Exception{
return strbuilder.toString();
}
return null;
} catch (Exception e) {
throw new TransportException("DELETE " + url.toExternalForm() + " failed", e);
} finally {
if (entityStream != null) {entityStream.close();}
response.close();
if (entityStream != null) {try {entityStream.close();} catch (Exception e) { }}
if (response != null) {try {response.close();} catch (Exception e) { }}
}
}
}
Loading

0 comments on commit 5377715

Please sign in to comment.