diff --git a/sig/client.java b/sig/client.java new file mode 100644 index 0000000..1a76ab9 --- /dev/null +++ b/sig/client.java @@ -0,0 +1,20 @@ +package sig; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; + +import sig.exceptions.FailedResponseException; +import sig.requests.GETRequest; +public class client { + public static void main(String[] args) { + try { + GETRequest res = new GETRequest("https://postman-echo.com/get"); + System.out.println(((HttpResponse)res.run()).body()); + } catch (FailedResponseException e) { + e.printStackTrace(); + } + } +} diff --git a/sig/exceptions/FailedResponseException.java b/sig/exceptions/FailedResponseException.java new file mode 100644 index 0000000..768fdec --- /dev/null +++ b/sig/exceptions/FailedResponseException.java @@ -0,0 +1,7 @@ +package sig.exceptions; + +public class FailedResponseException extends Exception{ + public FailedResponseException(String msg) { + super(msg); + } +} diff --git a/sig/requests/GETRequest.java b/sig/requests/GETRequest.java new file mode 100644 index 0000000..a52b7ff --- /dev/null +++ b/sig/requests/GETRequest.java @@ -0,0 +1,67 @@ +package sig.requests; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; +import java.net.http.HttpResponse.BodyHandlers; +import java.nio.file.Path; +import java.time.Duration; + +import sig.exceptions.FailedResponseException; + +public class GETRequest{ + String url; + String[] headers; + long timeout; + Path file; + private HttpRequest req; + private HttpClient client; + /** + * @param file The file path info, use this for file downloads or set to null for standard text. + * @param timeout in milliseconds + * */ + public GETRequest(String url, long timeout, Path file, String...headers){ + this.url = url; + this.headers = headers; + this.timeout = timeout; + this.file=file; + build(); + } + /** + * @param timeout in milliseconds + * */ + public GETRequest(String url, long timeout, String...headers){ + this(url,timeout,null,headers); + } + public GETRequest(String url){ + this(url,30000,"default","default"); + } + public HttpResponse run() throws FailedResponseException { + try { + if (file==null) { + return client.send(req,BodyHandlers.ofString()); + } else { + return client.send(req,BodyHandlers.ofFile(file)); + } + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + throw new FailedResponseException("No proper response returned. THIS SHOULD NOT BE HAPPENING!"); + } + private void build(){ + try { + req = HttpRequest.newBuilder(new URI(url)) + .version(HttpClient.Version.HTTP_2) + .headers(headers) + .timeout(Duration.ofMillis(timeout)) + .GET().build(); + client = HttpClient.newBuilder().build(); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file