java.net.http.HttpRequest.BodyPublisher Java Examples
The following examples show how to use
java.net.http.HttpRequest.BodyPublisher.
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: JavaNetHttpRequester.java From algoliasearch-client-java-2 with MIT License | -1 votes |
/** * Builds an http request from an AlgoliaRequest object * * @param algoliaRequest The Algolia request object */ private java.net.http.HttpRequest buildRequest(@Nonnull HttpRequest algoliaRequest) { java.net.http.HttpRequest.Builder builder = java.net.http.HttpRequest.newBuilder(); buildHeaders(builder, algoliaRequest.getHeaders()); buildURI(builder, algoliaRequest.getUri()); builder.timeout(Duration.ofMillis(algoliaRequest.getTimeout())); BodyPublisher body = buildRequestBody(builder, algoliaRequest); builder.method(algoliaRequest.getMethod().toString(), body); return builder.build(); }
Example #2
Source File: JavaNetHttpRequester.java From algoliasearch-client-java-2 with MIT License | -1 votes |
/** * Build the body for the request builder. Handling compression type of the request. * * @param builder Request Builder * @param algoliaRequest HttpClient agnostic Algolia's request */ private BodyPublisher buildRequestBody( @Nonnull Builder builder, @Nonnull HttpRequest algoliaRequest) { if (algoliaRequest.getBody() == null) { return java.net.http.HttpRequest.BodyPublishers.noBody(); } if (algoliaRequest.canCompress()) { builder.header(Defaults.CONTENT_ENCODING_HEADER, Defaults.CONTENT_ENCODING_GZIP); } else { builder.header(Defaults.CONTENT_TYPE_HEADER, Defaults.APPLICATION_JSON); } return BodyPublishers.ofInputStream(algoliaRequest::getBody); }
Example #3
Source File: AbstractRequester.java From catnip with BSD 3-Clause "New" or "Revised" License | -2 votes |
protected void executeHttpRequest(@Nonnull final Route route, @Nullable final BodyPublisher body, @Nonnull final QueuedRequest request, @Nonnull final String mediaType) { final HttpRequest.Builder builder; final String apiHostVersion = catnip.options().apiHost() + "/api/v" + catnip.options().apiVersion(); if(route.method() == GET) { // No body builder = HttpRequest.newBuilder(URI.create(apiHostVersion + route.baseRoute())).GET(); } else { final var fakeBody = request.request.emptyBody(); builder = HttpRequest.newBuilder(URI.create(apiHostVersion + route.baseRoute())) .setHeader("Content-Type", mediaType) .method(route.method().name(), fakeBody ? BodyPublishers.ofString(" ") : body); if(fakeBody) { // If we don't have a body, then the body param is null, which // seems to not set a Content-Length. This explicitly tries to set // up a request shaped in a way that makes Discord not complain. catnip.logAdapter().trace("Set fake body due to lack of body."); } } // Required by Discord builder.setHeader("User-Agent", "DiscordBot (https://github.com/mewna/catnip, " + CatnipMeta.VERSION + ')'); // Request more precise ratelimit headers for better timing // NOTE: THIS SHOULD NOT BE CONFIGURABLE BY THE END USER // This is pretty important for getting timing of things like reaction // routes correct, so there's no use for the end-user messing around // with this. // If they REALLY want it, they can write their own requester. builder.setHeader("X-RateLimit-Precision", "millisecond"); if(request.request().needsToken()) { builder.setHeader("Authorization", "Bot " + catnip.options().token()); } if(request.request().reason() != null) { catnip.logAdapter().trace("Adding reason header due to specific needs."); builder.header(Requester.REASON_HEADER, Utils.encodeUTF8(request.request().reason())); } // Update request start time as soon as possible // See QueuedRequest docs for why we do this request.start = System.nanoTime(); catnip.options().httpClient().sendAsync(builder.build(), BodyHandlers.ofString()) .thenAccept(res -> { final int code = res.statusCode(); final String message = "Unavailable to due Java's HTTP client."; final long requestEnd = System.nanoTime(); catnip.rxScheduler().scheduleDirect(() -> handleResponse(route, code, message, requestEnd, res.body(), res.headers(), request)); }) .exceptionally(e -> { request.bucket.failedRequest(request, e); return null; }); }
Example #4
Source File: MultipartBodyPublisher.java From catnip with BSD 3-Clause "New" or "Revised" License | -2 votes |
public BodyPublisher build() { if(partsSpecificationList.isEmpty()) { throw new IllegalStateException("Must have at least one part to build multipart message."); } addFinalBoundary(); return BodyPublishers.ofByteArrays(PartsIterator::new); }
Example #5
Source File: ReactivePost.java From demo-java-x with MIT License | -2 votes |
private LoggingBodyPublisher(BodyPublisher publisher) { this.publisher = publisher; }