Class BraveWebQuery
- All Implemented Interfaces:
BraveQueryBuilder<BraveWebQuery,WebSearchApiResponse>
This class provides a fluent API for constructing Brave Web search queries. The builder allows clients to specify various options such as the search term, the number of results to return, the offset, the units in which to display the results, and more.
The builder is not thread-safe and not intended to be instantiated directly, instead use the
builder() method to create a new instance of the builder. Also note that this builder is
a stateful, reusable builder intended for single-threaded use.
Method reset() will clear the internal state of the builder and must be called before
reusing.
-
Nested Class Summary
Nested Classes -
Field Summary
Fields inherited from class net.ygbstudio.jbrave.core.builders.AbstractQueryUrlBuilder
delayedOperators, optionTracker, QUERY_PROMPT, urlEnd, urlStart -
Method Summary
Modifier and TypeMethodDescriptionstatic @NotNull BraveWebQuerybuilder()Creates a new instance ofBraveWebQuerywith an empty URL query.count(int count) Adds the count option to the URL query.<T extends CountryIdentifier>
BraveWebQuerycountry(T country) Adds the country option to the URL query.Adds the operators option to the URL query.enableRichCallback(boolean enableRichCallback) Adds the enable_rich_callback option to the URL query.execute()Executes the request and stores the response.extraSnippets(boolean extraSnippets) Adds the extra_snippets option to the URL query.Adds a freshness option to the URL query representing a date range.Adds a freshness option to the URL query using an existingFreshnessinstance.@NotNull Optional<HttpResponse<String>> Returns the current HTTP response as anOptional.Returns the type of the API response.Adds a goggles URL option to the URL query.booleanChecks if the request has been executed.includeFetchMetadata(boolean includeFetchMetadata) Adds the include_fetch_metadata option to the URL query.<T extends LanguageIdentifier>
BraveWebQuerylanguage(T languageIdentifier) Adds the language option to the URL query.<T extends RegionLocaleIdentifier>
BraveWebQuerymarket(T regionLocale) Adds the market option to the URL query.offset(int offset) Adds the offset option to the URL query.Adds a search term to the URL query.reset()Clears the current instance ofBraveWebQueryby resetting its state to its initial values.resultFilters(Set<ResultFilter> resultFilterList) Adds result filters to the URL query.safeSearch(@NotNull SafeSearch safeSearch) Adds a safe search option to the URL query.spellCheck(boolean spellCheck) Adds the spellcheck option to the URL query.summary(boolean summary) Adds the summary option to the URL query.textDecorations(boolean textDecorations) Adds the text_decorations option to the URL query.Converts the current query to anHttpRequestinstance.Adds the units option to the URL query.withHeaders(@NotNull Consumer<BraveWebQuery.BraveRequestBuilder> headers) Sets the request headers using the provided consumer.withOperators(@NotNull Consumer<SearchOperatorBuilder> operators) Adds search operations to the query term for advanced result filtering.withRetries(int maxRetries) Sets the maximum number of retry attempts for rate-limited (HTTP 429) responses.withToken(@NotNull ClientInfo clientInfo) Sets the subscription token header using the providedClientInfoinstance.Methods inherited from class net.ygbstudio.jbrave.core.builders.AbstractQueryUrlBuilder
addInstanceVertical, addOption, addOptionCarrier, addQueryTerm, build, clear, isValidQuery, optionMissing, queryMissing, self, toURIMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface net.ygbstudio.jbrave.api.builders.BraveQueryBuilder
getEitherPOJO, getErrorPOJO, getPOJO, getRateLimitPolicy, getRateLimitRemaining, getRateLimits, toURI
-
Method Details
-
builder
Creates a new instance ofBraveWebQuerywith an empty URL query.- Returns:
- A new instance of
BraveWebQuery.
-
query
Adds a search term to the URL query.- Parameters:
queryTerm- The search term to add.- Returns:
- The current instance of the builder.
-
count
Adds the count option to the URL query.- Parameters:
count- The number of results to return.- Returns:
- The current instance of the builder.
-
offset
Adds the offset option to the URL query.- Parameters:
offset- The number of results to skip.- Returns:
- The current instance of the builder.
-
textDecorations
Adds the text_decorations option to the URL query.- Parameters:
textDecorations- Whether to include text decorations in the results.- Returns:
- The current instance of the builder.
-
spellCheck
Adds the spellcheck option to the URL query.- Parameters:
spellCheck- Whether to enable spellcheck.- Returns:
- The current instance of the builder.
-
extraSnippets
Adds the extra_snippets option to the URL query.- Parameters:
extraSnippets- Whether to include extra snippets in the results.- Returns:
- The current instance of the builder.
-
summary
Adds the summary option to the URL query.- Parameters:
summary- Whether to include a summary in the results.- Returns:
- The current instance of the builder.
-
enableOperators
Adds the operators option to the URL query.This option tells the API to read search operators from the query term.
- Returns:
- The current instance of the builder.
-
withOperators
@Contract("_ -> this") public BraveWebQuery withOperators(@NotNull @NotNull Consumer<SearchOperatorBuilder> operators) Adds search operations to the query term for advanced result filtering.- Parameters:
operators- A consumer that accepts aSearchOperatorBuilderinstance and populates it with operators. The builtSearchOperatorBuilderinstance will be used to construct the operators string.- Returns:
- The current instance of the builder.
-
country
Adds the country option to the URL query.- Parameters:
country- The country identifier to set.- Returns:
- The current instance of the builder.
- See Also:
-
market
Adds the market option to the URL query.- Parameters:
regionLocale- The region locale identifier to set.- Returns:
- The current instance of the builder.
- See Also:
-
language
Adds the language option to the URL query.- Parameters:
languageIdentifier- The language identifier to set.- Returns:
- The current instance of the builder.
- See Also:
-
enableRichCallback
Adds the enable_rich_callback option to the URL query.- Parameters:
enableRichCallback- Whether to enable rich callback.- Returns:
- The current instance of the builder.
-
units
Adds the units option to the URL query.- Parameters:
units- The units to set.- Returns:
- The current instance of the builder.
- See Also:
-
freshness
Adds a freshness option to the URL query using an existingFreshnessinstance.- Parameters:
freshness- aFreshnessdescribing the freshness constraint- Returns:
- the current instance of
BraveWebQuery - See Also:
-
freshness
Adds a freshness option to the URL query representing a date range.- Parameters:
startDate- the start date of the freshness rangeendDate- the end date of the freshness range- Returns:
- the current instance of
BraveWebQuery - See Also:
-
safeSearch
Adds a safe search option to the URL query.- Parameters:
safeSearch- aSafeSearchenum value specifying the safe search level- Returns:
- the current instance of
BraveWebQuery - See Also:
-
resultFilters
Adds result filters to the URL query.- Parameters:
resultFilterList- a set ofResultFilterelements to apply; may be empty but not null- Returns:
- the current instance of
BraveWebQuery - See Also:
-
goggles
Adds a goggles URL option to the URL query.- Parameters:
gogglesUri- theURIpointing to a goggles resource to use for the query- Returns:
- the current instance of
BraveWebQuery
-
includeFetchMetadata
Adds the include_fetch_metadata option to the URL query.- Parameters:
includeFetchMetadata- Whether to include fetch metadata in the results.- Returns:
- the current instance of the builder
-
withToken
Sets the subscription token header using the providedClientInfoinstance.- Parameters:
clientInfo- theClientInfoinstance containing the subscription token- Returns:
- the current instance of
BraveWebQuery
-
withHeaders
@Contract("_ -> this") public BraveWebQuery withHeaders(@NotNull @NotNull Consumer<BraveWebQuery.BraveRequestBuilder> headers) Sets the request headers using the provided consumer.- Parameters:
headers- a consumer that accepts an inner request builder instance and applies preconfigured headers to it via helper methods.- Returns:
- the current instance of
BraveWebQuery
-
withRetries
Sets the maximum number of retry attempts for rate-limited (HTTP 429) responses.Values less than or equal to zero fall back to the default retry policy (one retry). Retries sleep for the server-provided rate limit reset window before each attempt.
- Parameters:
maxRetries- the maximum number of retry attempts for rate-limited responses- Returns:
- the current instance of
BraveWebQuery
-
execute
Description copied from interface:BraveQueryBuilderExecutes 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
BraveQueryBuilder.getHttpResponse(),BraveQueryBuilder.getPOJO(), orBraveQueryBuilder.getErrorPOJO().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).- Specified by:
executein interfaceBraveQueryBuilder<BraveWebQuery,WebSearchApiResponse> - Returns:
- the current builder instance
-
hasExecuted
public boolean hasExecuted()Description copied from interface:BraveQueryBuilderChecks if the request has been executed. Precisely, it will tell the caller whether the builder has an existing (complete) request in it or not.- Specified by:
hasExecutedin interfaceBraveQueryBuilder<BraveWebQuery,WebSearchApiResponse> - Returns:
trueif the request has been executed,falseotherwise
-
toHttpRequest
Description copied from interface:BraveQueryBuilderConverts the current query to anHttpRequestinstance.- Specified by:
toHttpRequestin interfaceBraveQueryBuilder<BraveWebQuery,WebSearchApiResponse> - Returns:
- the
HttpRequestinstance representing the current query
-
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.- Specified by:
getHttpResponsein interfaceBraveQueryBuilder<BraveWebQuery,WebSearchApiResponse> - Returns:
- an
Optionalcontaining the current HTTP response.
-
getResponseType
Description copied from interface:BraveQueryBuilderReturns 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.- Specified by:
getResponseTypein interfaceBraveQueryBuilder<BraveWebQuery,WebSearchApiResponse> - Returns:
- the class of the API response
-
reset
Clears the current instance ofBraveWebQueryby resetting its state to its initial values.- Specified by:
resetin interfaceBraveQueryBuilder<BraveWebQuery,WebSearchApiResponse> - Returns:
- the current instance of
BraveWebQuery
-