Java Code Examples for java.net.http.HttpRequest#Builder
The following examples show how to use
java.net.http.HttpRequest#Builder .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UserApi.java From openapi-generator with Apache License 2.0 | 6 votes |
private HttpRequest.Builder deleteUserRequestBuilder(String username) throws ApiException { // verify the required parameter 'username' is set if (username == null) { throw new ApiException(400, "Missing the required parameter 'username' when calling deleteUser"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/user/{username}" .replace("{username}", ApiClient.urlEncode(username.toString())); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 2
Source File: UserApi.java From openapi-generator with Apache License 2.0 | 6 votes |
/** * Creates list of users with given input array * * @param body List of user object (required) * @throws ApiException if fails to make API call */ public CompletableFuture<Void> createUsersWithArrayInput(List<User> body) throws ApiException { try { HttpRequest.Builder localVarRequestBuilder = createUsersWithArrayInputRequestBuilder(body); return memberVarHttpClient.sendAsync( localVarRequestBuilder.build(), HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> { if (localVarResponse.statusCode()/ 100 != 2) { return CompletableFuture.failedFuture(new ApiException(localVarResponse.statusCode(), "createUsersWithArrayInput call received non-success response", localVarResponse.headers(), localVarResponse.body()) ); } else { return CompletableFuture.completedFuture(null); } }); } catch (ApiException e) { return CompletableFuture.failedFuture(e); } }
Example 3
Source File: StoreApi.java From openapi-generator with Apache License 2.0 | 6 votes |
private HttpRequest.Builder getInventoryRequestBuilder() throws ApiException { HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/store/inventory"; localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 4
Source File: UserApi.java From openapi-generator with Apache License 2.0 | 6 votes |
private HttpRequest.Builder getUserByNameRequestBuilder(String username) throws ApiException { // verify the required parameter 'username' is set if (username == null) { throw new ApiException(400, "Missing the required parameter 'username' when calling getUserByName"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/user/{username}" .replace("{username}", ApiClient.urlEncode(username.toString())); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 5
Source File: PetApi.java From openapi-generator with Apache License 2.0 | 6 votes |
/** * Deletes a pet * * @param petId Pet id to delete (required) * @param apiKey (optional) * @throws ApiException if fails to make API call */ public CompletableFuture<Void> deletePet(Long petId, String apiKey) throws ApiException { try { HttpRequest.Builder localVarRequestBuilder = deletePetRequestBuilder(petId, apiKey); return memberVarHttpClient.sendAsync( localVarRequestBuilder.build(), HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> { if (localVarResponse.statusCode()/ 100 != 2) { return CompletableFuture.failedFuture(new ApiException(localVarResponse.statusCode(), "deletePet call received non-success response", localVarResponse.headers(), localVarResponse.body()) ); } else { return CompletableFuture.completedFuture(null); } }); } catch (ApiException e) { return CompletableFuture.failedFuture(e); } }
Example 6
Source File: JavaRequestProcessor.java From milkman with MIT License | 6 votes |
@SneakyThrows private HttpRequest toHttpRequest(RestRequestContainer request, Templater templater) { HttpRequest.Builder builder = HttpRequest.newBuilder(); builder.uri(new URI(HttpUtil.escapeUrl(request, templater))); for (RequestAspect aspect : request.getAspects()) { if (aspect instanceof RestRequestAspect) { ((RestRequestAspect) aspect).enrichRequest(new JavaRequestBuilder(builder, request.getHttpMethod()), templater); } } if (builder.build().headers().firstValue(USER_AGENT_HEADER).isEmpty()) builder.setHeader(USER_AGENT_HEADER, "Milkman"); if (proxyCredentials != null) { builder.setHeader(PROXY_AUTHORIZATION_HEADER, HttpUtil.authorizationHeaderValue(proxyCredentials)); } return builder.build(); }
Example 7
Source File: FakeApi.java From openapi-generator with Apache License 2.0 | 6 votes |
private HttpRequest.Builder fakeOuterStringSerializeRequestBuilder(String body) throws ApiException { HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/fake/outer/string"; localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body); localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); } catch (IOException e) { throw new ApiException(e); } if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 8
Source File: FakeApi.java From openapi-generator with Apache License 2.0 | 6 votes |
private HttpRequest.Builder fakeOuterBooleanSerializeRequestBuilder(Boolean body) throws ApiException { HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/fake/outer/boolean"; localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body); localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); } catch (IOException e) { throw new ApiException(e); } if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 9
Source File: StoreApi.java From openapi-generator with Apache License 2.0 | 6 votes |
private HttpRequest.Builder getOrderByIdRequestBuilder(Long orderId) throws ApiException { // verify the required parameter 'orderId' is set if (orderId == null) { throw new ApiException(400, "Missing the required parameter 'orderId' when calling getOrderById"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/store/order/{order_id}" .replace("{order_id}", ApiClient.urlEncode(orderId.toString())); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Accept", "application/json"); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 10
Source File: HttpEndpoint.java From vespa with Apache License 2.0 | 5 votes |
@Override public HttpRequest.Builder request(String path, Map<String, String> properties) { return HttpRequest.newBuilder(endpoint.resolve(path + properties.entrySet().stream() .map(entry -> encode(entry.getKey(), UTF_8) + "=" + encode(entry.getValue(), UTF_8)) .collect(Collectors.joining("&", path.contains("?") ? "&" : "?", "")))); }
Example 11
Source File: UserApi.java From openapi-generator with Apache License 2.0 | 5 votes |
private HttpRequest.Builder createUsersWithArrayInputRequestBuilder(List<User> body) throws ApiException { // verify the required parameter 'body' is set if (body == null) { throw new ApiException(400, "Missing the required parameter 'body' when calling createUsersWithArrayInput"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/user/createWithArray"; localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body); localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); } catch (IOException e) { throw new ApiException(e); } if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 12
Source File: StoreApi.java From openapi-generator with Apache License 2.0 | 5 votes |
private HttpRequest.Builder placeOrderRequestBuilder(Order body) throws ApiException { // verify the required parameter 'body' is set if (body == null) { throw new ApiException(400, "Missing the required parameter 'body' when calling placeOrder"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/store/order"; localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body); localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); } catch (IOException e) { throw new ApiException(e); } if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 13
Source File: StoreApi.java From openapi-generator with Apache License 2.0 | 5 votes |
private HttpRequest.Builder placeOrderRequestBuilder(Order body) throws ApiException { // verify the required parameter 'body' is set if (body == null) { throw new ApiException(400, "Missing the required parameter 'body' when calling placeOrder"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/store/order"; localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body); localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); } catch (IOException e) { throw new ApiException(e); } if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 14
Source File: UserApi.java From openapi-generator with Apache License 2.0 | 5 votes |
private HttpRequest.Builder createUserRequestBuilder(User body) throws ApiException { // verify the required parameter 'body' is set if (body == null) { throw new ApiException(400, "Missing the required parameter 'body' when calling createUser"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/user"; localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body); localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); } catch (IOException e) { throw new ApiException(e); } if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 15
Source File: PetApi.java From openapi-generator with Apache License 2.0 | 5 votes |
private HttpRequest.Builder findPetsByStatusRequestBuilder(List<String> status) throws ApiException { // verify the required parameter 'status' is set if (status == null) { throw new ApiException(400, "Missing the required parameter 'status' when calling findPetsByStatus"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/pet/findByStatus"; List<Pair> localVarQueryParams = new ArrayList<>(); localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "status", status)); if (!localVarQueryParams.isEmpty()) { StringJoiner queryJoiner = new StringJoiner("&"); localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue())); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + queryJoiner.toString())); } else { localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); } localVarRequestBuilder.header("Accept", "application/json"); localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 16
Source File: FakeClassnameTags123Api.java From openapi-generator with Apache License 2.0 | 5 votes |
private HttpRequest.Builder testClassnameRequestBuilder(Client body) throws ApiException { // verify the required parameter 'body' is set if (body == null) { throw new ApiException(400, "Missing the required parameter 'body' when calling testClassname"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/fake_classname_test"; localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); localVarRequestBuilder.header("Content-Type", "application/json"); localVarRequestBuilder.header("Accept", "application/json"); try { byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body); localVarRequestBuilder.method("PATCH", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody)); } catch (IOException e) { throw new ApiException(e); } if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }
Example 17
Source File: SignaturesTest.java From vespa with Apache License 2.0 | 4 votes |
@Test void testSigning() { Clock clock = Clock.fixed(Instant.EPOCH, ZoneOffset.UTC); RequestSigner signer = new RequestSigner(ecPemPrivateKey, "myKey", clock); URI requestUri = URI.create("https://host:123/path//./../more%2fpath/?yes=no"); HttpRequest.Builder builder = HttpRequest.newBuilder(requestUri); HttpRequest request = signer.signed(builder, Method.GET, InputStream::nullInputStream); // GET request with correct signature and URI as-is. RequestVerifier verifier = new RequestVerifier(ecPemPublicKey, clock); assertTrue(verifier.verify(Method.valueOf(request.method()), request.uri(), request.headers().firstValue("X-Timestamp").get(), request.headers().firstValue("X-Content-Hash").get(), request.headers().firstValue("X-Authorization").get())); // POST request with correct signature and URI normalized. MultiPartStreamer streamer = new MultiPartStreamer().addText("message", new String(message, UTF_8)) .addBytes("copy", message); request = signer.signed(builder.setHeader("Content-Type", streamer.contentType()), Method.POST, streamer::data); assertTrue(verifier.verify(Method.valueOf(request.method()), request.uri().normalize(), request.headers().firstValue("X-Timestamp").get(), request.headers().firstValue("X-Content-Hash").get(), request.headers().firstValue("X-Authorization").get())); // Wrong method. assertFalse(verifier.verify(Method.PATCH, request.uri().normalize(), request.headers().firstValue("X-Timestamp").get(), request.headers().firstValue("X-Content-Hash").get(), request.headers().firstValue("X-Authorization").get())); // Wrong path. assertFalse(verifier.verify(Method.valueOf(request.method()), request.uri().resolve("asdf"), request.headers().firstValue("X-Timestamp").get(), request.headers().firstValue("X-Content-Hash").get(), request.headers().firstValue("X-Authorization").get())); // Wrong timestamp. assertFalse(verifier.verify(Method.valueOf(request.method()), request.uri().normalize(), Instant.EPOCH.plusMillis(1).toString(), request.headers().firstValue("X-Content-Hash").get(), request.headers().firstValue("X-Authorization").get())); // Wrong content hash. assertFalse(verifier.verify(Method.valueOf(request.method()), request.uri().normalize(), request.headers().firstValue("X-Timestamp").get(), "Wrong/hash", request.headers().firstValue("X-Authorization").get())); // Wrong signature. assertFalse(verifier.verify(Method.valueOf(request.method()), request.uri().normalize(), request.headers().firstValue("X-Timestamp").get(), request.headers().firstValue("X-Content-Hash").get(), "Wrong/signature")); // Key pair mismatch. verifier = new RequestVerifier(otherEcPemPublicKey, clock); assertFalse(verifier.verify(Method.valueOf(request.method()), request.uri().normalize(), request.headers().firstValue("X-Timestamp").get(), request.headers().firstValue("X-Content-Hash").get(), request.headers().firstValue("X-Authorization").get())); // Too old request. verifier = new RequestVerifier(ecPemPublicKey, Clock.fixed(Instant.EPOCH.plusSeconds(301), ZoneOffset.UTC)); assertFalse(verifier.verify(Method.valueOf(request.method()), request.uri().normalize(), request.headers().firstValue("X-Timestamp").get(), request.headers().firstValue("X-Content-Hash").get(), request.headers().firstValue("X-Authorization").get())); // Too new request. verifier = new RequestVerifier(ecPemPublicKey, Clock.fixed(Instant.EPOCH.minusSeconds(301), ZoneOffset.UTC)); assertFalse(verifier.verify(Method.valueOf(request.method()), request.uri().normalize(), request.headers().firstValue("X-Timestamp").get(), request.headers().firstValue("X-Content-Hash").get(), request.headers().firstValue("X-Authorization").get())); }
Example 18
Source File: RestClient.java From r2cloud with Apache License 2.0 | 4 votes |
private HttpRequest.Builder createDefaultRequest(String path) { return HttpRequest.newBuilder().uri(URI.create(baseUrl + path)).timeout(Duration.ofMinutes(1L)).header("User-Agent", "r2cloud/0.1 [email protected]"); }
Example 19
Source File: Endpoint.java From vespa with Apache License 2.0 | 4 votes |
/** Creates a request against the endpoint, with the given path and properties. */ HttpRequest.Builder request(String path, Map<String, String> properties);
Example 20
Source File: FakeApi.java From openapi-generator with Apache License 2.0 | 4 votes |
private HttpRequest.Builder testQueryParameterCollectionFormatRequestBuilder(List<String> pipe, List<String> ioutil, List<String> http, List<String> url, List<String> context) throws ApiException { // verify the required parameter 'pipe' is set if (pipe == null) { throw new ApiException(400, "Missing the required parameter 'pipe' when calling testQueryParameterCollectionFormat"); } // verify the required parameter 'ioutil' is set if (ioutil == null) { throw new ApiException(400, "Missing the required parameter 'ioutil' when calling testQueryParameterCollectionFormat"); } // verify the required parameter 'http' is set if (http == null) { throw new ApiException(400, "Missing the required parameter 'http' when calling testQueryParameterCollectionFormat"); } // verify the required parameter 'url' is set if (url == null) { throw new ApiException(400, "Missing the required parameter 'url' when calling testQueryParameterCollectionFormat"); } // verify the required parameter 'context' is set if (context == null) { throw new ApiException(400, "Missing the required parameter 'context' when calling testQueryParameterCollectionFormat"); } HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder(); String localVarPath = "/fake/test-query-paramters"; List<Pair> localVarQueryParams = new ArrayList<>(); localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "pipe", pipe)); localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "ioutil", ioutil)); localVarQueryParams.addAll(ApiClient.parameterToPairs("ssv", "http", http)); localVarQueryParams.addAll(ApiClient.parameterToPairs("csv", "url", url)); localVarQueryParams.addAll(ApiClient.parameterToPairs("multi", "context", context)); if (!localVarQueryParams.isEmpty()) { StringJoiner queryJoiner = new StringJoiner("&"); localVarQueryParams.forEach(p -> queryJoiner.add(p.getName() + '=' + p.getValue())); localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath + '?' + queryJoiner.toString())); } else { localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath)); } localVarRequestBuilder.header("Accept", "application/json"); localVarRequestBuilder.method("PUT", HttpRequest.BodyPublishers.noBody()); if (memberVarReadTimeout != null) { localVarRequestBuilder.timeout(memberVarReadTimeout); } if (memberVarInterceptor != null) { memberVarInterceptor.accept(localVarRequestBuilder); } return localVarRequestBuilder; }