Add POST method extension.

main
sigonasr2, Sig, Sigo 3 years ago
parent dfe1e58dce
commit f3ff26fa01
  1. 45
      sig/client.java
  2. 19
      sig/requests/GETRequest.java
  3. 53
      sig/requests/POSTRequest.java

@ -1,34 +1,49 @@
package sig; package sig;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.nio.file.Path;
import java.nio.file.Paths;
import sig.exceptions.FailedResponseException; import sig.exceptions.FailedResponseException;
import sig.requests.GETRequest; import sig.requests.GETRequest;
import sig.requests.POSTRequest;
public class client { public class client {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
//Regular get request: //Regular get request:
GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2"); GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
System.out.println(((HttpResponse<String>)res.run()).body()); System.out.println(((HttpResponse<String>)res.run()).body());
//Download to file: /*GET Block
//GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2",30000,Paths.get("test.html"),"default","value");
//System.out.println((Path)((HttpResponse<Path>)res.run()).body());
//Get request with headers: //Regular get request:
res = new GETRequest("https://postman-echo.com/headers",30000,"test-header1","value1","test-header2","value2"); GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
System.out.println(((HttpResponse<String>)res.run()).body()); System.out.println(((HttpResponse<String>)res.run()).body());
//Get request with headers: //Download to file:
res = new GETRequest("https://postman-echo.com/response-headers?foo1=bar1&foo2=bar2",30000,"test-header1","value1","test-header2","value2"); //GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2",30000,Paths.get("test.html"),"default","value");
System.out.println(((HttpResponse<String>)res.run()).body()); //System.out.println((Path)((HttpResponse<Path>)res.run()).body());
//Get request with headers:
res = new GETRequest("https://postman-echo.com/headers",30000,"test-header1","value1","test-header2","value2");
System.out.println(((HttpResponse<String>)res.run()).body());
//Get request with headers:
res = new GETRequest("https://postman-echo.com/response-headers?foo1=bar1&foo2=bar2",30000,"test-header1","value1","test-header2","value2");
System.out.println(((HttpResponse<String>)res.run()).body());
//Get request with authentication:
res = new GETRequest("https://postman-echo.com/basic-auth","postman","password",30000,null);
System.out.println(((HttpResponse<String>)res.run()).body());
*/
//Regular POST request with body:
POSTRequest postRes = new POSTRequest("https://postman-echo.com/post","Test body");
System.out.println(((HttpResponse<String>)postRes.run()).body());
//POST request with body and headers:
postRes = new POSTRequest("https://postman-echo.com/post","Test body", 30000, "header1","value1", "header2","value2");
System.out.println(((HttpResponse<String>)postRes.run()).body());
//Get request with authentication:
res = new GETRequest("https://postman-echo.com/basic-auth","postman","password",30000,null);
System.out.println(((HttpResponse<String>)res.run()).body());
} catch (FailedResponseException e) { } catch (FailedResponseException e) {
e.printStackTrace(); e.printStackTrace();
} }

@ -35,7 +35,6 @@ public class GETRequest{
this.headers = headers; this.headers = headers;
this.timeout = timeout; this.timeout = timeout;
this.file=file; this.file=file;
build();
} }
/** /**
* @param file The file path info, use this for file downloads or set to null for standard text. * @param file The file path info, use this for file downloads or set to null for standard text.
@ -54,6 +53,7 @@ public class GETRequest{
this(url,30000,null); this(url,30000,null);
} }
public HttpResponse<?> run() throws FailedResponseException { public HttpResponse<?> run() throws FailedResponseException {
build();
try { try {
if (file==null) { if (file==null) {
return client.send(req,BodyHandlers.ofString()); return client.send(req,BodyHandlers.ofString());
@ -65,17 +65,22 @@ public class GETRequest{
} }
throw new FailedResponseException("No proper response returned. THIS SHOULD NOT BE HAPPENING!"); throw new FailedResponseException("No proper response returned. THIS SHOULD NOT BE HAPPENING!");
} }
private void build(){ protected java.net.http.HttpRequest.Builder finalizeRequestPreBuild(java.net.http.HttpRequest.Builder requestBuild) throws FailedResponseException {
return requestBuild.GET();
}
protected Builder finalizeClientPreBuild(Builder clientBuild) {
return clientBuild;
}
protected void build(){
boolean AUTH_REQUIRED=user.length()>0&&pass.length()>0; boolean AUTH_REQUIRED=user.length()>0&&pass.length()>0;
try { try {
java.net.http.HttpRequest.Builder requestBuild=HttpRequest.newBuilder(new URI(url)) java.net.http.HttpRequest.Builder requestBuild=HttpRequest.newBuilder(new URI(url))
.version(HttpClient.Version.HTTP_2) .version(HttpClient.Version.HTTP_2)
.timeout(Duration.ofMillis(timeout)) .timeout(Duration.ofMillis(timeout));
.GET();
if (headers!=null&&headers.length>0) { if (headers!=null&&headers.length>0) {
requestBuild.headers(headers); requestBuild.headers(headers);
} }
req = requestBuild.build(); req = finalizeRequestPreBuild(requestBuild).build();
Builder clientBuild=HttpClient.newBuilder() Builder clientBuild=HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.ALWAYS); .followRedirects(HttpClient.Redirect.ALWAYS);
if (AUTH_REQUIRED) { if (AUTH_REQUIRED) {
@ -86,8 +91,8 @@ public class GETRequest{
} }
}); });
} }
client = clientBuild.build(); client = finalizeClientPreBuild(clientBuild).build();
} catch (URISyntaxException e) { } catch (URISyntaxException | FailedResponseException e) {
e.printStackTrace(); e.printStackTrace();
} }
} }

@ -0,0 +1,53 @@
package sig.requests;
import java.io.FileNotFoundException;
import java.net.http.HttpRequest;
import java.net.http.HttpClient.Builder;
import java.nio.file.Path;
import sig.exceptions.FailedResponseException;
public class POSTRequest extends GETRequest{
String body = "";
public POSTRequest(String url, String body, String username, String password, long timeout, Path file, String... headers) {
super(url, username, password, timeout, file, headers);
this.body=body;
}
public POSTRequest(String url, String body, long timeout, Path file, String... headers) {
super(url, timeout, file, headers);
this.body=body;
}
public POSTRequest(String url, String body, long timeout, String... headers) {
super(url, timeout, headers);
this.body=body;
}
public POSTRequest(String url, String body) {
super(url);
this.body=body;
}
@Override
protected java.net.http.HttpRequest.Builder finalizeRequestPreBuild(
java.net.http.HttpRequest.Builder requestBuild) throws FailedResponseException{
requestBuild.headers("Content-Type","application/json");
try {
return file!=null?requestBuild.POST(HttpRequest.BodyPublishers.ofFile(file)):
body.length()>0?requestBuild.POST(HttpRequest.BodyPublishers.ofString(body)):
requestBuild.POST(HttpRequest.BodyPublishers.noBody());
} catch (FileNotFoundException e) {
e.printStackTrace();
}
throw new FailedResponseException("Could not complete request build. THIS SHOULD NOT BE HAPPENING!");
}
@Override
protected Builder finalizeClientPreBuild(Builder clientBuild) {
return clientBuild;
}
}
Loading…
Cancel
Save