-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
140 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
package de.malkusch.km200.http; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.net.SocketTimeoutException; | ||
import java.net.URL; | ||
import java.net.http.HttpTimeoutException; | ||
import java.time.Duration; | ||
|
||
import de.malkusch.km200.KM200Exception; | ||
import de.malkusch.km200.http.Http.Request.Post; | ||
|
||
public final class UrlHttp extends Http { | ||
|
||
private final String uri; | ||
private final String userAgent; | ||
private final int timeoutMillis; | ||
|
||
public UrlHttp(String uri, String userAgent, Duration timeout) { | ||
this.uri = uri; | ||
this.userAgent = userAgent; | ||
this.timeoutMillis = (int) timeout.toMillis(); | ||
} | ||
|
||
@Override | ||
protected Response exchange(Request request) throws IOException, InterruptedException, KM200Exception { | ||
var connection = (HttpURLConnection) new URL(uri + request.path()).openConnection(); | ||
connection.setConnectTimeout(timeoutMillis); | ||
connection.setReadTimeout(timeoutMillis); | ||
connection.setRequestProperty("User-Agent", userAgent); | ||
|
||
if (request instanceof Post) { | ||
connection.setDoOutput(true); | ||
connection.setRequestProperty("Accept", "application/json"); | ||
} | ||
|
||
try { | ||
connection.connect(); | ||
|
||
if (request instanceof Post post) { | ||
try (var output = connection.getOutputStream()) { | ||
output.write(post.body()); | ||
} | ||
} | ||
|
||
var status = connection.getResponseCode(); | ||
if (status == -1) { | ||
throw new IOException(request + " received invalid HTTP response"); | ||
} | ||
|
||
try (var input = connection.getErrorStream() != null ? connection.getErrorStream() | ||
: connection.getInputStream()) { | ||
|
||
var body = input.readAllBytes(); | ||
var response = new Response(status, body); | ||
return assertHttpOk(request, response); | ||
} | ||
|
||
} catch (SocketTimeoutException e) { | ||
throw new HttpTimeoutException(request + " timed out"); | ||
|
||
} finally { | ||
connection.disconnect(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters