Class BraveNewsQuery
- All Implemented Interfaces:
BraveQueryBuilder<BraveNewsQuery,NewsSearchApiResponse>
BraveNewsQuery class provides a builder for building Brave News API queries.
This class is not intended to be instantiated directly, instead use the builder()
method to create a new instance of the builder.
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
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 BraveNewsQuerybuilder()Creates a new instance ofBraveNewsQuerycount(int count) Adds the count option to the URL query.enableOperators(boolean enableOperators) Adds the operators option to the URL query.execute()Executes the request and returns response of string.extraSnippets(boolean extraSnippets) Adds the extra_snippets option to the URL query.Adds a freshness option to the URL query using an existingFreshnessinstance.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>
BraveNewsQuerylanguage(T searchLanguage) Adds the search language option to the URL query.<T extends RegionLocaleIdentifier>
BraveNewsQuerymarket(T uiLanguage) 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 ofBraveNewsQueryby resetting its state to its initial values.safeSearch(@NotNull SafeSearch safeSearch) Adds a safe search option to the URL query.spellcheck(boolean spellcheck) Adds the spell check option to the URL query.Converts the current query to anHttpRequestinstance.withHeaders(@NotNull Consumer<BraveNewsQuery.BraveRequestBuilder> headers) Sets the request headers using the provided consumer.withRetries(int maxRetries) Sets the maximum number of retry attempts for rate-limited (HTTP 429) responses.withSearchOperators(@NotNull Consumer<SearchOperatorBuilder> operators) Adds search operations to the query term for advanced result filtering.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 ofBraveNewsQuery- Returns:
- a new instance of
BraveNewsQuery
-
query
Adds a search term to the URL query. Maximum of 400 characters and 50 words.If a query is already present, subsequent calls are ignored. Only one query term is supported per search request.
- Parameters:
queryTerm- the search term to add- Returns:
- the current instance of the builder
-
language
Adds the search language option to the URL query.- Parameters:
searchLanguage- the search language to set- Returns:
- the current instance of the builder
- See Also:
-
market
Adds the market option to the URL query.- Parameters:
uiLanguage- the market locale to set- Returns:
- the current instance of the builder
- 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 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
-
spellcheck
Adds the spell check option to the URL query.- Parameters:
spellcheck- whether to enable spellcheck- Returns:
- the current instance of the builder
-
freshness
Adds a freshness option to the URL query using an existingFreshnessinstance.- Parameters:
startDate- the start date of the freshness constraintendDate- the end date of the freshness constraint- Returns:
- the current instance of the builder
- See Also:
-
freshness
Adds a freshness option to the URL query using an existingFreshnessinstance.- Parameters:
freshness- a non-nullFreshnessdescribing the freshness constraint- Returns:
- the current instance of
BraveNewsQuery - See Also:
-
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
-
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
BraveNewsQuery
-
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
-
enableOperators
Adds the operators option to the URL query.This option tells the API to read search operators from the query term.
- Parameters:
enableOperators- Whether to include operators in the results.- Returns:
- The current instance of the builder.
-
withSearchOperators
@Contract("_ -> this") public BraveNewsQuery withSearchOperators(@NotNull @NotNull Consumer<SearchOperatorBuilder> operators) Adds search operations to the query term for advanced result filtering.Make sure to include the
enableOperators(boolean)method to tell the API that you will be including operators in your query.- 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.
-
withToken
Sets the subscription token header using the providedClientInfoinstance.- Parameters:
clientInfo- theClientInfoinstance containing the subscription token- Returns:
- the current instance of
BraveNewsQuery
-
withHeaders
@Contract("_ -> this") public BraveNewsQuery withHeaders(@NotNull @NotNull Consumer<BraveNewsQuery.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
BraveNewsQuery
-
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
BraveNewsQuery
-
execute
Executes the request and returns response of string.- Specified by:
executein interfaceBraveQueryBuilder<BraveNewsQuery,NewsSearchApiResponse> - Returns:
- an optional response to the request
-
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<BraveNewsQuery,NewsSearchApiResponse> - Returns:
trueif the request has been executed,falseotherwise
-
toHttpRequest
Converts the current query to anHttpRequestinstance.- Specified by:
toHttpRequestin interfaceBraveQueryBuilder<BraveNewsQuery,NewsSearchApiResponse> - 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<BraveNewsQuery,NewsSearchApiResponse> - 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<BraveNewsQuery,NewsSearchApiResponse> - Returns:
- the class of the API response
-
reset
Clears the current instance ofBraveNewsQueryby resetting its state to its initial values.- Specified by:
resetin interfaceBraveQueryBuilder<BraveNewsQuery,NewsSearchApiResponse> - Returns:
- the current instance of
BraveNewsQuery
-