Class BraveImageQuery

All Implemented Interfaces:
BraveQueryBuilder<BraveImageQuery,ImageSearchApiResponse>

A builder for constructing Brave Image Search API queries.

The builder is not thread-safe and is intended for single-threaded use. It is stateful and reusable: call reset() to clear its internal state before reusing it.

Builders are not intended to be instantiated directly; use the builder() factory method to create a new instance.

  • Method Details

    • builder

      public static BraveImageQuery builder()
      Creates a new instance of BraveImageQuery.
      Returns:
      a new instance of BraveImageQuery
    • query

      public BraveImageQuery query(String queryTerm)
      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

      public <T extends LanguageIdentifier> BraveImageQuery language(@NotNull T searchLanguage)
      Adds the search language option to the URL query.
      Type Parameters:
      T - the search language type
      Parameters:
      searchLanguage - the search language to set
      Returns:
      the current instance of the builder
      See Also:
    • safeSearch

      public BraveImageQuery safeSearch(@NotNull @NotNull SafeSearch safeSearch)
      Adds a safe search option to the URL query.
      Parameters:
      safeSearch - a SafeSearch enum value specifying the safe search level
      Returns:
      the current instance of the builder
    • country

      public <T extends CountryIdentifier> BraveImageQuery country(@NotNull T country)
      Adds the country option to the URL query.
      Type Parameters:
      T - the country type
      Parameters:
      country - the country identifier to set
      Returns:
      the current instance of the builder
      See Also:
    • count

      public BraveImageQuery count(int count)
      Adds the count option to the URL query.
      Parameters:
      count - the number of results to return
      Returns:
      the current instance of the builder
    • spellcheck

      public BraveImageQuery spellcheck(boolean spellCheck)
      Adds the spell check option to the URL query.
      Parameters:
      spellCheck - whether to enable spellcheck
      Returns:
      the current instance of the builder
    • withToken

      @Contract("_ -> this") public BraveImageQuery withToken(@NotNull @NotNull ClientInfo clientInfo)
      Sets the subscription token header using the provided ClientInfo instance.
      Parameters:
      clientInfo - the ClientInfo instance containing the subscription token
      Returns:
      the current instance of BraveImageQuery
    • withHeaders

      @Contract("_ -> this") public BraveImageQuery withHeaders(@NotNull @NotNull Consumer<VerticalHeaderBuilder<BraveVerticalRequest>> headers)
      Sets the request headers using the provided consumer.
      Parameters:
      headers - a consumer that receives a VerticalHeaderBuilder and applies the request's headers to it
      Returns:
      the current instance of BraveImageQuery
    • withRetries

      public BraveImageQuery withRetries(int maxRetries)
      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 BraveImageQuery
    • execute

      public BraveImageQuery 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 BraveQueryBuilder.getHttpResponse(), BraveQueryBuilder.getPOJO(), or BraveQueryBuilder.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:
      execute in interface BraveQueryBuilder<BraveImageQuery,ImageSearchApiResponse>
      Returns:
      the current builder instance
    • hasExecuted

      public 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.
      Specified by:
      hasExecuted in interface BraveQueryBuilder<BraveImageQuery,ImageSearchApiResponse>
      Returns:
      true if the request has been executed, false otherwise
    • toHttpRequest

      public HttpRequest toHttpRequest()
      Converts the current query to an HttpRequest instance.
      Specified by:
      toHttpRequest in interface BraveQueryBuilder<BraveImageQuery,ImageSearchApiResponse>
      Returns:
      the HttpRequest instance representing the current query
    • getResponseType

      public Class<ImageSearchApiResponse> getResponseType()
      Description copied from interface: BraveQueryBuilder
      Returns the type of the API response. The class returned by this method is the class of the concrete implementation of ApiResponse that this builder is expected to produce as a result of its query.
      Specified by:
      getResponseType in interface BraveQueryBuilder<BraveImageQuery,ImageSearchApiResponse>
      Returns:
      the class of the API response
    • getHttpResponse

      @NotNull public @NotNull Optional<HttpResponse<String>> getHttpResponse()
      Returns the current HTTP response as an Optional.

      If the request has not been executed, this method will execute the request and return the HttpResponse.

      Specified by:
      getHttpResponse in interface BraveQueryBuilder<BraveImageQuery,ImageSearchApiResponse>
      Returns:
      an Optional containing the current HTTP response.
    • reset

      public BraveImageQuery reset()
      Clears the current instance of BraveImageQuery by resetting its state to its initial values.
      Specified by:
      reset in interface BraveQueryBuilder<BraveImageQuery,ImageSearchApiResponse>
      Returns:
      the current instance of BraveImageQuery