Refactored into GETRequest usable object
This commit is contained in:
		
							parent
							
								
									574a403f63
								
							
						
					
					
						commit
						1afe6eb0ba
					
				
							
								
								
									
										20
									
								
								sig/client.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								sig/client.java
									
									
									
									
									
										Normal file
									
								
							| @ -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<String>)res.run()).body()); | ||||
|         } catch (FailedResponseException e) { | ||||
|             e.printStackTrace(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										7
									
								
								sig/exceptions/FailedResponseException.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								sig/exceptions/FailedResponseException.java
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,7 @@ | ||||
| package sig.exceptions; | ||||
| 
 | ||||
| public class FailedResponseException extends Exception{ | ||||
|     public FailedResponseException(String msg) { | ||||
|         super(msg); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										67
									
								
								sig/requests/GETRequest.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										67
									
								
								sig/requests/GETRequest.java
									
									
									
									
									
										Normal file
									
								
							| @ -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(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user