Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
4 changed files
with
78 additions
and
40 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
@@ -0,0 +1,38 @@ | ||
import java.net.http.HttpClient | ||
import java.net.http.HttpRequest | ||
import java.net.http.HttpResponse | ||
import java.time.Duration | ||
|
||
class HttpUtil { | ||
private HttpUtil() {} | ||
|
||
static HttpClient newClient() { | ||
HttpClient.newBuilder() | ||
.version(HttpClient.Version.HTTP_1_1) | ||
.connectTimeout(Duration.ofSeconds(10)) | ||
.build() | ||
|
||
} | ||
|
||
static HttpRequest getRequest(url, user, password) { | ||
HttpRequest.newBuilder() | ||
.uri(new URI(url)) | ||
.header('Authorization', 'Basic ' + "$user:$password".getBytes('iso-8859-1').encodeBase64()) | ||
.GET() | ||
.build() | ||
} | ||
|
||
static HttpRequest putRequest(url, json, user, password) { | ||
def body = HttpRequest.BodyPublishers.ofString(json) | ||
HttpRequest.newBuilder() | ||
.uri(new URI(url)) | ||
.header('Authorization', 'Basic ' + "$user:$password".getBytes('iso-8859-1').encodeBase64()) | ||
.header('Content-Type', 'application/json') | ||
.PUT(body) | ||
.build() | ||
} | ||
|
||
static WrappedResponse send(HttpClient client, HttpRequest request) { | ||
new WrappedResponse(client.send(request, HttpResponse.BodyHandlers.ofString())) | ||
} | ||
} |
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
@@ -0,0 +1,14 @@ | ||
import java.net.http.HttpResponse | ||
import groovy.json.JsonSlurper | ||
|
||
class WrappedResponse { | ||
@Delegate HttpResponse response | ||
|
||
WrappedResponse(HttpResponse response) { | ||
this.response = response | ||
} | ||
|
||
def getJson() { | ||
new JsonSlurper().parseText(response.body()) | ||
} | ||
} |
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