Add POST method extension.
This commit is contained in:
parent
dfe1e58dce
commit
f3ff26fa01
@ -1,34 +1,49 @@
|
||||
package sig;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import sig.exceptions.FailedResponseException;
|
||||
import sig.requests.GETRequest;
|
||||
import sig.requests.POSTRequest;
|
||||
|
||||
public class client {
|
||||
public static void main(String[] args) {
|
||||
try {
|
||||
|
||||
|
||||
//Regular get request:
|
||||
GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
|
||||
System.out.println(((HttpResponse<String>)res.run()).body());
|
||||
|
||||
//Download to file:
|
||||
//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 Block
|
||||
|
||||
//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());
|
||||
//Regular get request:
|
||||
GETRequest res = new GETRequest("https://postman-echo.com/get?foo1=bar1&foo2=bar2");
|
||||
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());
|
||||
//Download to file:
|
||||
//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:
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -35,7 +35,6 @@ public class GETRequest{
|
||||
this.headers = headers;
|
||||
this.timeout = timeout;
|
||||
this.file=file;
|
||||
build();
|
||||
}
|
||||
/**
|
||||
* @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);
|
||||
}
|
||||
public HttpResponse<?> run() throws FailedResponseException {
|
||||
build();
|
||||
try {
|
||||
if (file==null) {
|
||||
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!");
|
||||
}
|
||||
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;
|
||||
try {
|
||||
java.net.http.HttpRequest.Builder requestBuild=HttpRequest.newBuilder(new URI(url))
|
||||
.version(HttpClient.Version.HTTP_2)
|
||||
.timeout(Duration.ofMillis(timeout))
|
||||
.GET();
|
||||
.timeout(Duration.ofMillis(timeout));
|
||||
if (headers!=null&&headers.length>0) {
|
||||
requestBuild.headers(headers);
|
||||
}
|
||||
req = requestBuild.build();
|
||||
req = finalizeRequestPreBuild(requestBuild).build();
|
||||
Builder clientBuild=HttpClient.newBuilder()
|
||||
.followRedirects(HttpClient.Redirect.ALWAYS);
|
||||
if (AUTH_REQUIRED) {
|
||||
@ -86,8 +91,8 @@ public class GETRequest{
|
||||
}
|
||||
});
|
||||
}
|
||||
client = clientBuild.build();
|
||||
} catch (URISyntaxException e) {
|
||||
client = finalizeClientPreBuild(clientBuild).build();
|
||||
} catch (URISyntaxException | FailedResponseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
53
sig/requests/POSTRequest.java
Normal file
53
sig/requests/POSTRequest.java
Normal file
@ -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…
x
Reference in New Issue
Block a user