Interface BraveQueryBuilder<T,E extends ApiResponse>
- Type Parameters:
T- the type of the builderE- the type of the API response
- All Known Implementing Classes:
BraveImageQuery,BraveNewsQuery,BraveSpellcheckQuery,BraveSuggestQuery,BraveVideoQuery,BraveWebQuery
-
Method Summary
Modifier and TypeMethodDescriptionexecute()Executes the request and stores the response.default Optional<ApiResponse> Converts the current response to a POJO instance ofWebSearchApiResponseif the status code of the response is 200.default Optional<ErrorResponse> Converts the current response to a POJO instance ofErrorResponseif the status code of the response is not 200.Returns the current HTTP response as anOptional.getPOJO()Converts the current response to a POJO instance ofBraveQueryBuilderif the status code of the response is 200.default Optional<XRateLimitPolicy> Returns the rate limit policy from the HTTP response.default Optional<XRateLimitRemaining> Returns the rate limit remaining from the HTTP response.default Optional<XRateLimit> Returns the rate limit from the HTTP response.Returns the type of the API response.booleanChecks if the request has been executed.reset()Clears the builder by resetting it to its initial state.Converts the current query to anHttpRequestinstance.toURI()Converts the URL query to a URI.
-
Method Details
-
getRateLimits
Returns the rate limit from the HTTP response.- Returns:
- An optional containing the rate limit extracted from the HTTP response.
-
getRateLimitPolicy
Returns the rate limit policy from the HTTP response.- Returns:
- An optional containing the rate limit policy extracted from the HTTP response.
-
getRateLimitRemaining
Returns the rate limit remaining from the HTTP response.- Returns:
- An optional containing the rate limit remaining extracted from the HTTP response.
-
execute
T execute()Executes the request and stores the response.This method is typically used to decouple request execution from response extraction.
You can extract the response later with
getHttpResponse(),getPOJO(), orgetErrorPOJO().Note: This builder stores the response object internally and repeated calls to extraction methods will not trigger multiple requests; however,
execute()will send a new request per method call.If the client added retries to the building chain,
execute()will activate retry logic under the hood.By default, rate-limited (HTTP 429) responses are retried once after sleeping the server-provided rate limit reset window. Use
withRetries(int)to change the policy; values less than or equal to zero retain the default (one retry).- Returns:
- the current builder instance
-
hasExecuted
boolean hasExecuted()Checks if the request has been executed. Precisely, it will tell the caller whether the builder has an existing (complete) request in it or not.- Returns:
trueif the request has been executed,falseotherwise
-
toHttpRequest
HttpRequest toHttpRequest()Converts the current query to anHttpRequestinstance.- Returns:
- the
HttpRequestinstance representing the current query
-
getResponseType
Returns the type of the API response. The class returned by this method is the class of the concrete implementation ofApiResponsethat this builder is expected to produce as a result of its query.- Returns:
- the class of the API response
-
getPOJO
Converts the current response to a POJO instance ofBraveQueryBuilderif the status code of the response is 200.If you get an empty optional with this method, it is possible that the request failed with an error code. Use
getErrorPOJO()to get the error response object and use it in any error handling strategy. If you want to inspect the rawHttpResponseobject, you can always usegetHttpResponse().Note: Calling this method will execute the request and deserialize it. Once a request has been executed, this builder stores it in an internal field, so you can call this method and the ones mentioned above without having to send another request to the API.
- Returns:
- an
Optionalcontaining the current response as a POJO instance ofBraveQueryBuilder, or an emptyOptionalif the response status code is not 200
-
getErrorPOJO
Converts the current response to a POJO instance ofErrorResponseif the status code of the response is not 200.If you get an empty optional with this method, it is possible that the request was successful. Use
getPOJO()to get the response object and use it in any success handling strategy. If you want to inspect the rawHttpResponseobject, you can always usegetHttpResponse().Calling this method will execute the request and deserialize it. Once a request has been executed, this builder stores it in an internal field, so you can call this method and the ones mentioned above without having to send another request to the API.
- Returns:
- an
Optionalcontaining the current response as a POJO instance ofErrorResponse, or an emptyOptionalif the response status code is 200
-
getEitherPOJO
Converts the current response to a POJO instance ofWebSearchApiResponseif the status code of the response is 200. If the status code is not 200, it attempts to deserialize the response body into anErrorResponseobject. This is particularly useful if you want to check the success of your request with aninstanceofcheck and then handle it as you like depending on whatApiResponseimplementation you get.Calling this method will execute the request and deserialize it. Once a request has been executed, this builder stores it in an internal field, so you can call this method and the ones mentioned above without having to send another request to the API.
- Returns:
- an
Optionalcontaining the current response as a POJO instance of eitherWebSearchApiResponseorErrorResponse, or an emptyOptionalif the response status code is neither 200 nor any other expected error code
-
getHttpResponse
Optional<HttpResponse<String>> getHttpResponse()Returns the current HTTP response as anOptional.If the request has not been executed, this method will execute the request and return the
HttpResponse.- Returns:
- an
Optionalcontaining the current HTTP response.
-
toURI
URI toURI()Converts the URL query to a URI.- Returns:
- The URI representation of the URL query.
-
reset
T reset()Clears the builder by resetting it to its initial state.This method is used to reset the builder to its initial state before adding any options.
- Returns:
- The current instance of the builder.
-