Class Api

All Implemented Interfaces:
ApiConfig, ApiInt

public class Api extends ApiAbstract implements ApiInt, ApiConfig
Simple wrapper around RestAssured for sending HTTP requests.

For one instance run recommended: Api api = api.getInstance();

This client provides a convenient fluent API for:

  • Sending GET, POST, PUT and DELETE requests
  • Configuring request headers/query params
  • Using Basic or Bearer authentication
  • Enabling request/response logging
  • Support BugReaper YML config
  • Constructor Details

    • Api

      public Api(String url, int port)
      This constructor initializes client for interaction with API
      Parameters:
      url - service url ("http://my-service")
      port - service port
    • Api

      public Api()
      Constructs api client configuration.

      Loads configuration values from a YAML file.

      Default file: bugreaper.yml

      Custom file: using -DbugreaperEnv=test loads bugreaper-test.yml

       modules:
         api:
           url: http://localhost
           port: 1082
           username: user_1 # optional
           password: pass123 # optional
           token: my-token # optional more priority than base auth!
           logging: true # optional
           max-response-ms-assert: 2000 # optional
           max-timeout-ms: 1000 # optional
       

      Missing required keys will result in configuration errors. Missing optional keys will fall back to predefined defaults.

    • Api

      public Api(String suffix)
      Constructs api client configuration.
      Parameters:
      suffix - concatenation of Api client (example Api("-2") keys will be "modules.api-2")

      Loads configuration values from a YAML file.

      Default file: bugreaper.yml

      Custom file: using -DbugreaperEnv=test loads bugreaper-test.yml

       modules:
         api{suffix}: # example new Api("-2") key will be "api-2"
           url: http://localhost
           port: 8080
           username: user_2 # optional
           password: pass123 # optional
           token: my-token # optional more priority than base auth!
           logging: true # optional
           max-response-ms-assert: 2000 # optional
           max-timeout-ms: 1000 # optional
       

      Missing required keys will result in configuration errors. Missing optional keys will fall back to predefined defaults.

  • Method Details

    • getInstance

      public static Api getInstance()
      Returns the instance of Api with config builder Api().

      This implementation is thread-safe using method-level synchronization.

      Returns:
      the singleton instance of Api
      See Also:
    • setContentTypeJson

      public Api setContentTypeJson()
      Description copied from interface: ApiConfig
      Set content type JSON
      Specified by:
      setContentTypeJson in interface ApiConfig
      Returns:
      this instance for method chaining
    • setContentTypeXml

      public Api setContentTypeXml()
      Description copied from interface: ApiConfig
      Set content type XML
      Specified by:
      setContentTypeXml in interface ApiConfig
      Returns:
      this instance for method chaining
    • setContentType

      public Api setContentType(io.restassured.http.ContentType contentType)
      Description copied from interface: ApiConfig
      Set content type manually ContentType
      Specified by:
      setContentType in interface ApiConfig
      Parameters:
      contentType - ContentType:
      • ContentType.ANY
      • ContentType.TEXT
      • ContentType.JSON
      • ContentType.XML
      • ContentType.HTML
      • ContentType.URLENC
      • ContentType.BINARY
      • ContentType.MULTIPART
      Returns:
      this instance for method chaining
    • setMaxResponseMsAssert

      public Api setMaxResponseMsAssert(int maxResponseMs)
      Description copied from interface: ApiConfig
      Set maxResponseTimeout (does not break the connection - only assert)
      Specified by:
      setMaxResponseMsAssert in interface ApiConfig
      Parameters:
      maxResponseMs - assert ms for max response time
      Returns:
      this instance for method chaining
    • setTimeoutMs

      public Api setTimeoutMs(int maxTimeoutMs)
      Description copied from interface: ApiConfig
      Set max request timeout(connection/read)

      Drop connection on timeout is up

      Specified by:
      setTimeoutMs in interface ApiConfig
      Parameters:
      maxTimeoutMs - max ms for connection/read
      Returns:
      this instance for method chaining
    • setNoContentType

      public Api setNoContentType()
      Description copied from interface: ApiConfig
      Set content type absent
      Specified by:
      setNoContentType in interface ApiConfig
      Returns:
      this instance for method chaining
    • setHeader

      public Api setHeader(String key, Object value)
      Description copied from interface: ApiConfig
      Add or updates header globally (add to previously set)
      Specified by:
      setHeader in interface ApiConfig
      Parameters:
      key - header key
      value - header value
      Returns:
      this instance for method chaining
    • setHeaders

      public Api setHeaders(Map<String,Object> headers)
      Description copied from interface: ApiConfig
      Set headers globally (overwrite previously set)
      Specified by:
      setHeaders in interface ApiConfig
      Parameters:
      headers - Map (key, value)
      Returns:
      this instance for method chaining
    • cleanSetHeaders

      public Api cleanSetHeaders()
      Description copied from interface: ApiConfig
      Remove global headers
      Specified by:
      cleanSetHeaders in interface ApiConfig
      Returns:
      this instance for method chaining
    • withHeader

      public Api withHeader(String key, Object value)
      Description copied from interface: ApiConfig
      Add header for one next request (global headers will be ignored)
      • use for specific request
      • can be used multiple times to add multiple headers
      • after request global headers are taken into account again
      Specified by:
      withHeader in interface ApiConfig
      Parameters:
      key - header key
      value - header value
      Returns:
      this instance for method chaining
    • withHeaders

      public Api withHeaders(Map<String,Object> queryParams)
      Description copied from interface: ApiConfig
      Add headers for one next request (global headers will be ignored)
      • use for specific request
      • after request global headers are taken into account again
      Specified by:
      withHeaders in interface ApiConfig
      Parameters:
      queryParams - Map (key, value)
      Returns:
      this instance for method chaining
    • setQueryParams

      public Api setQueryParams(Map<String,Object> queryParams)
      Description copied from interface: ApiConfig
      Set query parameters globally (overwrite previously set)
      Specified by:
      setQueryParams in interface ApiConfig
      Parameters:
      queryParams - Map (key, value)
      Returns:
      this instance for method chaining
    • cleanSetQueryParams

      public Api cleanSetQueryParams()
      Description copied from interface: ApiConfig
      Remove global query parameters
      Specified by:
      cleanSetQueryParams in interface ApiConfig
      Returns:
      this instance for method chaining
    • withQueryParam

      public Api withQueryParam(String key, Object value)
      Description copied from interface: ApiConfig
      Add query parameter once for next request (global query parameters will be ignored)
      • use before specific request
      • can be used multiple times to add multiple query parameters
      • after request global query parameter are taken into account again
      Specified by:
      withQueryParam in interface ApiConfig
      Parameters:
      key - parameter key
      value - parameter value
      Returns:
      this instance for method chaining
    • withQueryParams

      public Api withQueryParams(Map<String,Object> queryParams)
      Description copied from interface: ApiConfig
      Add query parameters once for next request (global query parameters will be ignored)
      • use before specific request
      • after request global query parameter are taken into account again
      Specified by:
      withQueryParams in interface ApiConfig
      Parameters:
      queryParams - Map (key, value)
      Returns:
      this instance for method chaining
    • setBearerAuth

      public Api setBearerAuth(String token)
      Description copied from interface: ApiConfig
      Sets Bearer authentication
      Specified by:
      setBearerAuth in interface ApiConfig
      Parameters:
      token - String with token
      Returns:
      this instance for method chaining
    • setBasicAuth

      public Api setBasicAuth(String username, String password)
      Description copied from interface: ApiConfig
      Sets Basic authentication
      Specified by:
      setBasicAuth in interface ApiConfig
      Parameters:
      username - String with username
      password - String with password
      Returns:
      this instance for method chaining
    • setNoAuth

      public Api setNoAuth()
      Description copied from interface: ApiConfig
      Unset all authentications
      Specified by:
      setNoAuth in interface ApiConfig
      Returns:
      this instance for method chaining
    • setLogging

      public Api setLogging(boolean enable)
      Description copied from interface: ApiConfig
      Enables or disables logging manually (debug log level will print logs anyway!)
      Specified by:
      setLogging in interface ApiConfig
      Parameters:
      enable - true=request/response logging (Allure on always!)
      Returns:
      this instance for method chaining
    • getConfigSummary

      public String getConfigSummary()
      Description copied from interface: ApiConfig
      Returns and logs (at INFO level) a human-readable summary of all resolved configuration values.

      The summary includes values loaded from the YAML configuration file as well as any fields overridden programmatically after construction. Optional fields that were not present in the configuration and resolved via default values may also be included.

      Specified by:
      getConfigSummary in interface ApiConfig
      Returns:
      String with summary
    • sendGet

      @Step("(API) Send GET {path}") public AssertableResponse sendGet(String path)
      Description copied from interface: ApiInt
      Send GET request
      Specified by:
      sendGet in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      Returns:
      AssertableResponse
    • sendHead

      @Step("(API) Send HEAD {path}") public AssertableResponse sendHead(String path)
      Description copied from interface: ApiInt
      Send HEAD request
      Specified by:
      sendHead in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      Returns:
      AssertableResponse
    • sendOptions

      @Step("(API) Send OPTIONS {path}") public AssertableResponse sendOptions(String path)
      Description copied from interface: ApiInt
      Send OPTIONS request
      Specified by:
      sendOptions in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      Returns:
      AssertableResponse
    • sendPost

      @Step("(API) Send POST {path}") public AssertableResponse sendPost(String path, Object body)
      Description copied from interface: ApiInt
      Send POST request with body
      Specified by:
      sendPost in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      body - String with body
      Returns:
      AssertableResponse
    • sendPost

      @Step("(API) Send POST {path}") public AssertableResponse sendPost(String path)
      Description copied from interface: ApiInt
      Send POST request without body
      Specified by:
      sendPost in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      Returns:
      AssertableResponse
    • sendPut

      @Step("(API) Send PUT {path}") public AssertableResponse sendPut(String path, Object body)
      Description copied from interface: ApiInt
      Send PUT request with body
      Specified by:
      sendPut in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      body - String with body
      Returns:
      AssertableResponse
    • sendPut

      @Step("(API) Send PUT {path}") public AssertableResponse sendPut(String path)
      Description copied from interface: ApiInt
      Send PUT request without body
      Specified by:
      sendPut in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      Returns:
      AssertableResponse
    • sendPatch

      @Step("(API) Send PATCH {path}") public AssertableResponse sendPatch(String path, Object body)
      Description copied from interface: ApiInt
      Send PATCH request with body
      Specified by:
      sendPatch in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      body - String with body
      Returns:
      AssertableResponse
    • sendDelete

      @Step("(API) Send DELETE {path}") public AssertableResponse sendDelete(String path, Object body)
      Description copied from interface: ApiInt
      Send DELETE request with body
      Specified by:
      sendDelete in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      body - String with body
      Returns:
      AssertableResponse
    • sendDelete

      @Step("(API) Send DELETE {path}") public AssertableResponse sendDelete(String path)
      Description copied from interface: ApiInt
      Send DELETE request without body
      Specified by:
      sendDelete in interface ApiInt
      Parameters:
      path - String with path (/api/user1)
      Returns:
      AssertableResponse